using System.Windows;
using Noctalk_client.Splash_screen;

namespace Noctalk_client;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        ShutdownMode = ShutdownMode.OnExplicitShutdown;
        _ = App__startup_async();
    }

    private async Task App__startup_async()
    {
        bool flagReviewAllSplashPlaces = App__splash_review_mode__enabled();
        SplashWindow splashWindow = new(flagReviewAllSplashPlaces);
        splashWindow.Show();

        if (flagReviewAllSplashPlaces)
        {
            await splashWindow.Wait_review_duration_async();
            await splashWindow.Fade_out_async();
            splashWindow.Close();
            Shutdown();
            return;
        }

        MainWindow mainWindow = new()
        {
            Visibility = Visibility.Hidden,
            ShowInTaskbar = false
        };

        Task minimumSplashTask = splashWindow.Wait_minimum_duration_async();
        Task initializeTask = mainWindow.Initialize_client_async();

        try
        {
            await Task.WhenAll(minimumSplashTask, initializeTask);
        }
        catch
        {
            await minimumSplashTask;
        }

        await splashWindow.Fade_out_async();
        MainWindow = mainWindow;
        mainWindow.ShowInTaskbar = true;
        mainWindow.Visibility = Visibility.Visible;
        mainWindow.Show();
        splashWindow.Close();
        ShutdownMode = ShutdownMode.OnMainWindowClose;
    }

    private static bool App__splash_review_mode__enabled()
    {
        return false;
    }
}

