using System.IO;
using System.Windows;
using NexusShell.Views;

namespace NexusShell.Services;

internal static class DesignEvidenceCaptureRunner
{
    public static async Task RunAsync(Window owner)
    {
        var outputDir = ResolveEvidenceDirectory();

        var captures = new (string FileName, Func<Window> CreateWindow)[]
        {
            ("window-framework-files.png", () => new NexusFilesWindow
            {
                Owner = owner,
                DataContext = App.Services.FilesViewModel,
            }),
            ("window-framework-settings.png", () => new NexusSettingsWindow
            {
                Owner = owner,
                DataContext = App.Services.SettingsViewModel,
            }),
            ("window-framework-store.png", () => new NexusStoreWindow
            {
                Owner = owner,
                DataContext = App.Services.StoreViewModel,
            }),
            ("window-framework-device-manager.png", () => new NexusDeviceManagerWindow
            {
                Owner = owner,
                DataContext = App.Services.DeviceManagerViewModel,
            }),
            ("window-framework-browser.png", () => new NexusBrowserWindow
            {
                Owner = owner,
                DataContext = App.Services.BrowserViewModel,
            }),
            ("window-framework-photos.png", () => new NexusPhotosWindow
            {
                Owner = owner,
                DataContext = App.Services.PhotosViewModel,
            }),
            ("window-framework-media.png", () => new NexusMediaWindow
            {
                Owner = owner,
                DataContext = App.Services.MediaViewModel,
            }),
            ("window-framework-console.png", () =>
            {
                var window = new NexusConsoleWindow
                {
                    Owner = owner,
                    DataContext = App.Services.NexusConsoleViewModel,
                };
                window.WireEvents(App.Services.NexusConsoleViewModel);
                return window;
            }),
        };

        App.Services.SettingsViewModel.Refresh();

        foreach (var (fileName, createWindow) in captures)
        {
            var window = createWindow();
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.Show();
            window.Activate();

            await EvidenceScreenshotService.CaptureAsync(window, Path.Combine(outputDir, fileName));
            window.Close();
            await Task.Delay(200);
        }
    }

    private static string ResolveEvidenceDirectory()
    {
        var dir = new DirectoryInfo(AppContext.BaseDirectory);
        while (dir is not null)
        {
            if (Directory.Exists(Path.Combine(dir.FullName, "docs")))
            {
                var outputDir = Path.Combine(dir.FullName, "docs", "evidence", "nexus-design-system-v2");
                Directory.CreateDirectory(outputDir);
                return outputDir;
            }

            dir = dir.Parent;
        }

        var fallback = Path.Combine(AppContext.BaseDirectory, "docs", "evidence", "nexus-design-system-v2");
        Directory.CreateDirectory(fallback);
        return fallback;
    }
}
