using System.Windows;
using CyberX8_Simulator.Instances;
using MECF.Framework.UI.Core.Applications;
using CyberX8_Core;
using System;

namespace CyberX8_Simulator
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            base.OnStartup(e);
            SystemConfig.Instance.Initialize();
            //SimulatorJetChamber.CurrentPMAChamber = (JetChamber)SystemConfig.Instance.GetValue<int>($"PMA.ChamberType");
            //SimulatorJetChamber.CurrentPMBChamber = (JetChamber)SystemConfig.Instance.GetValue<int>($"PMB.ChamberType");
            //SimulatorJetChamber.CurrentPMCChamber = (JetChamber)SystemConfig.Instance.GetValue<int>($"PMC.ChamberType");
            //SimulatorJetChamber.CurrentPMDChamber = (JetChamber)SystemConfig.Instance.GetValue<int>($"PMD.ChamberType");


            UiApplication.Instance.Initialize(new UiInstance());

            SimulatorSystem.Instance.Initialize();

        }
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            ShowMessageDialog((Exception)e.ExceptionObject);
        }

        static void ShowMessageDialog(Exception ex)
        {
            string message = string.Format(" UI unknown exception:{0},\n", $"{ex.StackTrace} {DateTime.Now}");
            System.Windows.MessageBox.Show(message + ex.Message, "Unexpected exception", System.Windows.MessageBoxButton.YesNo,
                System.Windows.MessageBoxImage.Exclamation,
                System.Windows.MessageBoxResult.No,
                System.Windows.MessageBoxOptions.DefaultDesktopOnly);
            //Environment.Exit(0);
        }

        protected override void OnExit(ExitEventArgs e)
        {
            UiApplication.Instance.Terminate();

            SimulatorSystem.Instance.Terminate();

            base.OnExit(e);
        }
    }
}