using Managing.Core;
using Managing.Domain.Indicators;
using Managing.Domain.Shared.Rules;
using Managing.Domain.Strategies.Base;
using Skender.Stock.Indicators;
using static Managing.Common.Enums;
using Candle = Managing.Domain.Candles.Candle;

namespace Managing.Domain.Strategies.Signals;

public class RsiDivergenceConfirmIndicatorBase : IndicatorBase
{
    public List<LightSignal> Signals { get; set; }

    public RsiDivergenceConfirmIndicatorBase(string name, int period) : base(name, IndicatorType.RsiDivergenceConfirm)
    {
        Period = period;
        Signals = new List<LightSignal>();
    }

    /// <summary>
    /// Get RSI signals
    /// </summary>
    /// <returns></returns>
    public override List<LightSignal> Run(HashSet<Candle> candles)
    {
        if (candles.Count <= Period)
        {
            return null;
        }

        var ticker = candles.First().Ticker;

        try
        {
            var rsiResult = candles.TakeLast(10 * Period.Value).GetRsi(Period.Value).ToList();
            var candlesRsi = MapRsiToCandle(rsiResult, candles.TakeLast(10 * Period.Value));

            if (candlesRsi.Count(c => c.Rsi > 0) == 0)
                return null;

            GetLongSignals(candlesRsi, candles);
            GetShortSignals(candlesRsi, candles);

            return Signals.Where(s => s.Confidence != Confidence.None).OrderBy(s => s.Date).ToList();
        }
        catch (RuleException)
        {
            return null;
        }
    }

    public override IndicatorsResultBase GetIndicatorValues(HashSet<Candle> candles)
    {
        return new IndicatorsResultBase()
        {
            Rsi = candles.GetRsi(Period.Value).ToList()
        };
    }

    private void GetLongSignals(List<CandleRsi> candlesRsi, HashSet<Candle> candles)
    {
        // Set the low and high for first candle
        var firstCandleRsi = candlesRsi.First(c => c.Rsi > 0);
        var highPrices = new List<CandleRsi>();
        var lowPrices = new List<CandleRsi>();

        var highRsi = new List<CandleRsi>();
        var lowRsi = new List<CandleRsi>();

        highPrices.Add(firstCandleRsi);
        lowPrices.Add(firstCandleRsi);

        highRsi.Add(firstCandleRsi);
        lowRsi.Add(firstCandleRsi);

        var previousCandle = firstCandleRsi;

        // For a long 
        foreach (var currentCandle in candlesRsi.FindAll(r => r.Rsi > 0).Skip(1))
        {
            // If price go down
            if (previousCandle.Close > currentCandle.Close)
            {
                // because the last price is upper than the current
                highPrices.AddItem(previousCandle);

                // Check if rsi is higher than the last lowest
                if (currentCandle.Rsi > lowRsi.TakeLast(Period.Value).Min(r => r.Rsi))
                {
                    // If new higher high, we set it
                    if (currentCandle.Rsi > highRsi.Last().Rsi)
                        highRsi.AddItem(currentCandle);

                    if (currentCandle.Rsi > lowRsi.Last().Rsi)
                        lowRsi.AddItem(currentCandle);

                    // Price go down but RSI go up
                    if (currentCandle.Close < lowPrices.TakeLast(Period.Value).Min(p => p.Close))
                    {
                        AddSignal(currentCandle, TradeDirection.Long, Confidence.None);
                    }
                }
                else
                {
                    // No divergence, price go down, rsi go down
                    lowRsi.AddItem(currentCandle);
                }

                lowPrices.AddItem(currentCandle);
            }
            else
            {
                // Price go up, so we have to update if price is a new higher high than previous candle
                // Normally always true
                if (previousCandle.Close < currentCandle.Close)
                    highPrices.AddItem(currentCandle); //15-15-12-14-17

                // If rsi is lower low or not set
                if (currentCandle.Rsi < lowRsi.Last().Rsi || lowRsi.Last().Rsi == 0)
                    lowRsi.AddItem(currentCandle);

                // Price going up, so if its a new high we set it
                if (currentCandle.Rsi > highRsi.Last().Rsi)
                    highRsi.AddItem(currentCandle);
            }

            CheckIfConfimation(currentCandle, TradeDirection.Long, candles);

            previousCandle = currentCandle;
        }
    }

