using Microsoft.Web.WebView2.Core;
using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;

namespace ReFoneAtelierOS;

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += async (_, _) => await StartAsync();
    }

    private async Task StartAsync()
    {
        try
        {
            await Browser.EnsureCoreWebView2Async();
            string? indexPath = ResolveIndexPath();

            if (indexPath is null)
            {
                Browser.NavigateToString(BuildErrorPage("Impossible de trouver WEB/index.html a cote du projet Atelier OS."));
                return;
            }

            Browser.CoreWebView2.Settings.IsStatusBarEnabled = false;
            Browser.Source = new Uri(indexPath);
        }
        catch (Exception ex)
        {
            Browser.NavigateToString(BuildErrorPage(ex.Message));
        }
    }

    private static string? ResolveIndexPath()
    {
        string current = AppContext.BaseDirectory;

        for (int level = 0; level < 8; level++)
        {
            string candidate = Path.Combine(current, "WEB", "index.html");
            if (File.Exists(candidate))
            {
                return new Uri(candidate).AbsoluteUri;
            }

            DirectoryInfo? parent = Directory.GetParent(current);
            if (parent is null)
            {
                break;
            }

            current = parent.FullName;
        }

        return null;
    }

    private static string BuildErrorPage(string message)
    {
        return $"""
        <!DOCTYPE html>
        <html lang="fr">
        <head>
          <meta charset="utf-8">
          <title>ReFone Atelier OS</title>
          <style>
            body {{
              margin: 0;
              min-height: 100vh;
              display: grid;
              place-items: center;
              background: radial-gradient(circle at top left, rgba(61,124,255,.22), transparent 35%), #070b16;
              color: white;
              font-family: "Segoe UI", sans-serif;
            }}
            article {{
              width: min(560px, calc(100vw - 48px));
              padding: 28px;
              border-radius: 28px;
              background: rgba(255,255,255,.06);
              border: 1px solid rgba(255,255,255,.08);
              box-shadow: 0 24px 60px rgba(0,0,0,.28);
            }}
            h1 {{ margin: 0 0 10px; font-size: 32px; }}
            p {{ margin: 0; line-height: 1.65; color: rgba(255,255,255,.74); }}
          </style>
        </head>
        <body>
          <article>
            <h1>Atelier OS introuvable</h1>
            <p>{System.Net.WebUtility.HtmlEncode(message)}</p>
          </article>
        </body>
        </html>
        """;
    }
}
