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

namespace ProjectManagementSystem.TaskBookEvent
{
    public class TaskBookBehavior_63030 : BaseTaskBookBehaviorV2
    {
        LocationPropertyManager locationManager = new LocationPropertyManager();
        public TaskBookBehavior_63030() : base(63030, "等待按钮放行（成品发动机）")
        {
        }

        public override bool TaskBookOperator(TaskData taskDetailInfo, StepData stepInfo)
        {
            try
            {
                int[] marks = stepInfo.WareHouseID.GetLocationMember("放行叫料路线").ToValueArray<int>();
                if(marks == null || marks.Length == 0)
                {
                    Log(taskDetailInfo, stepInfo, "未配置放行叫料路线");
                    return false;
                }


                string nextWareHouse = null;
                var infoBoxDictionary = DeviceControl.Instance.Communication.InfoBoxDictionary;
                for (int i = 0; i < marks.Length; i++)
                {
                    int mark = marks[i];

                    var first = infoBoxDictionary.Values.FirstOrDefault(d => d.DataValid && d.Route == mark && d.BoxCalled);
                    if(first != null)
                    {
                        nextWareHouse = stepInfo.WareHouseID.GetLocationMember("续接任务放货点").GetArrayValue<string>(i);

                        if(!string.IsNullOrEmpty(nextWareHouse))
                        {
                            break;
                        }
                    }
                }

                if(string.IsNullOrEmpty(nextWareHouse))
                {
                    Log(taskDetailInfo, stepInfo, "等待按钮放行（成品）");
                    return false;
                }

                PmsTaskInfoDto data = new PmsTaskInfoDto();
                data.LocationCode = stepInfo.WareHouseID;
                data.TargetLocationCode = nextWareHouse;
                data.ParentTaskId = taskDetailInfo.TaskID;
                data.Carrier = taskDetailInfo.Carrier;

                var result = PmsTaskService.Instance.TaskAddNoCheck(data, new string[] { });
                if (result.code != 20000)
                {
                    Log(taskDetailInfo, stepInfo, $"续接任务失败 {result.message}");
                    return false;
                }

                //Log(taskDetailInfo, stepInfo, $"续接任务成功 {result.message}");
                //System.Threading.Thread.Sleep(500);

                for (int j = 0; j < 3; j++)
                {
                    for (int i = 0; i < marks.Length; i++)
                    {
                        DeviceControl.Instance.CallBoxLightOff(marks[i]);
                        System.Threading.Thread.Sleep(100);
                    }
                }

                Log(taskDetailInfo, stepInfo, $"已放行，灭灯");
                return true;
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
            return false;
        }
    }
}
