using static Managing.Common.Enums;

namespace Managing.Domain.Candles;

public static class CandleExtensions
{

    public static Candle SetupClosingCandle(this Candle candle)
    {

        return candle;
    }

    public static DateTime GetBotPreloadSinceFromTimeframe(Timeframe timeframe)
    {
        return timeframe switch
        {
            Timeframe.FiveMinutes => DateTime.UtcNow.AddDays(-1),
            Timeframe.FifteenMinutes => DateTime.UtcNow.AddDays(-5),
            Timeframe.ThirtyMinutes => DateTime.UtcNow.AddDays(-10),
            Timeframe.OneHour => DateTime.UtcNow.AddDays(-30),
            Timeframe.FourHour => DateTime.UtcNow.AddDays(-60),
            Timeframe.OneDay => DateTime.UtcNow.AddDays(-360),
            _ => DateTime.Now.AddHours(-15),
        };
    }

    public static DateTime GetPreloadSinceFromTimeframe(Timeframe timeframe)
    {
        return DateTime.UtcNow.AddDays(GetMinimalDays(timeframe));
    }

    public static int GetIntervalFromTimeframe(Timeframe timeframe)
    {
        var seconds = GetBaseIntervalInSeconds(timeframe);
        // Run every 1/5th of the candle duration
        return seconds / 5 * 1000; // milliseconds
    }

    /// <summary>
    /// Gets the interval in minutes for the given timeframe.
    /// This is useful for cooldown period calculations.
    /// </summary>
    /// <param name="timeframe">The timeframe to get the interval for</param>
    /// <returns>The interval in minutes</returns>
    public static double GetIntervalInMinutes(Timeframe timeframe)
    {
        var seconds = GetBaseIntervalInSeconds(timeframe);
        // Run every 1/5th of the candle duration
        return (seconds / 5.0) / 60.0; // minutes
    }

    /// <summary>
    /// Gets the base interval in seconds for the given timeframe.
    /// This is the core interval logic that can be used for various calculations.
    /// </summary>
    /// <param name="timeframe">The timeframe to get the base interval for</param>
    /// <returns>The base interval in seconds</returns>
    public static int GetBaseIntervalInSeconds(Timeframe timeframe)
    {
        return timeframe switch
        {
            Timeframe.OneDay => 86400,
            Timeframe.FourHour => 14400,
            Timeframe.OneHour => 3600,
            Timeframe.ThirtyMinutes => 1800,
            Timeframe.FifteenMinutes => 900,
            Timeframe.FiveMinutes => 300,
            _ => 300,
        };
    }

    public static int GetUnixInterval(this Timeframe timeframe)
    {
        return timeframe switch
        {
            Timeframe.OneDay => 86400,
            Timeframe.FiveMinutes => 300,
            Timeframe.FifteenMinutes => 900,
            Timeframe.FourHour => 14400,
            Timeframe.OneHour => 3600,
            _ => throw new NotImplementedException()
        };
    }

    public static double GetMinimalDays(Timeframe timeframe)
    {
        return timeframe switch
        {
            Timeframe.FiveMinutes => -1,
            Timeframe.FifteenMinutes => -5,
            Timeframe.ThirtyMinutes => -10,
            Timeframe.OneHour => -30,
            Timeframe.FourHour => -60,
            Timeframe.OneDay => -360,
            _ => throw new NotImplementedException()
        };
    }
}
