using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.UI.Xaml.Shapes;
using Sonex.DBWrapper;
using Sonex.Windows;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace Sonex
{
    /// <summary>
    /// Provides application-specific behavior to supplement the default Application class.
    /// </summary>
    public partial class App : Application
    {
        private Window _MainWindow;
        private Window _LoginWindow;

        /// <summary>
        /// Initializes the singleton application object.  This is the first line of authored code
        /// executed, and as such is the logical equivalent of main() or WinMain().
        /// </summary>
        public App()
        {
            Console.WriteLine("Start APP");

            try
            {
                InitializeComponent();
                DB.InitPostgreSQL("Host=localhost;Port=5432;Username=postgres;Password=Xenos2015;Database=postgres;Pooling=true;Minimum Pool Size=0;Maximum Pool Size=100;Timeout=15;Command Timeout=30;Application Name=Sonex.WinUI3;");
                AppDomain.CurrentDomain.UnhandledException += (_, e) =>
                    System.IO.File.AppendAllText("startup_crash.log", e.ExceptionObject?.ToString() + Environment.NewLine);

                TaskScheduler.UnobservedTaskException += (_, e) =>
                    System.IO.File.AppendAllText("startup_crash.log", e.Exception + Environment.NewLine);

                UnhandledException += (_, e) =>
                    System.IO.File.AppendAllText("startup_crash.log", e.Exception + Environment.NewLine);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

        }

        /// <summary>
        /// Invoked when the application is launched.
        /// </summary>
        /// <param name="args">Details about the launch request and process.</param>
        protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs args)
        {
            OpenLoginWindow();
        }
        public void OpenLoginWindow()
        {
            _MainWindow?.Close();
            _LoginWindow = new LoginWindow();
            _LoginWindow.Activate();
        }
        public void OpenMainWindow()
        {
            _LoginWindow?.Close();
            _MainWindow = new MainWindow();
            _MainWindow.Activate();
        }


    }
}
