using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using StammGenerator.HostBuilders;
using StammGenerator.ViewModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using CodeMeter;
using SewerStammGen.Shared;
using System.Threading;
using System.Windows.Threading;

namespace StammGenerator
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private readonly IHost _host;
        public App()
        {
            new Mutex(initiallyOwned: true, "Stammdatengenerator", out bool result);
            if(!result)
            {
                MessageBox.Show("Bitte nur 1 Instanz der Software Starten!","Fehler", MessageBoxButton.OK, MessageBoxImage.Error);
                Environment.Exit(0);
            }

            using (WWRuntime wWRuntime = new WWRuntime(21))
            {
                if(wWRuntime.CheckDongleVorhanden())
                {
                    wWRuntime.CleanDongle();
                    _host = CreateHostBuilder().Build();
                }
                else
                {
                    MessageBox.Show("Kein Dongle gefunden");
                    Environment.Exit(0);
                }
            }
                
        }

        static IHostBuilder CreateHostBuilder(string[]? args = null)
        {
            return Host.CreateDefaultBuilder(args)
                .AddConfiguration()
                .AddServices()
                .AddViewModels()
                .AddStores();
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            if (_host == null) return;

            Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            _host.Start();

            MainWindow? window = new MainWindow() { DataContext = _host.Services.GetRequiredService<MainWindowViewModel>() };
            window.Show();

            base.OnStartup(e);
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            try
            {
                Exception ex = (Exception)e.ExceptionObject;
                string text = "Ein Software fehler ist Aufgetreten. Bitte kontaktiere uns mit folgende Information:\n\n";
                MessageBox.Show(text + " " + ex.Message + "\n\n" + ex.StackTrace);
            }
            catch (Exception ex2)
            {
                MessageBox.Show("Fatal Non-UI error", ex2.Message, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void Current_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}
