using DbCommon.BusinessCore.BaseCore;
using ProjectManagementSystem.Common.Config;
using ProjectManagementSystem.Common.Core;
using ProjectManagementSystem.Common.Logger;
using ProjectManagementSystem.Config;
using ProjectManagementSystem.Dispatch;
using ProjectManagementSystem.DispatchPack;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.Enterance
{
    /// <summary>
    /// 磁导航调度入口
    /// </summary>
    public class DispatchSystem
    {
        public DispatchSystem()
        {
            Init();
        }

        public void Init()
        {
            CConfiManager.Init();

            if (ExcelConfig.Instance == null)
            {
                string logContext = "Excel配置加载失败，启动失败";
                KillCurrentProcess(logContext);
                return;
            }

            if (RouteCalled.Instance == null)
            {
                string logContext = "初始化RouteCalled失败，启动失败";
                KillCurrentProcess(logContext);
                return;
            }

            if (DeviceControl.Instance == null)
            {
                string logContext = "初始化DeviceControl失败，启动失败";
                KillCurrentProcess(logContext);
                return;
            }

            //if (SimpleStep.Instance == null)
            //{
            //    string logContext = "初始化SimpleStep失败，启动失败";
            //    KillCurrentProcess(logContext);
            //    return;
            //}

            PackEngine.Instance.Start();

        }

        private void KillCurrentProcess(string logContext)
        {
            System.Windows.Forms.MessageBox.Show(logContext);
            System.Windows.Forms.Application.Exit();

            Process[] process = Process.GetProcesses();
            Process currentProcess = Process.GetCurrentProcess();
            for (int idx = 0; idx < process.Count(); idx++)
            {
                if (process[idx].ProcessName == currentProcess.ProcessName)
                {
                    process[idx].Kill();
                }
            }
            return;
        }
    }
}
