using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using HBLConsole.DialogWindow.View;
using HBLConsole.View;
using System.Diagnostics;
using HBLConsole.Service;
using HBLConsole.Model;
using HBLConsole.MainConsole;
using HBLConsole.GVL;
using BPA.Message.Enum;

namespace HBLConsole
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        MainView mainView = new MainView();
        ListDialogView listDialogView;
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            GeneralConfig.GetInstance.DeviceType = DeviceClientType.MORKS;
            SystemHelper.GetInstance.AutoStart(false);
            SystemHelper.GetInstance.CreateDesktopShortcut();
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            MainConsole.Main.GetInstance.DataInit();
            mainView.Show();
            if (Json<MorkOrderPushPar>.GetInstance.Base.morkOrderPushes.Count > 0)
            {
                listDialogView = new ListDialogView();
                listDialogView.ShowDialog();
            }
            MainConsole.Main.GetInstance.BusinessInit();
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            MainConsole.Main.GetInstance.DataSave();
            Process.Start($"{AppDomain.CurrentDomain.BaseDirectory}{AppDomain.CurrentDomain.FriendlyName}.exe");
        }

        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
            MainConsole.Main.GetInstance.DataSave();
        }
    }
}
