using Jc.SnowId;
using Khd.Core.Domain.Models;
using Khd.Core.EntityFramework;
using Khd.Core.Library;
using Khd.Core.Wcs.Global;
using Khd.Core.Wcs.Wcs;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System.Data;
using Z.EntityFramework.Plus;

namespace Khd.Core.Wcs
{
    public class MainCentralControl
    {
        public static object QingKongDianWei = 0;
        public static object WcsChuLiWanCheng = 1;
        public static object WcsMoRenQuXiang = 1;

        private readonly IHost _host;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="host"></param>
        public MainCentralControl(IHost host)
        {
            this._host = host;
        }

        /// <summary>
        /// 启动程序
        /// </summary>
        public void Start()
        {
            LoggerUtils logger = new LoggerUtils();
            using var scope = _host.Services.CreateScope();
            using var dbContext = scope.ServiceProvider.GetRequiredService<DefaultDbContext>();
            try
            {
                //dbContext.WmsBaseLocation.Update(t => new Dictionary<string, WmsBaseLocation>() { { "1", new WmsBaseLocation { locationStatus="1" } } });

                StaticData.BasePlcpointList = dbContext.BasePlcpoint.Where(t => t.isDelete == 0).ToList();//设备交互用
                StaticData.basePlcs = dbContext.BasePlc.ToList();//陈工Agv和设备信息推送
                StaticData.BaseEquip = dbContext.BaseEquip.ToList();//设备信息
                StaticData.BaseDictionary = dbContext.BaseDictionary.ToList();//字典表，Agv任务模板
                StaticData.DmsBaseAlarmRuleList = dbContext.DmsBaseAlarmRule.ToList();
               
                foreach (var plcConfig in StaticData.PlcConfigs)
                {
                    if (!StaticData.PlcDic.Any(t => t.Key == plcConfig.Code))
                    {
                        Plc.S7.Plc plc;
                        plc = new Plc.S7.Plc(plcConfig.CpuType, plcConfig.IP, plcConfig.Port, plcConfig.Rack, plcConfig.Slot);
                        try
                        {
                            plc.Open();
                            Console.WriteLine(DateTime.Now + ":连接PLC：" + plcConfig.IP);
                        }
                        catch
                        {
                            Console.WriteLine(DateTime.Now + ":连接Plc" + plcConfig.IP + "失败~~~~~~~");
                        }
                        StaticData.PlcDic.TryAdd(plcConfig.Code, plc);
                    }
                }

                //创建定时器
                SystemTimer systemTimer = new SystemTimer(_host);
                systemTimer.Start();

                //创建任务
                CreateTaskByRecord createTaskByRecord = new(_host);
                createTaskByRecord.StartPoint();

                //一楼提升机以及接驳位
                FirstFloor firstFloor = new(_host);
                firstFloor.StartPoint();

                //二层接驳位
                SecondFloorPoint secondFloorPoint = new(_host, 2);
                secondFloorPoint.StartPoint();

                ////二层AGV
                SecondFloorAGV secondFloorAGV = new(_host, 2);
                secondFloorAGV.StartPoint();

                //二楼线体
                SecondFloorLine secondFloorLine = new(_host, 2);
                secondFloorLine.StartPoint();

                //三层接驳位
                ThirdFloorPoint thirdFloorPoint = new(_host, 3);
                thirdFloorPoint.StartPoint();

               // 三层AGV
               ThirdFloorAGV thirdFloorAGV = new(_host, 3);
                thirdFloorAGV.StartPoint();

                //四楼接驳位
                FourthFloorPoint fourthFloorPoint = new FourthFloorPoint(_host, 4);
                fourthFloorPoint.StartPoint();

                //五层接驳位
                FiveFloorPoint fifthFloorPoint = new(_host, 5);
                fifthFloorPoint.StartPoint();

                //五层CTU
                FiveFloorCTU fiveFloorCTU = new(_host, 5);
                fiveFloorCTU.StartPoint();

                //五层AGV
                FiveFloorAGV fifthFloorAGV = new(_host, 5);
                fifthFloorAGV.StartPoint();

                ////背负式Agv
                FiveFloorBearAgv fiveFloorBearAgv = new(_host, 5);
                fiveFloorBearAgv.StartPoint();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                logger.Error(ex.Message + "\n" + ex.StackTrace);
            }


        }

        


    }
}
