using Pms.DataLibrary.Order;
using Pms.Models;
using ProjectManagementSystem.Common.Config;
using ProjectManagementSystem.Common.Core;
using ProjectManagementSystem.Common.Logger;
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 AsyncBehaviorRollerSend : AsyncBehavior
    {
        public override bool FirstOperator(TaskData taskData, StepData stepInfo)
        {
            string location = stepInfo.WareHouseID;
            int id = ExcelConfig.Instance.GetRouteConfig(location).RollerID;

            if (DeviceControl.Instance.Communication.InfoRollerDictionary.ContainsKey(id))
            {
                var data = DeviceControl.Instance.Communication.InfoRollerDictionary[id];

                if (data.DataValid)
                {
                    DeviceControl.Instance.RollerOperateClear(id);
                    Log(taskData, stepInfo, $"滚筒台清除操作已发送");

                    if (data.OperationProgress == 0)
                    {
                        Log(taskData, stepInfo, $"滚筒台操作清除完成");
                        return true;
                    }
                }
            }
            else
            {
                Log(taskData, stepInfo, "获取滚筒台数据失败");
            }


            return false;
        }


        public override bool CustomOperator(TaskData taskData, StepData stepInfo)
        {
            var agvData = PmsApi.GetCarrier(taskData.Carrier);
            if (agvData == null)
            {
                Log(taskData, stepInfo, "获取AGV数据失败");
                return false;
            }

            //判断AGV滚筒是否已经转动
            var logicBits = agvData.LogicBits;
            bool rollerMoveing = (logicBits & (0x01 << 7)) > 0;
            if (!rollerMoveing)
            {
                Log(taskData, stepInfo, $"等待AGV滚筒转动，LogicBits：{logicBits}");
                return false;
            }

            string location = stepInfo.WareHouseID;
            int id = ExcelConfig.Instance.GetRouteConfig(location).RollerID;

            if (DeviceControl.Instance.Communication.InfoRollerDictionary.ContainsKey(id))
            {
                var data = DeviceControl.Instance.Communication.InfoRollerDictionary[id];
                
                if (data.DataValid
                    && data.OperationProgress >= 100)
                {
                    Log(taskData, stepInfo, $"滚筒台进度{data.OperationProgress}");
                    return true;
                }
                else
                {
                    ushort actionCode = 0x2222;
                    if (id == 2 || id == 4)
                    {
                        actionCode = 0x8888;
                    }

                    Log(taskData, stepInfo, $"AGV滚筒已转动，LogicBits：{logicBits}，滚筒台进度{data.OperationProgress}，有效：{data.DataValid}，发送操作0x{actionCode:X2}");
                    DeviceControl.Instance.RollerOperate(id, actionCode);
                }
            }
            else
            {
                Log(taskData, stepInfo, "获取滚筒台数据失败");
            }

            return false;
        }
    }
}
