#nullable enable

using System.Collections.ObjectModel;

namespace MBN_STOCK_WEBVIEW.Core.Playout.Scenes;

/// <summary>
/// Closed refresh behavior reproduced from MainForm.Show_PlayList's m_time assignments
/// and its subsequent WinForms Timer.Interval assignment. Browser input cannot select
/// or alter these values.
/// </summary>
public static class LegacySceneRefreshPolicy
{
    public static TimeSpan RecurringInterval { get; } = TimeSpan.FromSeconds(3);

    private static readonly IReadOnlyDictionary<string, TimeSpan> Intervals =
        new ReadOnlyDictionary<string, TimeSpan>(new Dictionary<string, TimeSpan>(
            StringComparer.Ordinal)
        {
            ["5001"] = Seconds(2),
            ["N5001"] = Seconds(2),
            ["5006"] = Seconds(5),
            ["5011"] = Seconds(5),
            ["5016"] = Seconds(5),
            ["50160"] = Seconds(30),
            ["5023"] = Seconds(30),
            ["5024"] = Seconds(60),
            ["5025"] = Seconds(30),
            ["5026"] = Seconds(8),
            ["5029"] = Seconds(7),
            ["5032"] = Seconds(5),
            ["5037"] = Seconds(5),
            ["5074"] = Seconds(7),
            ["5077"] = Seconds(7),
            ["5078"] = Seconds(30),
            ["5082"] = Seconds(6),
            ["5083"] = Seconds(60),
            ["5084"] = Seconds(5),
            ["5085"] = Seconds(30),
            ["5086"] = Seconds(7),
            ["50860"] = Seconds(7),
            ["5087"] = Seconds(7),
            ["5088"] = Seconds(7),
            ["6001"] = Seconds(30),
            ["6067"] = Seconds(30),
            ["8001"] = Seconds(120),
            ["8002"] = Seconds(120),
            ["8003"] = Seconds(8),
            ["8018"] = Seconds(5),
            ["8032"] = Seconds(5),
            ["8035"] = Seconds(8),
            ["8040"] = Seconds(8),
            ["8046"] = Seconds(8),
            ["8051"] = Seconds(8),
            ["8056"] = Seconds(8),
            ["8061"] = Seconds(8),
            ["8067"] = Seconds(30),
            ["5068"] = Seconds(30),
            ["5070"] = Seconds(30),
            ["5072"] = Seconds(30)
        });

    // MainForm stores 3,000,000 seconds, then evaluates m_time * 1000 as an
    // unchecked Int32. The result overflows negative and assigning it to the
    // WinForms Timer throws inside btnTake's swallowed catch after timer.Stop().
    // These scenes therefore never arm automatic refresh in the original runtime.
    private static readonly IReadOnlySet<string> DisabledByTimerOverflow =
        new HashSet<string>(["5076", "5079", "5080", "5081"], StringComparer.Ordinal);

    public static int Count => Intervals.Count + DisabledByTimerOverflow.Count;

    public static bool ContainsAlias(string? cutCode) =>
        !string.IsNullOrWhiteSpace(cutCode) &&
        (Intervals.ContainsKey(cutCode) || DisabledByTimerOverflow.Contains(cutCode));

    public static bool IsDisabledByLegacyTimerOverflow(string? cutCode) =>
        !string.IsNullOrWhiteSpace(cutCode) &&
        DisabledByTimerOverflow.Contains(cutCode);

    public static bool TryGetInterval(string? cutCode, out TimeSpan interval)
    {
        if (string.IsNullOrWhiteSpace(cutCode))
        {
            interval = default;
            return false;
        }

        return Intervals.TryGetValue(cutCode, out interval);
    }

    private static TimeSpan Seconds(int seconds) => TimeSpan.FromSeconds(seconds);
}
