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;
using HBLConsole.Business;

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);
            SystemHelper.GetInstance.AutoStart(false);
            SystemHelper.GetInstance.CreateDesktopShortcut();
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            MainConsole.Main.GetInstance.DataInit();
            ThreadOperate.GetInstance.Start(new Action(() => { SqlHelper.GetInstance.GetData(); }), "GetSqliteData");
            mainView.Show();
            if (Json<MorkOrderPushPar>.Data.morkOrderPushes.Count > 0)
            {
                listDialogView = new ListDialogView();
                listDialogView.ShowDialog();
            }
            MainConsole.Main.GetInstance.BusinessInit();
        }

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

        protected override void OnExit(ExitEventArgs e)
        {
            //IotReport.GetInstance.Close();
            base.OnExit(e);
            SqlHelper.GetInstance.Save();
            MainConsole.Main.GetInstance.DataSave();
            ThreadOperate.GetInstance.Dispose();
        }
    }
}
