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

namespace ProjectManagementSystem.Common.Core
{
    public abstract class CTaskEventBase
    {
        protected LocationPropertyManager m_locationManager = new LocationPropertyManager();
        protected MaterialBindingManager materialBindingManager = new MaterialBindingManager();

        private Dictionary<string, TaskData> TaskDataDict { get; set; } = new Dictionary<string, TaskData>();
        private List<ICustomProcess> taskProcesses = new List<ICustomProcess>();

        private Dictionary<int, ResultAgvData> agvDataDict = new Dictionary<int, ResultAgvData>();
        private StringBuilder stringBuilder = new StringBuilder();

        public CTaskEventBase()
        {
            var assemblyName = this.GetType().Assembly.GetName().Name;
            var nameSpace = this.GetType().Namespace;

            var processArray = Common.Function.AppSetting.TryGetValue<string>("TaskProcess").ToValueArray<string>();
            foreach (var item in processArray)
            {

                string fullClassName = $"{nameSpace}.{item}";
                var processObj = Common.Function.InstanceConstructor.GetInstance<ICustomProcess>(assemblyName, fullClassName);

                if (processObj != null)
                {
                    taskProcesses.Add(processObj);
                }
                else
                {
                    CLog.Instance.SystemLog.WriteInfo($"创建对象失败 {assemblyName} {fullClassName}");
                }
            }


            Task.Factory.StartNew(TaskThread);
            Task.Factory.StartNew(TaskThread2);
        }

        private void TaskEventProc()
        {
            DateTime start = DateTime.Now;

            var taskDataList = PmsApi.GetTaskDataList();
            if (taskDataList == null) return;

            var agvDataList = PmsApi.GetAllCarrier();
            if (agvDataList == null) return;

            var taskApiTimeSpan = (DateTime.Now - start).TotalMilliseconds;

            DateTime start2 = DateTime.Now;
            AgvDataProc(taskDataList, agvDataList);
            TaskStateProc(taskDataList, agvDataList);
            TaskDynamicRely(taskDataList);
            TaskCustomProc(taskDataList);
            var operatorTimeSpan = (DateTime.Now - start2).TotalMilliseconds;


            //DateTime start3 = DateTime.Now;

            //stringBuilder.Clear();
            //for (int i = 0; i < taskProcesses.Count; i++)
            //{
            //    DateTime temp = DateTime.Now;
            //    taskProcesses[i].CustomProcess(taskDataList);
            //    var tempTimeSpan = (DateTime.Now - temp).TotalMilliseconds;

            //    stringBuilder.Append($"{tempTimeSpan:F1}ms ");
            //}
            //var customProcessTimeSpan = (DateTime.Now - start3).TotalMilliseconds;

            CLog.Instance.TaskLog.WriteDebug($"{taskApiTimeSpan:F1}ms {operatorTimeSpan:F1}ms");
        }

        private void TaskEventProc2()
        {
            DateTime start = DateTime.Now;

            var taskDataList = PmsApi.GetTaskDataList();
            if (taskDataList == null) return;

            var taskApiTimeSpan = (DateTime.Now - start).TotalMilliseconds;

            //DateTime start2 = DateTime.Now;
            //AgvDataProc(taskDataList, agvDataList);
            //TaskStateProc(taskDataList, agvDataList);
            //TaskDynamicRely(taskDataList);
            //TaskCustomProc(taskDataList);
            //var operatorTimeSpan = (DateTime.Now - start2).TotalMilliseconds;


            DateTime start3 = DateTime.Now;

            stringBuilder.Clear();
            for (int i = 0; i < taskProcesses.Count; i++)
            {
                DateTime temp = DateTime.Now;
                taskProcesses[i].CustomProcess(taskDataList);
                var tempTimeSpan = (DateTime.Now - temp).TotalMilliseconds;

                stringBuilder.Append($"{tempTimeSpan:F1}ms ");
            }
            var customProcessTimeSpan = (DateTime.Now - start3).TotalMilliseconds;

            CLog.Instance.TaskLog.WriteDebug($"{taskApiTimeSpan:F1}ms {customProcessTimeSpan:F1}ms 【{stringBuilder.ToString()}】");
        }


