using Microsoft.UI.Xaml;
using Microsoft.Windows.Globalization;
using Sonex.Client.AppWindows;
using Sonex.Client.Dialogs;
using Logger = Sonex.Client.Helpers.Logger;
using LogerSender = Sonex.Client.Helpers.LogerSender;
using Sonex.Data.Database;
using System;
using System.Globalization;
using System.Threading.Tasks;
using Sonex.Client.Helpers;

namespace Sonex.Client
{
    public partial class App : Application
    {
        private LoginWindow? _LoginWindow;
        private MainWindow? _MainWindow;

        public App()
        {
            // Najpierw probujemy wyslac zalegle logi. Ta operacja niczego nie loguje i niczego nie blokuje.
            LogerSender.SendLogs();
            Logger.Initialize();

            // Rejestrujemy globalne handlery jak najwcześniej, żeby łapać także wyjątki ze startu aplikacji.
            AppDomain.CurrentDomain.UnhandledException += Logger.OnException;
            //AppDomain.CurrentDomain.FirstChanceException += Logger.OnException;
            TaskScheduler.UnobservedTaskException += Logger.OnException;

            try
            {
                //var culture = new CultureInfo("pl-PL");
                GlobalValues.WebApiBaseUrl = Config.GetStringValue("WEB_API_BASE_URL", "http://localhost:5000").Trim('/');

                //CultureInfo.CurrentCulture = culture;
                //CultureInfo.CurrentUICulture = culture;
                //CultureInfo.DefaultThreadCurrentCulture = culture;
                //CultureInfo.DefaultThreadCurrentUICulture = culture;

                //ApplicationLanguages.PrimaryLanguageOverride = "pl-PL";
                InitializeComponent();
                DB.SetClientSessionInfo(AppSessionInfo.CreateDbClientSessionInfo());

                // WinUI ma własny globalny event dla wyjątków wychodzących z warstwy XAML/UI.
                UnhandledException += Logger.OnException;
            }
            catch (Exception e)
            {
                Logger.Fatal(e, source: nameof(App), operation: "App bootstrap", isUnhandled: false);
                Logger.FlushAndClose();
                throw;
            }
        }

        protected override void OnLaunched(LaunchActivatedEventArgs args)
        {
            try
            {
                OpenLoginWindow();
            }
            catch (Exception ex)
            {
                Logger.Fatal(ex, source: nameof(App), operation: nameof(OnLaunched), isUnhandled: false);
                Logger.FlushAndClose();
                throw;
            }
        }

        public void OpenLoginWindow()
        {
            _LoginWindow = new LoginWindow();
            _LoginWindow.PrepareBeforeShow();
            _LoginWindow.HideNative();
            _LoginWindow.Activate();

        }

        public void OpenMainWindow()
        {
            _MainWindow = new MainWindow();
            _MainWindow.Activate();
            Sonex.Client.Dialogs.AppWindowsHelper.BringToFront(_MainWindow);

            _LoginWindow?.Close();
            _LoginWindow = null;
        }

    }
}
