using System.Text.Json.Serialization;
using Managing.Core.FixedSizedQueue;
using Managing.Domain.Candles;
using Managing.Domain.Scenarios;
using Managing.Domain.Strategies.Base;
using Managing.Domain.Users;
using static Managing.Common.Enums;

namespace Managing.Domain.Strategies
{
    public class Strategy : IStrategy
    {
        public Strategy(string name, StrategyType type)
        {
            Name = name;
            Type = type;
            SignalType = ScenarioHelpers.GetSignalType(type);
            Candles = new FixedSizeQueue<Candle>(500);
        }

        public string Name { get; set; }
        [JsonIgnore] public FixedSizeQueue<Candle> Candles { get; set; }
        public StrategyType Type { get; set; }
        public SignalType SignalType { get; set; }
        public int MinimumHistory { get; set; }
        public int? Period { get; set; }
        public int? FastPeriods { get; set; }
        public int? SlowPeriods { get; set; }
        public int? SignalPeriods { get; set; }
        public double? Multiplier { get; set; }
        public int? SmoothPeriods { get; set; }
        public int? StochPeriods { get; set; }
        public int? CyclePeriods { get; set; }
        public User User { get; set; }

        public virtual List<Signal> Run()
        {
            return new List<Signal>();
        }

        public virtual StrategiesResultBase GetStrategyValues()
        {
            return new StrategiesResultBase();
        }

        public void UpdateCandles(HashSet<Candle> newCandles)
        {
            if (newCandles == null || newCandles.Count == 0)
            {
                return;
            }

            lock (Candles)
            {
                foreach (var item in newCandles.ToList())
                {
                    if (Candles.All(c => c.Date != item.Date))
                    {
                        Candles.Enqueue(item);
                    }
                }
            }
        }

        public string GetName()
        {
            return Name;
        }
    }
}