namespace FrymasterBadgeApp;

public partial class App : Application
{
    private readonly IServiceProvider _serviceProvider;
    private const string ThemePrefKey = "AppTheme"; // Must match SettingsPage

    public App(IServiceProvider serviceProvider)
    {
        AppLogger.Info("App: Constructor - Internal setup beginning");
        try
        {
            InitializeComponent();
            ApplySavedTheme(); // Set the theme before resolving Shell
            AppLogger.Info("App: InitializeComponent and Theme application successful");
        }
        catch (Exception ex)
        {
            AppLogger.Error("App: FATAL XAML ERROR", ex);
            throw;
        }
        _serviceProvider = serviceProvider;
    }

/// <summary>
/// Applies the saved theme preference to the app.
/// If the preference could not be applied, a warning is logged.
/// </summary>
    private void ApplySavedTheme()
    {
        try
        {
            var pref = Microsoft.Maui.Storage.Preferences.Default.Get(ThemePrefKey, "System");
            AppLogger.Info($"App: Applying saved theme preference: {pref}");

            Application.Current.UserAppTheme = pref switch
            {
                "Light" => AppTheme.Light,
                "Dark" => AppTheme.Dark,
                _ => AppTheme.Unspecified
            };
        }
        catch (Exception ex)
        {
            AppLogger.Warn($"App: Failed to apply theme preference: {ex.Message}");
        }
    }

/// <summary>
/// Creates a new instance of the required AppShell service and 
/// opens a new window with the specified content.
/// </summary>
    protected override Window CreateWindow(IActivationState? activationState)
    {
        AppLogger.Info("App: CreateWindow entered");
        try
        {
            var shell = _serviceProvider.GetRequiredService<AppShell>();
            AppLogger.Info("App: AppShell resolved successfully");
            return new Window(shell);
        }
        catch (Exception ex)
        {
            AppLogger.Error("CRITICAL STARTUP ERROR: Dependency Resolution Failed", ex);

            return new Window(
                new ContentPage
                {
                    Content = new VerticalStackLayout
                    {
                        VerticalOptions = LayoutOptions.Center,
                        Padding = 20,
                        Children =
                        {
                            new Label
                            {
                                Text = "Startup Failed",
                                FontAttributes = FontAttributes.Bold,
                                HorizontalTextAlignment = TextAlignment.Center,
                                FontSize = 20
                            },
                            new Label
                            {
                                Text = ex.Message,
                                HorizontalTextAlignment = TextAlignment.Center,
                                TextColor = Colors.Red,
                            },
                            new Label
                            {
                                Text = "Check log for details.",
                                HorizontalTextAlignment = TextAlignment.Center,
                            },
                        }
                    }
                }
            );
        }
    }
}