    private void GetShortSignals(List<CandleRsi> candlesRsi, HashSet<Candle> candles)
    {
        // Set the low and high for first candle
        var firstCandleRsi = candlesRsi.First(c => c.Rsi > 0);

        var signals = new List<Signal>();
        var highPrices = new List<CandleRsi>();
        var lowPrices = new List<CandleRsi>();

        var highRsi = new List<CandleRsi>();
        var lowRsi = new List<CandleRsi>();

        highPrices.Add(firstCandleRsi);
        lowPrices.Add(firstCandleRsi);

        highRsi.Add(firstCandleRsi);
        lowRsi.Add(firstCandleRsi);

        var previousCandle = firstCandleRsi;

        // For a short 
        foreach (var currentCandle in candlesRsi.FindAll(r => r.Rsi > 0).Skip(1))
        {
            // If price go up
            if (previousCandle.Close < currentCandle.Close)
            {
                // because the last price is lower than the current
                lowPrices.AddItem(previousCandle);

                // Check if rsi is lower than the last high
                if (currentCandle.Rsi < highRsi.TakeLast(Period.Value).Max(r => r.Rsi))
                {
                    // If new lower low, we set it
                    if (currentCandle.Rsi < lowRsi.Last().Rsi)
                        lowRsi.AddItem(currentCandle);

                    if (currentCandle.Rsi < highRsi.Last().Rsi)
                        highRsi.AddItem(currentCandle);

                    // Price go up but RSI go down
                    if (currentCandle.Close > highPrices.TakeLast(Period.Value).Max(p => p.Close))
                    {
                        AddSignal(currentCandle, TradeDirection.Short, Confidence.None);
                    }
                }
                else
                {
                    // No divergence, price go up, rsi go up
                    highRsi.AddItem(currentCandle);
                }

                highPrices.AddItem(currentCandle);
            }
            else
            {
                // Price go down, so we have to update if price is a new lower low than previous candle
                if (previousCandle.Close > currentCandle.Close)
                    lowPrices.AddItem(currentCandle);

                // If rsi is higher high or not set
                if (currentCandle.Rsi > highRsi.Last().Rsi || highRsi.Last().Rsi == 0)
                    highRsi.AddItem(currentCandle);

                // Price going down, so if its a new low we set it
                if (currentCandle.Rsi < lowRsi.Last().Rsi)
                    lowRsi.AddItem(currentCandle);
            }

            CheckIfConfimation(currentCandle, TradeDirection.Short, candles);

            previousCandle = currentCandle;
        }
    }

    private void CheckIfConfimation(CandleRsi currentCandle, TradeDirection direction, HashSet<Candle> candles)
    {
        var lastCandleOnPeriod = candles.TakeLast(Period.Value).ToList();
        var signalsOnPeriod = Signals.Where(s => s.Date >= lastCandleOnPeriod[0].Date
                                                 && s.Date < currentCandle.Date
                                                 && s.Direction == direction
                                                 && s.Confidence == Confidence.None
                                                 && s.Status != SignalStatus.Expired
                                                 && s.Status != SignalStatus.PositionOpen).ToList();

        foreach (var signal in signalsOnPeriod)
        {
            if (direction == TradeDirection.Short && currentCandle.Close < signal.Candle.Open)
            {
                AddSignal(currentCandle, direction, Confidence.High);
                Signals.FirstOrDefault(s => s.Identifier == signal.Identifier).Status = SignalStatus.Expired;
            }

            if (direction == TradeDirection.Long && currentCandle.Close > signal.Candle.Open)
            {
                AddSignal(currentCandle, direction, Confidence.High);
                Signals.FirstOrDefault(s => s.Identifier == signal.Identifier).Status = SignalStatus.Expired;
            }
        }
    }

    private void AddSignal(CandleRsi candleSignal, TradeDirection direction, Confidence confidence)
    {
        var signal = new LightSignal(MiscExtensions.ParseEnum<Ticker>(candleSignal.Ticker), direction, confidence,
            candleSignal, candleSignal.Date, candleSignal.Exchange, Type, SignalType, Name);
        if (!Signals.Any(s => s.Identifier == signal.Identifier))
        {
            Signals.AddItem(signal);
        }
    }

    private List<CandleRsi> MapRsiToCandle(IReadOnlyCollection<RsiResult> rsiResult,
        IEnumerable<Candle> candles)
    {
        return candles.Select(c => new CandleRsi()
        {
            Close = c.Close,
            Open = c.Open,
            Rsi = rsiResult.Find(c.Date).Rsi.GetValueOrDefault(),
            Date = c.Date,
            Ticker = c.Ticker,
            Exchange = c.Exchange
        }).ToList();
    }

    private class CandleRsi : Candle
    {
        public double Rsi { get; set; }
    }
}