using BinanceExchange.API.Models.Response;
using Newtonsoft.Json;
using SignalsTest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SignalsTest
{
    public class TAReport
    {
        public string pair;
        public int interval;
        [JsonIgnore]
        public KlineCandleStickResponse candle;
        public decimal Close;
        public decimal Open;
        public decimal High;
        public decimal Low;
        public DateTime CloseTime;
        public decimal SMA7;
        public decimal SMA25;
        public decimal SMA99;

        public decimal EMA7, EMA25, EMA99;

        public decimal MACD = 0;
        public decimal EMACD = 0;

        public decimal ATR14;
        public decimal ATR10;
        public decimal AVGDiff = 0;

        public decimal STHigh;
        public decimal STLow;
        public decimal ST;

        public decimal RSI = 0;

        public bool STUp = false;
        public bool TGOR=false;
        public static TAReport Generate(string _pair, int _interval, List<KlineCandleStickResponse> response, int index, List<TAReport> history)
        {
            TAReport report = new TAReport();
            report.pair = _pair;
            report.interval = _interval;
            report.candle = response[index];
            report.Open = response[index].Open;
            report.Close = response[index].Close;
            report.High = response[index].High;
            report.Low = response[index].Low;
            report.CloseTime = response[index].CloseTime;
            // foreach(int sma in m_SMA)
            // {
            //     report.SMAs.Add(sma, Indicators.getSMA(response, index, sma));
            // }

            // foreach (int ema in m_EMA)
            // {
            //     report.SMAs.Add(ema, Indicators.getEMA(response, index, 0, ema));
            // }
            report.SMA7 = Indicators.getSMA(response, index, 7);
            report.SMA25 = Indicators.getSMA(response, index, 25);
            report.SMA99 = Indicators.getSMA(response, index, 99);

            report.EMA7 = Indicators.getEMA(response, index, 0, 7);
            report.EMA25 = Indicators.getEMA(response, index, 0, 25);
            report.EMA99 = Indicators.getEMA(response, index, 0, 99);
            report.MACD = Indicators.getMACD(response, 12, 26, index);

            report.RSI = Indicators.getRSI(response, index);

            if (history == null)
            {
                return report;
            }
            //MACD Signal
            List<decimal> MACDHistory = new List<decimal>();
            List<decimal> ATR10History = new List<decimal>();
            List<decimal> ATR14History = new List<decimal>();

            foreach (TAReport item in history)
            {
                MACDHistory.Add(item.MACD);
                ATR10History.Add(item.ATR10);
                ATR14History.Add(item.ATR14);
            }
            report.EMACD = Indicators.getEMA(MACDHistory, index, 0, 9);

            report.ATR10 = Indicators.getATR(response, 10, index, ATR10History);
            report.ATR14 = Indicators.getATR(response, 14, index, ATR14History);

            // SuperTrend Multiplier
            decimal multiplier = 3;

            // Basic Upper and Lower Bands
            decimal basicUpperBand = (response[index].High + response[index].Low) / 2 + multiplier * report.ATR14;
            decimal basicLowerBand = (response[index].High + response[index].Low) / 2 - multiplier * report.ATR10;

            // Initialize Final Upper and Lower Bands
            decimal finalUpperBand = basicUpperBand;
            decimal finalLowerBand = basicLowerBand;

            // Adjust Final Upper and Lower Bands based on previous history
            if (index > 0)
            {
                finalUpperBand = (history[index - 1].Close <= history[index - 1].STHigh)
                    ? Math.Min(basicUpperBand, history[index - 1].STHigh)
                    : basicUpperBand;

                finalLowerBand = (history[index - 1].Close >= history[index - 1].STLow)
                    ? Math.Max(basicLowerBand, history[index - 1].STLow)
                    : basicLowerBand;
            }

            bool currentTrendUp = true;

            if (index > 0)
            {
                if (history[index - 1].STUp) // If the previous trend was up
                {
                    currentTrendUp = (response[index].Close > finalLowerBand); // Price > Final Lower Band => Stay up
                }
                else // If the previous trend was down
                {
                    currentTrendUp = (response[index].Close >= finalUpperBand); // Price >= Final Upper Band => Flip to up
                }
            }


            // Assign SuperTrend values to the report
            report.STHigh = finalUpperBand;
            report.STLow = finalLowerBand;
            report.STUp = currentTrendUp;
            report.ST = currentTrendUp ? finalLowerBand : finalUpperBand;
            
            report.TGOR= Patterns.GetTGOR(response, index);

            return report;
        }


    }
}


public static class CustomExtensions
{

    public static string toJson(this TAReport report)
    {
        return JsonConvert.SerializeObject(report, Formatting.Indented);
    }
}