namespace MBN_STOCK_WEBVIEW.LegacyWeb.Tests;

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

    [Fact]
    public void Restored_large_lists_share_one_roving_tab_contract()
    {
        foreach (var selector in new[]
                 {
                     "button[data-comparison-selection-id]",
                     "button[data-comparison-target-id]",
                     "button[data-comparison-pair-id]",
                     "button[data-theme-result-id]",
                     "button[data-expert-selection-id]",
                     "button[data-halt-selection-id]",
                     "button[data-manual-result-id]",
                     "button[data-manual-stock-result-id]",
                     "button[data-manual-vi-result-id]",
                     "button[data-named-playlist-definition-id]",
                     "button[data-operator-catalog-result-id]",
                     "button[data-operator-catalog-stock-result-id]"
                 })
        {
            Assert.Contains(selector, App,
                StringComparison.Ordinal);
        }

        Assert.Contains("function configureRovingList", App, StringComparison.Ordinal);
        Assert.Contains("row.tabIndex = row === tabStop ? 0 : -1;", App,
            StringComparison.Ordinal);
        Assert.Contains("list.tabIndex = tabStop ? -1 : 0;", App,
            StringComparison.Ordinal);
        Assert.Contains("settings.rowRole || \"option\"", App,
            StringComparison.Ordinal);
        Assert.Contains("settings.listRole || \"listbox\"", App,
            StringComparison.Ordinal);
        Assert.Contains("configureTreeRovingLists", App, StringComparison.Ordinal);
        Assert.Contains("listRole: \"tree\"", App, StringComparison.Ordinal);
        Assert.Contains("rowRole: \"treeitem\"", App, StringComparison.Ordinal);
        Assert.Contains("activation: \"focus\"", App, StringComparison.Ordinal);
    }

    [Fact]
    public void Roving_navigation_is_IME_safe_and_sends_only_closed_selection_intents()
    {
        var start = App.IndexOf("function activateRovingListRow", StringComparison.Ordinal);
        var end = App.IndexOf("document.addEventListener(\"focusin\"", start,
            StringComparison.Ordinal);
        Assert.True(start >= 0 && end > start);
        var activation = App[start..end];

        foreach (var intent in new[]
                 {
                     "select-comparison-domestic",
                     "select-comparison-world",
                     "select-comparison-fixed",
                     "select-comparison-pair",
                     "select-expert",
                     "select-trading-halt",
                     "load-manual-financial",
                     "select-manual-vi-result"
                 })
        {
            Assert.Contains($"\"{intent}\"", activation, StringComparison.Ordinal);
        }

        Assert.DoesNotContain("choose-comparison", activation, StringComparison.Ordinal);
        Assert.DoesNotContain("activate-", activation, StringComparison.Ordinal);
        Assert.Contains("isRovingListNavigationKey(event.key)", App,
            StringComparison.Ordinal);
        Assert.Contains("!isImeComposing(event)", App, StringComparison.Ordinal);
        Assert.Contains("event.key === \"ArrowUp\"", App, StringComparison.Ordinal);
        Assert.Contains("event.key === \"ArrowDown\"", App, StringComparison.Ordinal);
        Assert.Contains("event.key === \"Home\"", App, StringComparison.Ordinal);
        Assert.Contains("event.key === \"End\"", App, StringComparison.Ordinal);
        Assert.Contains("key === \"PageUp\"", App, StringComparison.Ordinal);
        Assert.Contains("key === \"PageDown\"", App, StringComparison.Ordinal);
        Assert.Contains("rovingListPageSize", App, StringComparison.Ordinal);
    }

    private static string FindRepositoryRoot()
    {
        var directory = new DirectoryInfo(AppContext.BaseDirectory);
        while (directory is not null)
        {
            if (File.Exists(Path.Combine(directory.FullName, "MBN_STOCK_WEBVIEW.sln")))
            {
                return directory.FullName;
            }

            directory = directory.Parent;
        }

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