namespace SilverOS.Welcome.App;

public partial class App : Application
{
	public App()
	{
		Diag.Log("App ctor");
		AppDomain.CurrentDomain.UnhandledException += (s, e) =>
			Diag.Log("UNHANDLED: " + (e.ExceptionObject as Exception)?.ToString());
		TaskScheduler.UnobservedTaskException += (s, e) =>
		{
			Diag.Log("UNOBSERVED TASK: " + e.Exception);
			e.SetObserved();
		};
		InitializeComponent();
	}

	protected override Window CreateWindow(IActivationState? activationState)
	{
		Diag.Log("CreateWindow");
		var window = new Window(new MainPage()) { Title = "SilverMetal Windows" };
#if WINDOWS
		// Apply kiosk chrome once the native window handler exists. Wrapped so a
		// chrome failure can NEVER break app/WebView startup (a previous version
		// threw here and left the wizard blank). HandlerChanged can fire before the
		// HWND is ready, so ApplyKioskChrome null-guards and re-applies safely.
		window.HandlerChanged += (s, e) =>
		{
			try
			{
				if (window.Handler?.PlatformView is Microsoft.UI.Xaml.Window native)
				{
					var ok = native.ApplyKioskChrome();
					Diag.Log($"ApplyKioskChrome applied={ok}");
				}
			}
			catch (Exception ex) { Diag.Log("ApplyKioskChrome FAILED: " + ex); }
		};
#endif
		return window;
	}
}
