using MBN_STOCK_WEBVIEW.Core.Playout.Scenes;

namespace MBN_STOCK_WEBVIEW.LegacyApplication.Tests;

public sealed class LegacyCutInfoSceneAliasResolverTests
{
    [Theory]
    [InlineData("2열판- 라인그래프", "50160")]
    [InlineData("영업이익 1열판기본", "5081")]
    [InlineData("(NXT) 1열판 기본", "N5001")]
    [InlineData("1열판 상세 시가", "5006")]
    [InlineData("종목별 비교분석_라인 그래프", "5087")]
    [InlineData("매매동향 라인그래프", "5084")]
    [InlineData("캔들그래프 120일 20일", "8051")]
    public void TryResolve_PreservesOriginalOrderedPrecedence(string info, string expected)
    {
        Assert.True(LegacyCutInfoSceneAliasResolver.TryResolve(
            info, string.Empty, string.Empty, string.Empty, out var alias));
        Assert.Equal(expected, alias);
    }

    [Fact]
    public void TryResolve_RemovesAsciiSpacesBeforeMatching()
    {
        Assert.True(LegacyCutInfoSceneAliasResolver.TryResolve(
            " 종 목 별 비 교 분 석 _ 캔 들 그 래 프 ",
            string.Empty,
            string.Empty,
            string.Empty,
            out var alias));

        Assert.Equal("5026", alias);
    }

    [Fact]
    public void TryResolve_CombinesTheFourVisibleFieldsInOriginalOrder()
    {
        Assert.True(LegacyCutInfoSceneAliasResolver.TryResolve(
            "코스피",
            "삼성전자",
            "1열판",
            "기본",
            out var alias));

        Assert.Equal("5001", alias);
    }

    [Fact]
    public void TryResolve_ImageGraphPreservesOriginalShortKeySpecialCase()
    {
        Assert.True(LegacyCutInfoSceneAliasResolver.TryResolve(
            "영업이익",
            "미국",
            "이미지 그래프",
            "매출액",
            out var alias));

        Assert.Equal("6001", alias);
    }

    [Fact]
    public void TryResolve_DoesNotRemoveNonAsciiWhitespace()
    {
        Assert.False(LegacyCutInfoSceneAliasResolver.TryResolve(
            "1\t열판기본",
            string.Empty,
            string.Empty,
            string.Empty,
            out var alias));

        Assert.Equal(string.Empty, alias);
    }

    [Theory]
    [InlineData("")]
    [InlineData("등록되지 않은 컷")]
    [InlineData("캔들그래프")]
    [InlineData("2열판")]
    public void TryResolve_RejectsUnknownOrIncompleteLegacyText(string info)
    {
        Assert.False(LegacyCutInfoSceneAliasResolver.TryResolve(
            info, string.Empty, string.Empty, string.Empty, out var alias));
        Assert.Equal(string.Empty, alias);
    }

    [Fact]
    public void TryResolve_RejectsMissingField()
    {
        Assert.False(LegacyCutInfoSceneAliasResolver.TryResolve(
            null!, string.Empty, string.Empty, string.Empty, out var alias));
        Assert.Equal(string.Empty, alias);
    }

    [Theory]
    [MemberData(nameof(EveryOriginalResult))]
    public void TryResolve_ReturnsOnlyRegisteredSceneAliases(string info, string expected)
    {
        Assert.True(LegacyCutInfoSceneAliasResolver.TryResolve(
            info, string.Empty, string.Empty, string.Empty, out var alias));
        Assert.Equal(expected, alias);
        Assert.NotEmpty(LegacySceneCatalog.FindByCutAlias(alias));
    }

    public static TheoryData<string, string> EveryOriginalResult => new()
    {
        { "2열판-", "50160" },
        { "영업이익", "5081" },
        { "매출액", "5080" },
        { "성장성지표", "5079" },
        { "주요매출구성", "5076" },
        { "(NXT)1열판기본", "N5001" },
        { "1열판기본", "5001" },
        { "1열판상세시가", "5006" },
        { "1열판상세", "5011" },
        { "종목별비교분석_캔들그래프", "5026" },
        { "종목별비교분석_라인그래프", "5087" },
        { "종목별수익률비교", "5029" },
        { "수익률그래프", "5086" },
        { "캔들그래프일봉", "8035" },
        { "캔들그래프5일", "8061" },
        { "캔들그래프120일", "8051" },
        { "캔들그래프20일", "8040" },
        { "캔들그래프60일", "8046" },
        { "캔들그래프240일", "8056" },
        { "호가창", "8003" },
        { "거래원", "5037" },
        { "3열판", "5016" },
        { "미국증시지도", "5068" },
        { "유럽증시지도", "5070" },
        { "아시아증시지도", "5072" },
        { "글로벌증시지도", "8067" },
        { "미국업종", "5078" },
        { "섹터지수", "5078" },
        { "이미지그래프", "6001" },
        { "주체별매매동향", "5082" },
        { "매매동향표그래프", "5023" },
        { "개인매매동향", "5083" },
        { "기관매매동향", "5083" },
        { "외국인매매동향", "5083" },
        { "매매동향라인그래프", "5084" },
        { "매매동향막대그래프", "5024" },
        { "기관순매수현황", "6067" },
        { "프로그램매매", "5085" },
        { "순매도상위", "5025" },
        { "코스피네모그래프", "8001" },
        { "코스닥네모그래프", "8002" },
        { "5단표그래프", "5074" },
        { "6종목현재가", "5077" },
        { "12종목현재가", "5088" },
        { "지수선물2열판", "5032" },
        { "종목2열판", "8032" },
        { "코스피업종2열판", "8018" },
        { "코스닥업종2열판", "8032" },
        { "라인그래프", "50860" }
    };
}
