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;
using HBLConsole.AdvertiseWindow;
using System.Windows.Forms;
using System.Drawing;

//问题.net core项目中引用 using System.Windows.Forms; 失败
//解决方案：在 csproj 文件的 PropertyGroup里面添加  <UseWindowsForms>true</UseWindowsForms>

namespace HBLConsole
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : System.Windows.Application
    {
        MainView mainView;
        MORKD_Window_Left left;
        ListDialogView listDialogView;
        MORKD_Window_Right right;
        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();
            ThreadManage.GetInstance.Start(new Action(() => { SqlHelper.GetInstance.GetData(); }), "GetSqliteData");
            mainView = new MainView();
            mainView.Show();
            SplitScreenDisplay();
            NoCompleteOrderInit();
            MainConsole.Main.GetInstance.BusinessSimTest();
        }

        /// <summary>
        /// 分屏显示
        /// </summary>
        private void SplitScreenDisplay()
        {
            if (Screen.AllScreens.Length == 3)
            {
                left = new MORKD_Window_Left();
                right = new MORKD_Window_Right();

                Screen[] screen = Screen.AllScreens;
                Rectangle[] rectangle = new Rectangle[screen.Length];
                Window[] windows = new Window[screen.Length];
                windows[0] = mainView;
                windows[1] = left;
                windows[2] = right;
                for (int i = 0; i < screen.Length; i++)
                {
                    rectangle[i] = screen[i].WorkingArea;
                    windows[i].Height = rectangle[i].Height;
                    windows[i].Width = rectangle[i].Width;
                    windows[i].Top = rectangle[i].Top;
                    windows[i].Left = rectangle[i].Left;
                }

                left.Show();
                right.Show();
                windows[1].Owner = windows[0];
                windows[2].Owner = windows[0];
            }
        }

        /// <summary>
        /// 未完成订单初始化
        /// </summary>
        private void NoCompleteOrderInit()
        {
            if (Json<KeepDataBase>.Data.orderLists.Count > 0)
            {
                listDialogView = new ListDialogView();
                listDialogView.ShowDialog();
            }
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var ErroLog = e.ExceptionObject.ToString();
            TextHelper.GetInstance.WriteTextInfo(ErroLog, "ErroLog");
            SqlHelper.GetInstance.Save();
            MainConsole.Main.GetInstance.DataSave();

            // Process.Start($"{AppDomain.CurrentDomain.BaseDirectory}{AppDomain.CurrentDomain.FriendlyName}.exe");
        }

        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
            IotReport.Close();
            SqlHelper.GetInstance.Save();
            MainConsole.Main.GetInstance.DataSave();
            ThreadManage.GetInstance.Dispose();
            System.Environment.Exit(0);

        }
    }
}