        private void AgvDataProc(List<TaskData> taskDataList, List<ResultAgvData> agvDataList)
        {
            //AGV数据的记录
            for (int i = 0; i < agvDataList.Count; i++)
            {
                var agvData = agvDataList[i];
                bool record = false;

                if (!agvDataDict.ContainsKey(agvData.AgvID))
                {
                    agvDataDict.Add(agvData.AgvID, agvData);
                    record = true;
                }
                else
                {
                    var lastAgvData = agvDataDict[agvData.AgvID];

                    if (agvData.Online != lastAgvData.Online
                        || agvData.GraphEdge != lastAgvData.GraphEdge
                        || agvData.GraphVertex != lastAgvData.GraphVertex
                        || agvData.LogicBits != lastAgvData.LogicBits)
                    {
                        record = true;
                    }

                    agvDataDict[agvData.AgvID] = agvData;
                }

                if (record)
                {
                    string stepString = taskDataList.FirstOrDefault(d => d.TaskState > ETaskState.None && d.Carrier == agvData.AgvID)?.GetCurrentStepInfo()?.ToSimpleString();
                    CLog.Instance.GetAgvLog(agvData.AgvID).WriteDebug($"{stepString}【{agvData.ToSimpleString()}】");
                }
            }
        }

        private void TaskStateProc(List<TaskData> taskDataList, List<ResultAgvData> agvDataList)
        {
            // 当前任务更新到缓存
            foreach (var taskData in taskDataList)
            {
                int agvID = taskData.Carrier > 0 ? taskData.Carrier : taskData.BindingAGVNumber;
                bool recordStep = false;

                if (!TaskDataDict.ContainsKey(taskData.ID))
                {
                    if (taskData.TaskState > ETaskState.None)
                    {
                        TaskDataDict.Add(taskData.ID, taskData);
                        CLog.Instance.GetAgvLog(agvID).WriteDebug($"{taskData.TaskID} 任务状态{taskData.TaskState}(添加记录) {taskData.ID} {taskData.ToSimpleString()}");

                        recordStep = true;
                    }
                }
                else
                {
                    var lastTaskInfo = TaskDataDict[taskData.ID];
                    if (lastTaskInfo.TaskState != taskData.TaskState)
                    {
                        //CLog.Instance.GetAgvLog(agvID).WriteDebug($"{taskData.TaskID} 运行状态改变 {lastTaskInfo.TaskState} => {taskData.TaskState}");
                        TaskStateChanged(taskData, lastTaskInfo);
                    }

                    var lastStepInfo = lastTaskInfo.GetCurrentStepInfo();
                    var stepInfo = taskData.GetCurrentStepInfo();

                    if (stepInfo != null
                        && (lastStepInfo?.TemplateStepID != stepInfo.TemplateStepID
                        || lastStepInfo?.SystemStepState != stepInfo.SystemStepState))
                    {
                        recordStep = true;
                    }

                    TaskDataDict[taskData.ID] = taskData;
                }

                if (recordStep)
                {
                    string stepString = taskData.GetCurrentStepInfo()?.ToSimpleString();
                    var agvData = agvDataDict.ContainsKey(taskData.Carrier) ? agvDataDict[taskData.Carrier] : null;
                    string agvString = agvData?.ToSimpleString();

                    if (!string.IsNullOrEmpty(stepString)
                        ||!string.IsNullOrEmpty(agvString))
                    {
                        CLog.Instance.GetAgvLog(agvID).WriteDebug($"{stepString}【{agvString}】");
                    }
                }
            }

            //获取历史任务对比
            if (TaskDataDict.Count == 0) { return; }
            string[] taskIdArray = TaskDataDict.Keys.ToArray();
            foreach (string id in taskIdArray)
            {
                if (taskDataList.Exists(d => d.ID == id)) continue;

                var taskData = PmsApi.GetHistoryTaskData(TaskDataDict[id].TaskID);
                if (taskData != null
                    && taskData.ID == id
                    && TaskDataDict.ContainsKey(id))
                {
                    CLog.Instance.GetAgvLog(taskData.Carrier).WriteDebug($"{TaskDataDict[id].TaskID} 任务状态{taskData.TaskState}(移除记录) {id}");
                    TaskDataDict.Remove(id);
                    switch (taskData.TaskState)
                    {
                        case ETaskState.Finished:
                            TaskFinished(taskData);
                            break;
                        case ETaskState.Canceled:
                            TaskCanceled(taskData);
                            break;
                        case ETaskState.TerminateTask:
                            TaskTerminated(taskData);
                            break;
                        case ETaskState.Exceptioned:
                            TaskExceptioned(taskData);
                            break;
                        case ETaskState.RecoveryException:
                            break;
                        default:
                            TaskBecomeHistory(taskData);
                            break;
                    }
                }
            }
        }

