using DbCommon.BusinessCore.BaseCore;
using DbCommon.Enties.DbModels;
using Pms.DataLibrary.Models;
using Pms.DataLibrary.Order;
using Pms.Models;
using ProjectManagementSystem.Common.Core;
using ProjectManagementSystem.Common.Extenions;
using ProjectManagementSystem.Common.Logger;
using ProjectManagementSystem.Common.WebApi;
using ProjectManagementSystem.Common.Models;
using ProjectManagementSystem.TaskBookEvent;
using ProjectManagementSystem.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.TaskEvent
{
    public class CTaskEvent : CTaskEventBase
    {
        public override void TaskCustomProc(List<TaskData> taskDataList)
        {
        }

        public override void TaskStateChanged(TaskData taskInfo, TaskData lastTaskInfo)
        {
        }
        public override void TaskFinished(TaskData taskInfo)
        {
            CLog.Instance.GetAgvLog(taskInfo.Carrier).WriteInfo($"{taskInfo.TaskID} 任务完成");
        }

        public override void TaskCanceled(TaskData taskInfo)
        {
            CLog.Instance.GetAgvLog(taskInfo.Carrier).WriteInfo($"{taskInfo.TaskID} 任务取消");

            RecoverLocationStatus(taskInfo);
        }

        public override void TaskTerminated(TaskData taskInfo)
        {
            CLog.Instance.GetAgvLog(taskInfo.Carrier).WriteInfo($"{taskInfo.TaskID} 任务终止");

            RecoverLocationStatus(taskInfo);
        }

        public override void TaskExceptioned(TaskData taskInfo)
        {
            CLog.Instance.GetAgvLog(taskInfo.Carrier).WriteInfo($"{taskInfo.TaskID} 任务异常");

            RecoverLocationStatus(taskInfo);
        }

        public override void TaskBecomeHistory(TaskData taskInfo)
        {
            CLog.Instance.GetAgvLog(taskInfo.Carrier).WriteInfo($"{taskInfo.TaskID} 任务已转入历史任务，任务状态：{taskInfo.TaskState}（可能原因：AGVS重启）");

            RecoverLocationStatus(taskInfo);
        }

        private void RecoverLocationStatus(TaskData taskInfo)
        {
            var posArray = taskInfo.StepList?
                .OrderBy(d => d.StepID)?
                .Select(d => d.WareHouseID)?
                .Distinct()?
                .ToList();

            if(posArray == null) return;

            for (int i = 0; i < posArray.Count; i++)
            {
                string location = posArray[i];

                var locationData = m_locationManager.GetById(location);
                if (locationData == null)
                {
                    continue;
                }

                if (locationData.Status == LocationStatus.Locked
                    || locationData.Status == LocationStatus.Exception)
                {
                    if(locationData.CurrentTaskID == taskInfo.TaskID)
                    {
                        //var statusBackup = locationData.StatusBackup.ToLocationStatus();

                        locationData.Status = LocationStatus.None;
                        locationData.CurrentTaskID = string.Empty;
                        bool result = m_locationManager.Update(locationData);

                        CLog.Instance.GetAgvLog(taskInfo.Carrier).WriteInfo($"{taskInfo.TaskID} 库位恢复状态{result.ToChineseString()}：{location} {locationData.Status}");
                    }
                }

                var config = Common.Config.ExcelConfig.Instance.GetRouteConfig(location);
                if(config != null
                    && config.Route > 0)
                {
                    DeviceControl.Instance.CallBoxLightOff(config.Route);
                }
            }

            //var mBinding = materialBindingManager.GetById(taskInfo.TaskID);
            //if(mBinding != null)
            //{
            //    bool result2 = materialBindingManager.Delete(taskInfo.TaskID);
            //    CLog.Instance.GetAgvLog(taskInfo.Carrier).WriteInfo($"{taskInfo.TaskID} 清理物料绑定{result2.ToChineseString()}");
            //}
        }
    }
}
