using Managing.Domain.Scenarios;
using Orleans;
using static Managing.Common.Enums;

namespace Managing.Domain.Strategies;

/// <summary>
/// Lightweight indicator class for Orleans serialization
/// Contains only the essential properties needed for backtesting
/// </summary>
[GenerateSerializer]
public class LightIndicator
{
    public LightIndicator(string name, IndicatorType type)
    {
        Name = name;
        Type = type;
        SignalType = ScenarioHelpers.GetSignalType(type);
    }

    [Id(0)] public string Name { get; set; }

    [Id(1)] public IndicatorType Type { get; set; }

    [Id(2)] public SignalType SignalType { get; set; }

    [Id(3)] public int MinimumHistory { get; set; }

    [Id(4)] public int? Period { get; set; }

    [Id(5)] public int? FastPeriods { get; set; }

    [Id(6)] public int? SlowPeriods { get; set; }

    [Id(7)] public int? SignalPeriods { get; set; }

    [Id(8)] public double? Multiplier { get; set; }

    [Id(9)] public int? SmoothPeriods { get; set; }

    [Id(10)] public int? StochPeriods { get; set; }

    [Id(11)] public int? CyclePeriods { get; set; }

    /// <summary>
    /// Converts a full Indicator to a LightIndicator
    /// </summary>
    public static LightIndicator BaseToLight(IndicatorBase indicatorBase)
    {
        return new LightIndicator(indicatorBase.Name, indicatorBase.Type)
        {
            SignalType = indicatorBase.SignalType,
            MinimumHistory = indicatorBase.MinimumHistory,
            Period = indicatorBase.Period,
            FastPeriods = indicatorBase.FastPeriods,
            SlowPeriods = indicatorBase.SlowPeriods,
            SignalPeriods = indicatorBase.SignalPeriods,
            Multiplier = indicatorBase.Multiplier,
            SmoothPeriods = indicatorBase.SmoothPeriods,
            StochPeriods = indicatorBase.StochPeriods,
            CyclePeriods = indicatorBase.CyclePeriods
        };
    }

    /// <summary>
    /// Converts a LightIndicator back to a full Indicator
    /// </summary>
    public IIndicator ToInterface()
    {
        // Use the factory method to create the correct indicator type
        return ScenarioHelpers.BuildIndicator(this);
    }

    public IndicatorBase LightToBase()
    {
        var baseIndicator = new IndicatorBase(Name, Type)
        {
            SignalType = SignalType,
            MinimumHistory = MinimumHistory,
            Period = Period,
            FastPeriods = FastPeriods,
            SlowPeriods = SlowPeriods,
            SignalPeriods = SignalPeriods,
            Multiplier = Multiplier,
            SmoothPeriods = SmoothPeriods,
            StochPeriods = StochPeriods,
            CyclePeriods = CyclePeriods
        };

        return baseIndicator;
    }
}