using BPA.Message;
using BPA.Message.Enum;
using BPASmartClient.Helper;
using BPASmartClient.Message;
using BPASmartClient.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace BPASmartClient
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : System.Windows.Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            SystemHelper.GetInstance.CreateDesktopShortcut();
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            DataRead();
            MainWindow mainView = new MainWindow();
            mainView.Show();
            SplitScreenDisplay();
            NoCompleteOrderInit();
        }

        /// <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)
            {
                OrderListDialogView listDialogView = new OrderListDialogView();
                listDialogView.ShowDialog();
            }
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var ErroLog = e.ExceptionObject.ToString();
            MessageLog.GetInstance.Show(ErroLog);
            TextHelper.GetInstance.WriteTextInfo(ErroLog, "ErroLog");
            DataSave();
            //SqlHelper.GetInstance.Save();
            //MainConsole.Main.GetInstance.DataSave();
            //IotReport.GetInstance.HttpAddLog(new BPA.Message.API请求.LogTable
            //{
            //    ClientId = InternetInfo.ClientId.ToString(),
            //    LogTime = DateTime.Now,
            //    LogType = "1",
            //    LogMessage = ErroLog,
            //    LogVla = "异常",
            //});
            // Process.Start($"{AppDomain.CurrentDomain.BaseDirectory}{AppDomain.CurrentDomain.FriendlyName}.exe");
        }

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

        }

        /// <summary>
        /// 输入保存
        /// </summary>
        private void DataSave()
        {
            Json<KeepDataBase>.Save();
            //Sqlite<Alarm>.GetInstance.Save();
        }

        /// <summary>
        /// 输入读取
        /// </summary>
        private void DataRead()
        {
            Json<KeepDataBase>.Read();
            Task.Run(new Action(() => { Sqlite<Alarm>.GetInstance.GetData(); }));
        }



    }
}
