﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WhiteLightCommon;
using WhiteLightCommon.Indicators;
using WhiteLightCommon.Utils;

namespace StockDataManager
{
    public class VixHighLowStrategy : VixBaseStrategy
    {
        private static int yellowWindow = 3;

        public VixHighLowStrategy() : base()
        {
        }

        protected override void Execute()
        {
            ProcessGreen(12);
        }

        private void ProcessGreen(int index)
        {
            vixRegime[vixData[index].date] = VixType.Green;
            Console.Write("Green - " + vixData[index].date.ToShortDateString() + " - ");
            index++;
            for (; index <= vixData.Count; index++)
            {
                if (IsRed(index))
                {
                    Console.WriteLine(vixData[index].date.ToShortDateString());
                    ProcessRed(index);
                    return;
                }
                else
                {
                    vixRegime[vixData[index].date] = VixType.Green;
                }
            }
        }

        private void ProcessRed(int index)
        {
            vixRegime[vixData[index].date] = VixType.Red;
            Console.Write("Red - " + vixData[index].date.ToShortDateString() + " - ");
            index++;

            for (; index <= vixData.Count; index++)
            {
                if (IsGreen(index))
                {
                    Console.WriteLine(vixData[index].date.ToShortDateString());
                    ProcessGreen(index);
                    return;
                }
                else
                {
                    vixRegime[vixData[index].date] = VixType.Red;
                }
            }
        }

        private bool IsGreen(int index)
        {
            return isLow(index)
            || ((GetHighLowRelative(index) > 1.2) &&
                (vixData[index].close < vixData[index - 1].close) &&
                (vixData[index].close < vixData[index].open));
            // ||                (vixData[index].close < MovingAverageHelper.GetSimpleMovingAverage(index, 10, vixData));
        }

        private double GetHighLowRelative(int index)
        {
            return (3 * (vixData[index].high - vixData[index].low)) /
                ((vixData[index - 1].high - vixData[index - 1].low) + (vixData[index - 2].high - vixData[index - 2].low) +
                (vixData[index - 3].high - vixData[index - 3].low));
        }

        private bool IsRed(int index)
        {
            return GetHighLowRelative(index) > 1.2 &&
                (vixData[index].close > vixData[index - 1].close)
            && (vixData[index].close > MovingAverageHelper.GetSimpleMovingAverage(index, 10, vixData));
        }
    }
}