namespace MBN_STOCK_WEBVIEW.LegacyWeb.Tests;

public sealed class LegacyWindowSizingNativeContractTests
{
    private static readonly string Window = File.ReadAllText(Path.Combine(
        FindRepositoryRoot(),
        "src",
        "MBN_STOCK_WEBVIEW.LegacyParityApp",
        "MainWindow.xaml.cs"));

    [Fact]
    public void MainWindowClampsRestoreBoundsAndUsesWorkAreaAwareSystemMaximize()
    {
        var configureWindow = Slice(
            Window,
            "private void ConfigureWindow()",
            "private async Task InitializeWebViewAsync()");

        var displayArea = RequiredIndex(
            configureWindow,
            "DisplayArea.GetFromWindowId(windowId, DisplayAreaFallback.Primary)");
        var workArea = RequiredIndex(
            configureWindow,
            "var workArea = displayArea.WorkArea;");
        var resizeClient = RequiredIndex(
            configureWindow,
            "appWindow.ResizeClient(new SizeInt32(1905, 1015));");
        var readOuterSize = RequiredIndex(
            configureWindow,
            "var requestedWindowSize = appWindow.Size;");
        var moveAndResize = RequiredIndex(
            configureWindow,
            "appWindow.MoveAndResize(");
        var workAreaX = RequiredIndex(configureWindow, "workArea.X,");
        var workAreaY = RequiredIndex(configureWindow, "workArea.Y,");
        var allowSystemMaximize = RequiredIndex(
            configureWindow,
            "presenter.IsMaximizable = true;");
        var maximize = RequiredIndex(
            configureWindow,
            "presenter.Maximize();");
        Assert.True(displayArea < workArea);
        Assert.True(workArea < resizeClient);
        Assert.True(resizeClient < readOuterSize);
        Assert.True(readOuterSize < moveAndResize);
        Assert.True(moveAndResize < workAreaX);
        Assert.True(workAreaX < workAreaY);
        Assert.True(moveAndResize < allowSystemMaximize);
        Assert.True(allowSystemMaximize < maximize);
        Assert.DoesNotContain(
            "presenter.IsMaximizable = false;",
            configureWindow,
            StringComparison.Ordinal);
        Assert.Contains(
            "presenter.IsResizable = false;",
            configureWindow,
            StringComparison.Ordinal);
        Assert.Contains(
            "var requestedWindowSize = appWindow.Size;",
            configureWindow,
            StringComparison.Ordinal);
        Assert.Contains(
            "Math.Min(requestedWindowSize.Width, workArea.Width)",
            configureWindow,
            StringComparison.Ordinal);
        Assert.Contains(
            "Math.Min(requestedWindowSize.Height, workArea.Height)",
            configureWindow,
            StringComparison.Ordinal);
        Assert.DoesNotContain(
            "appWindow.Move(new PointInt32(",
            configureWindow,
            StringComparison.Ordinal);
    }

    private static int RequiredIndex(string value, string needle)
    {
        var index = value.IndexOf(needle, StringComparison.Ordinal);
        Assert.True(index >= 0, $"Required marker was not found: {needle}");
        return index;
    }

    private static string Slice(string value, string start, string end)
    {
        var startIndex = value.IndexOf(start, StringComparison.Ordinal);
        var endIndex = value.IndexOf(
            end,
            startIndex + start.Length,
            StringComparison.Ordinal);

        Assert.True(startIndex >= 0, $"Required marker was not found: {start}");
        Assert.True(endIndex >= 0, $"Required marker was not found: {end}");
        return value[startIndex..endIndex];
    }

    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("Could not find the repository root.");
    }
}
