using MBN_STOCK_WEBVIEW.Core.Playout.Scenes;
using MBN_STOCK_WEBVIEW.Playout.Configuration;

namespace MBN_STOCK_WEBVIEW.Playout.Tests;

public sealed class LegacyParityStartupCompositionResolverTests
{
    [Fact]
    public void AbsentLocalConfiguration_UsesValidatedLegacyDefaultAtConfiguredFade()
    {
        using var scenes = TemporarySceneDirectory.Create(
            "5001.t2s",
            PlayoutSceneCompositionFactory.LegacyStartupDefaultBackgroundAssetPath);
        CreateBackground(scenes, PlayoutSceneCompositionFactory.LegacyDefaultBackgroundFileName);
        var options = new PlayoutOptions
        {
            SceneDirectory = scenes.Path,
            LegacySceneFadeDuration = 11
        };

        var result = LegacyParityStartupCompositionResolver.Resolve(
            options,
            explicitLocalConfigurationExists: false);

        Assert.False(result.HasWarning);
        Assert.Equal(11, result.Composition.FadeDuration);
        Assert.Equal(LegacySceneBackgroundKind.Video, result.Composition.BackgroundKind);
        Assert.Equal(@"video\기본.vrv", result.Composition.BackgroundAssetPath);
        Assert.Equal(
            PlayoutAssetRoot.SceneDirectory,
            result.Composition.BackgroundAssetRoot);
    }

    [Fact]
    public void AbsentLocalConfigurationAndMissingDefault_RemainsNoneWithBoundedWarning()
    {
        using var scenes = TemporarySceneDirectory.Create("5001.t2s");
        CreateBackground(scenes, PlayoutSceneCompositionFactory.LegacyDefaultBackgroundFileName);
        var options = new PlayoutOptions
        {
            SceneDirectory = scenes.Path,
            LegacySceneFadeDuration = 9
        };

        var result = LegacyParityStartupCompositionResolver.Resolve(
            options,
            explicitLocalConfigurationExists: false);

        Assert.True(result.HasWarning);
        Assert.Equal(
            LegacyParityStartupCompositionResolver.MissingLegacyDefaultWarning,
            result.WarningMessage);
        Assert.DoesNotContain(scenes.Path, result.WarningMessage, StringComparison.Ordinal);
        Assert.InRange(result.WarningMessage.Length, 1, 128);
        Assert.Equal(9, result.Composition.FadeDuration);
        Assert.Equal(LegacySceneBackgroundKind.None, result.Composition.BackgroundKind);
        Assert.Null(result.Composition.BackgroundAssetPath);
    }

    [Fact]
    public void ExplicitLocalNone_IsHonoredEvenWhenLegacyDefaultExists()
    {
        using var scenes = TemporarySceneDirectory.Create(
            "5001.t2s",
            PlayoutSceneCompositionFactory.LegacyStartupDefaultBackgroundAssetPath);
        var options = new PlayoutOptions
        {
            SceneDirectory = scenes.Path,
            LegacySceneBackgroundKind = LegacySceneBackgroundKind.None
        };

        var result = LegacyParityStartupCompositionResolver.Resolve(
            options,
            explicitLocalConfigurationExists: true);

        Assert.False(result.HasWarning);
        Assert.Equal(LegacySceneBackgroundKind.None, result.Composition.BackgroundKind);
        Assert.Null(result.Composition.BackgroundAssetPath);
    }

    [Fact]
    public void ExplicitLocalAsset_IsHonoredWithoutReplacingItWithLegacyDefault()
    {
        using var scenes = TemporarySceneDirectory.Create("5001.t2s");
        CreateBackground(
            scenes,
            PlayoutSceneCompositionFactory.LegacyDefaultBackgroundFileName,
            "custom.vrv");
        var options = new PlayoutOptions
        {
            SceneDirectory = scenes.Path,
            LegacySceneBackgroundKind = LegacySceneBackgroundKind.Video,
            LegacySceneBackgroundAssetPath = "custom.vrv"
        };

        var result = LegacyParityStartupCompositionResolver.Resolve(
            options,
            explicitLocalConfigurationExists: true);

        Assert.False(result.HasWarning);
        Assert.Equal(LegacySceneBackgroundKind.Video, result.Composition.BackgroundKind);
        Assert.Equal("custom.vrv", result.Composition.BackgroundAssetPath);
    }

    private static void CreateBackground(
        TemporarySceneDirectory scenes,
        params string[] fileNames)
    {
        var background = Path.Combine(Directory.GetParent(scenes.Path)!.FullName, "배경");
        Directory.CreateDirectory(background);
        foreach (var fileName in fileNames)
        {
            File.WriteAllText(Path.Combine(background, fileName), "trusted test background");
        }
    }
}
