using Pms.DataLibrary.Models;
using Pms.Models;
using PmsSecondaryPackaging.Interface.Model.Carrier;
using PmsSecondaryPackaging.Interface.Model.StepBook;
using PmsSecondaryPackaging.Interface.Model.TaskBook;
using ProjectManagementSystem.Common.Config;
using ProjectManagementSystem.Common.Log;
using ProjectManagementSystem.Common.WebApi;
using ProjectManagementSystem.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ProjectManagementSystem.TaskBookEvent
{
    public class TaskBookRegister
    {
        public TaskBookRegister()
        {
            new TaskBookBehavior_62001();
            new TaskBookBehavior_62002();
            new TaskBookBehavior_62003();
            new TaskBookBehavior_63000();
            new TaskBookBehavior_63001();
            new TaskBookBehavior_63002();
            new TaskBookBehavior_63003();

            new TaskBookBehavior_63011();
            new TaskBookBehavior_63012();
            new TaskBookBehavior_63013();

            new TaskBookBehavior_63400();

            //TaskBookBehavior_65051 taskBookBehavior_65051 = new TaskBookBehavior_65051();
            //TaskBookBehavior_65052 taskBookBehavior_65052 = new TaskBookBehavior_65052();
            //TaskBookBehavior_65053 taskBookBehavior_65053 = new TaskBookBehavior_65053();

            TaskBookBehavior_61005 taskBookBehavior_61005 = new TaskBookBehavior_61005();
            TaskBookBehavior_61007 taskBookBehavior_61007 = new TaskBookBehavior_61007();
            TaskBookBehavior_61008 taskBookBehavior_61008 = new TaskBookBehavior_61008();
            TaskBookBehavior_61009 taskBookBehavior_61009 = new TaskBookBehavior_61009();
            TaskBookBehavior_61010 taskBookBehavior_61010 = new TaskBookBehavior_61010();

            //InitBehaviorCustom();
        }

        private void InitBehaviorCustom()
        {
            PmsApi.Get(EPmsApi.GetOperateList, new { Name = string.Empty }, null, out List<OperateInfo> operateInfoList);

            var plcBehaviorList = ExcelConfig.Instance.PlcBehaviorList;
            foreach (var data in plcBehaviorList)
            {
                new TaskBookBehavior_PLC(data.Behavior);
                AddOrUpdateOperate(data.Behavior, data.Remark, operateInfoList);
            }

            AddOrUpdateOperate(63000, "库位置空", operateInfoList);
            AddOrUpdateOperate(63001, "库位置满", operateInfoList);
            AddOrUpdateOperate(63002, "库位取货", operateInfoList);
            AddOrUpdateOperate(63003, "库位放货", operateInfoList);

            AddOrUpdateOperate(63011, "等待秤台允许上秤", operateInfoList);
            AddOrUpdateOperate(63012, "通知秤台称重", operateInfoList);
            AddOrUpdateOperate(63013, "等待秤台称重完成", operateInfoList);
        }

        public static void AddOrUpdateOperate(int behavior, string remark, List<OperateInfo> operateInfoList)
        {
            if (operateInfoList != null)
            {
                int findIndex = operateInfoList.FindIndex(d => d.Name == behavior.ToString());
                if (findIndex < 0)
                {
                    //添加
                    RequestOperateInfo requestOperateInfo = new RequestOperateInfo()
                    {
                        Name = behavior.ToString(),
                        Code = behavior,
                        isAgvBehavior = false,
                        isDelete = true,
                        isEdit = true,
                        Remark = remark,
                    };

                    PmsApi.Post(EPmsApi.AddOperate, null, requestOperateInfo, out string dataString);
                }
                else
                {
                    //更新
                    var operateInfo = operateInfoList[findIndex];
                    if (operateInfo.Remark != remark)
                    {
                        operateInfo.Remark = remark;
                        PmsApi.Post(EPmsApi.UpdateOperate, null, operateInfo, out string dataString);
                    }
                }
            }
        }


        public static void Log(TaskDetailInfoModel taskDetailInfo, StepDetailInfoModel taskStepInfo, string log)
        {
            string strLog = $"{taskDetailInfo.TaskID} {taskStepInfo.PmsBehavior} {taskDetailInfo.Carrier}# {taskStepInfo.WareHouseID} {log}";
            CLog.Instance.GetLog("Task").WriteInfo(strLog);
        }

        public static void LogException(Exception ex)
        {
            CLog.Instance.GetLog("Task").WriteException("Exception", ex);
        }

        public static StepDetailInfoModel GetCurrentStepDetailInfo(TaskDetailInfoModel taskDetailInfo, List<StepDetailInfoModel> taskStepInfoList)
        {
            return taskStepInfoList.FirstOrDefault(d => d.StepId == taskDetailInfo.CurrentStepID);
        }
    }
}

