using System.Text.RegularExpressions;

namespace MBN_STOCK_WEBVIEW.LegacyWeb.Tests;

public sealed class LegacySceneActionClickParityTests
{
    private static readonly string App = File.ReadAllText(Path.Combine(
        FindRepositoryRoot(),
        "src",
        "MBN_STOCK_WEBVIEW.LegacyParityApp",
        "Web",
        "app.js"));

    [Fact]
    public void ThemeExpertAndTradingHaltActions_AreSingleClickOnly()
    {
        // Original evidence: UC4.Designer.cs btn1..btn4, UC6.Designer.cs
        // btlistadd, and UC7.Designer.cs btn1/btn2 are wired to Click.
        var doubleClickDelegate = ExtractDelegate("dblclick", "click");
        var clickDelegate = ExtractDelegate("click", "change");

        foreach (var intent in new[]
                 {
                     "activate-theme-action",
                     "activate-expert-action",
                     "activate-trading-halt-action"
                 })
        {
            Assert.Contains($"send(\"{intent}\"", clickDelegate, StringComparison.Ordinal);
            Assert.DoesNotContain($"send(\"{intent}\"", doubleClickDelegate,
                StringComparison.Ordinal);
            Assert.Single(Regex.Matches(App, Regex.Escape($"send(\"{intent}\"")).Cast<Match>());
        }
    }

    [Fact]
    public void ExistingSelectionAndTrueDoubleClickBehavior_RemainsSeparated()
    {
        var doubleClickDelegate = ExtractDelegate("dblclick", "click");
        var clickDelegate = ExtractDelegate("click", "change");

        Assert.Contains("activateIndustryRow(industryRow)", doubleClickDelegate,
            StringComparison.Ordinal);
        Assert.Contains("send(\"double-click-industry\"", App, StringComparison.Ordinal);
        Assert.Contains("send(\"activate-comparison-action\"", doubleClickDelegate,
            StringComparison.Ordinal);
        Assert.Contains("send(\"select-theme\"", clickDelegate, StringComparison.Ordinal);
        Assert.Contains("send(\"select-expert\"", clickDelegate, StringComparison.Ordinal);
        Assert.Contains("send(\"select-trading-halt\"", clickDelegate,
            StringComparison.Ordinal);
    }

    private static string ExtractDelegate(string eventName, string nextEventName)
    {
        var startToken = $"categoryTree.addEventListener(\"{eventName}\"";
        var endToken = $"categoryTree.addEventListener(\"{nextEventName}\"";
        var start = App.IndexOf(startToken, StringComparison.Ordinal);
        var end = App.IndexOf(endToken, start + startToken.Length, StringComparison.Ordinal);
        Assert.True(start >= 0 && end > start);
        return App[start..end];
    }

    private static string FindRepositoryRoot()
    {
        var current = new DirectoryInfo(AppContext.BaseDirectory);
        while (current is not null)
        {
            if (Directory.Exists(Path.Combine(current.FullName, "src")) &&
                Directory.Exists(Path.Combine(current.FullName, "tests")))
            {
                return current.FullName;
            }

            current = current.Parent;
        }

        throw new DirectoryNotFoundException("Repository root was not found.");
    }
}