        /// <summary>
        /// 动态自动续接任务（通过Excel配置的任务依赖）
        /// </summary>
        private void TaskDynamicRely(List<TaskData> taskDataList)
        {
            if (ExcelConfig.Instance.TaskRelyList.Count == 0) return;

            var taskIds = taskDataList.Where(d => d.TaskState == ETaskState.Add)
                .Select(d => d.TaskID)
                .ToArray();

            for (int i = 0; i < taskIds.Length; i++)
            {
                string taskId = taskIds[i];

                //重新获取当前所有任务
                var taskDataListTemp = PmsApi.GetTaskDataList();
                if (taskDataListTemp == null) continue;

                var taskData = taskDataListTemp.FirstOrDefault(d => d.TaskID == taskId);
                if (taskData == null) continue;

                string parentId = GetParentTaskId(taskData, taskDataListTemp, out string currentWhouse, out string relayLastWhouse, out int carrier);
                if (parentId != taskData.ParentTaskID)
                {
                    bool result = PmsApi.TaskChangeRely(taskData.TaskID, parentId);

                    CLog.Instance.GetAgvLog(taskData.Carrier).WriteInfo($"{taskData.TaskID} 更改父任务{result.ToChineseString()} {taskData.ParentTaskID} => {parentId}" +
                        (string.IsNullOrEmpty(parentId) ? string.Empty : $"（{carrier}# {relayLastWhouse} 前往续接 {currentWhouse}）"));
                }
            }
        }

        private string GetParentTaskId(TaskData taskData, List<TaskData> taskDataList, out string currentWhouse, out string relayLastWhouse, out int carrier)
        {
            currentWhouse = null;
            relayLastWhouse = null;
            carrier = 0;

            //从配置表中获取当前任务的第一个点可以续接哪些库位
            string firstPos = taskData.StepList.OrderBy(d => d.StepID).FirstOrDefault()?.WareHouseID;
            string firstWhouse = m_locationManager.QueryWareHouse(firstPos);
            var taskRely = Common.Config.ExcelConfig.Instance.TaskRelyList.FirstOrDefault(f => f.StartWareHouse == firstWhouse);
            if (taskRely == null) return string.Empty;

            //获取已经被续接的任务号
            var parentTaskList = taskDataList.Where(d => !string.IsNullOrEmpty(d.ParentTaskID)).ToList();

            var task = taskDataList
                .Where(d =>
                {
                    //已经续接过的任务不能用
                    var parentTask = parentTaskList.FirstOrDefault(f => f.ParentTaskID == d.TaskID);
                    if (parentTask != null)
                    {
                        if (parentTask.TaskID != taskData.TaskID)
                        {
                            return false;
                        }
                    }

                    //查找已开始任务
                    if (d.TaskState <= ETaskState.Add) return false;

                    //AGV类型需要一致
                    if (d.CarrierType != taskData.CarrierType) return false;

                    ////临近任务结束
                    //if (d.StepList.Count - d.CurrentStepID > 3) return false;

                    string lastPos = d.StepList.OrderByDescending(f => f.StepID).FirstOrDefault()?.WareHouseID;
                    string lastWhouse = m_locationManager.QueryWareHouse(lastPos);
                    d.reserved2 = lastWhouse;

                    return taskRely.EndWareHouse.Contains(lastWhouse);
                })
                .OrderBy(d =>
                {
                    return d.StepList.Count - d.CurrentStepID;//依据任务离最后一步的步骤数量进行排序
                })
                .ThenBy(d => d.StartTime)
                .FirstOrDefault();


            if (task != null)
            {
                currentWhouse = firstWhouse;
                relayLastWhouse = task.reserved2;
                carrier = task.Carrier;
            }

            return (task == null) ? string.Empty : task.TaskID;
        }

        public void TaskThread()
        {
            CLog.Instance.SystemLog.WriteDebug($"CTaskEvent已启动");
            while (true)
            {
                try
                {
                    TaskEventProc();
                }
                catch (Exception ex)
                {
                    CLog.Instance.TaskLog.WriteException("TaskEvent", ex);
                    Thread.Sleep(5000);
                }
                Thread.Sleep(1000);
            }
        }

        public void TaskThread2()
        {
            CLog.Instance.SystemLog.WriteDebug($"CTaskEvent2已启动");
            while (true)
            {
                try
                {
                    TaskEventProc2();
                }
                catch (Exception ex)
                {
                    CLog.Instance.TaskLog.WriteException("TaskEvent2", ex);
                    Thread.Sleep(5000);
                }
                Thread.Sleep(1000);
            }
        }


        public abstract void TaskCustomProc(List<TaskData> taskDataList);
        public abstract void TaskStateChanged(TaskData currentTaskInfo, TaskData lastTaskInfo);
        public abstract void TaskFinished(TaskData taskInfo);
        public abstract void TaskExceptioned(TaskData taskInfo);
        public abstract void TaskCanceled(TaskData taskInfo);
        public abstract void TaskTerminated(TaskData taskInfo);
        public abstract void TaskBecomeHistory(TaskData taskInfo);
    }

    public interface ICustomProcess
    {
        void CustomProcess(List<TaskData> taskDataList);
    }
}
