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 System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProjectManagementSystem.TaskBookEvent
{
    public class TaskBookBehavior_63006 : BaseTaskBookBehaviorV2
    {
        LocationPropertyManager locationManager = new LocationPropertyManager();
        public TaskBookBehavior_63006() : base(63006, "等待放行盒放行")
        {
        }

        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 true;
                }

                int count = DeviceControl.Instance.Communication.InfoBoxDictionary.Values
                    .Count(d => d.DataValid && marks.Contains(d.Route) && d.BoxCalled);

                if (count < marks.Length)
                {
                    Log(taskDetailInfo, stepInfo, "等待放行");
                    return false;
                }

                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;
        }
    }
}
