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 AsyncBehaviorRollerRecv : AsyncBehavior
    {

        public override bool CustomOperator(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
                    && data.OperationProgress >= 100)
                {
                    Log(taskData, stepInfo, $"滚筒台进度{data.OperationProgress}");
                    return true;
                }
                else
                {
                    ushort actionCode = 0x4444;
                    if (id == 1 || id == 3)
                    {
                        actionCode = 0x1111;
                    }

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

            return false;
        }
    }
}
