using DbCommon.BusinessCore.BaseCore;
using ProjectManagementSystem.Common.Core;
using ProjectManagementSystem.Common.Logger;
using ProjectManagementSystem.Device.CommandCallback;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.DispatchPack.Entity
{
    public class Entity_PACK线 : EntityBase
    {
        private StationInfoManager m_stationInfoManager = new StationInfoManager();

        public override void CustomUpdate()
        {
            DateTime nowDateTime = DateTime.Now;

            var dataList = m_stationInfoManager.GetList();

            int stopCount = StationInfoList.Count(d => d.Online && d.AgvStausValue == (byte)EnumInfoAgvStatus.停止点);
            if (stopCount == StationInfoList.Count)
            {

                bool needUpdate = false;
                //工位AGV全部到位
                for (int i = 0; i < StationInfoList.Count; i++)
                {
                    var info = StationInfoList[i];
                    var data = dataList.Find(d => d.Id == info.Id);
                    if (data == null)
                    {
                        continue;
                    }

                    if (data.CountInterval > 0
                        && data.Command == 0)
                    {
                        data.Command = 10;
                        data.CountStartTime = nowDateTime;

                        info.Command = data.Command;
                        info.CountStartTime = data.CountStartTime;

                        CLog.Instance.TaskLog.WriteInfo($"放行倒计时，地标：{info.Mark}，路线：{info.ReleaseRoute}，AGV：{info.AgvId}，{info.Name}，0x{info.AgvStausValue:X2}");

                        needUpdate = true;
                    }
                }

                if(needUpdate)
                {
                    m_stationInfoManager.UpdateCountStart(StationInfoList);
                }
                
            }

            for (int i = 0; i < StationInfoList.Count; i++)
            {
                var info = StationInfoList[i];
                var data = dataList.Find(d => d.Id == info.Id);
                if (data == null)
                {
                    continue;
                }

                if (data.Command == 10)
                {
                    DateTime alarmTime = data.CountStartTime.AddSeconds(data.CountInterval - data.CountAlarm);
                    DateTime releaseTime = data.CountStartTime.AddSeconds(data.CountInterval);

                    info.CountSec = (int)(releaseTime - nowDateTime).TotalSeconds;
                    if (info.CountSec < 0) info.CountSec = 0;

                    if (nowDateTime > alarmTime)
                    {
                        //发送放行报警信息
                        DeviceControl.Instance.RailAgv_ReleaseAlarm(info.AgvId, info.CountSec);
                        CLog.Instance.TaskLog.WriteInfo($"放行报警，地标：{info.Mark}，路线：{info.ReleaseRoute}，AGV：{info.AgvId}，{info.Name}，0x{info.AgvStausValue:X2}");
                    }

                    if (nowDateTime > releaseTime)
                    {
                        data.Command = 11;
                        m_stationInfoManager.UpdateCommand(data);
                    }
                }
            }


            for (int i = 0; i < StationInfoList.Count; i++)
            {
                var info = StationInfoList[i];

                int releaseCommand = 0;
                var data = dataList.Find(d => d.Id == info.Id);
                if (data != null)
                {
                    releaseCommand = data.Command;
                    info.Command = data.Command;
                }


                if (info.Online
                    && !info.Traffic)
                {
                    switch (releaseCommand)
                    {
                        case 1:
                        case 11:
                            if (data.HasCar
                                && info.HasCar
                                && (info.AgvStausValue == (byte)EnumInfoAgvStatus.停止点
                                || info.AgvStausValue == (byte)EnumInfoAgvStatus.结束点
                                || info.AgvStausValue == (byte)EnumInfoAgvStatus.充电中)
                                && info.ReleaseCold)
                            {
                                DeviceControl.Instance.ReleaseAgvOnStop(info.Mark, info.ReleaseRoute, info.AgvId);
                                info.LastReleaseTime = DateTime.Now.Ticks;

                                bool commandChanged = releaseCommand != info.LastCommand;
                                string realeseTag = releaseCommand == 11 ? "工位预约放行" : "工位放行";

                                CLog.Instance.TaskLog.WriteInfo($"{realeseTag}{(commandChanged ? "(触发)" : "")}，地标：{info.Mark}，路线：{info.ReleaseRoute}，AGV：{info.AgvId}，{info.Name}，0x{info.AgvStausValue:X2}");
                                info.LastCommand = releaseCommand;
                            }
                            break;
                        //case (int)DBControlValues.NGRelease:
                        //    if (info.HasCar)
                        //    {
                        //        DispatcherTool.ReleaseAgvOnStop(info.Mark, info.ReleaseNGRoute);
                        //    }
                        //    break;
                        //case (int)DBControlValues.StopImmediate:
                        //    DispatcherTool.ControlAGVBy1071(info.AgvId, 1);//暂停
                        //    break;
                        //case (int)DBControlValues.StopRelease:
                        //    DispatcherTool.ControlAGVBy1071(info.AgvId, 0);//启动
                        //    break;
                        default:
                            break;
                    }
                }


                //AGV放走后，复位Command
                if (data.HasCar == false
                    && (releaseCommand == 1 || releaseCommand == 11))
                {
                    info.LastCommand = 0;
                    data.Command = 0;
                    bool result = m_stationInfoManager.UpdateCommand(data);

                    CLog.Instance.TaskLog.WriteInfo($"放行复位，地标：{info.Mark}，路线：{info.ReleaseRoute}，AGV：{info.AgvId}，{info.Name}，0x{info.AgvStausValue:X2}");
                }
            }
        }
    }
}
