using MBN_STOCK_WEBVIEW.Core.Playout.Scenes;

namespace MBN_STOCK_WEBVIEW.Core.Tests;

public sealed class LegacySceneRefreshPolicyTests
{
    public static TheoryData<string, int> OriginalMainFormIntervals => new()
    {
        { "5001", 2 }, { "N5001", 2 }, { "5006", 5 }, { "5011", 5 },
        { "5016", 5 }, { "50160", 30 }, { "5023", 30 }, { "5024", 60 },
        { "5025", 30 }, { "5026", 8 }, { "5029", 7 }, { "5032", 5 },
        { "5037", 5 }, { "5074", 7 }, { "5077", 7 }, { "5078", 30 },
        { "5082", 6 }, { "5083", 60 }, { "5084", 5 },
        { "5085", 30 }, { "5086", 7 }, { "50860", 7 }, { "5087", 7 },
        { "5088", 7 }, { "6001", 30 }, { "6067", 30 }, { "8001", 120 },
        { "8002", 120 }, { "8003", 8 }, { "8018", 5 }, { "8032", 5 },
        { "8035", 8 }, { "8040", 8 }, { "8046", 8 }, { "8051", 8 },
        { "8056", 8 }, { "8061", 8 }, { "8067", 30 }, { "5068", 30 },
        { "5070", 30 }, { "5072", 30 }
    };

    [Theory]
    [MemberData(nameof(OriginalMainFormIntervals))]
    public void EveryActiveAliasUsesOriginalMainFormInterval(string alias, int seconds)
    {
        Assert.True(LegacySceneRefreshPolicy.TryGetInterval(alias, out var interval));
        Assert.Equal(TimeSpan.FromSeconds(seconds), interval);
    }

    [Theory]
    [InlineData("5076")]
    [InlineData("5079")]
    [InlineData("5080")]
    [InlineData("5081")]
    public void OverflowedMainFormTimerAliasesDoNotArmAutomaticRefresh(string alias)
    {
        Assert.True(LegacySceneRefreshPolicy.ContainsAlias(alias));
        Assert.True(LegacySceneRefreshPolicy.IsDisabledByLegacyTimerOverflow(alias));
        Assert.False(LegacySceneRefreshPolicy.TryGetInterval(alias, out _));
    }

    [Fact]
    public void PolicyCoversExactlyTheCatalogsActiveAliases()
    {
        var aliases = LegacySceneCatalog.Definitions
            .SelectMany(definition => definition.CutAliases)
            .Distinct(StringComparer.Ordinal)
            .ToArray();

        Assert.Equal(45, aliases.Length);
        Assert.Equal(aliases.Length, LegacySceneRefreshPolicy.Count);
        Assert.All(aliases, alias =>
            Assert.True(LegacySceneRefreshPolicy.ContainsAlias(alias), alias));
        Assert.False(LegacySceneRefreshPolicy.TryGetInterval("8086", out _));
        Assert.False(LegacySceneRefreshPolicy.TryGetInterval("../5001", out _));
        Assert.Equal(TimeSpan.FromSeconds(3), LegacySceneRefreshPolicy.RecurringInterval);
    }
}
