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

namespace ProjectManagementSystem.TaskEvent
{
    public class ProcessEndProduct : ICustomProcess
    {
        private LocationPropertyManager locationManager = new LocationPropertyManager();


        public void CustomProcess(List<TaskData> taskDataList)
        {
            //Process产线按钮叫料();
            Process发动机仓库_产线();
            Process发动机热试_仓库();
        }

        private void Process产线按钮叫料()
        {
            string startWhouse = "发动机仓库";
            string endPos = "装配1";

            int maxTaskCount = 3;
            int taskCount = PmsTaskService.Instance.GetEndPosTaskCount(endPos);
            if (taskCount >= 0 && taskCount < maxTaskCount)
            {
                //按钮1是否叫料
                int route = endPos.GetLocationMember("线路").ToValue<int>();
                var infoBoxDict = DeviceControl.Instance.Communication.InfoBoxDictionary;

                if(infoBoxDict.ContainsKey(route)
                    && infoBoxDict[route].DataValid
                    && infoBoxDict[route].BoxCalled)
                {
                    string startPos = locationManager.QueryLocation_Out(startWhouse, LocationStatus.Filled, "空架");
                    if (!string.IsNullOrEmpty(startPos))
                    {
                        PmsTaskInfoDto pmsTaskInfoDto = new PmsTaskInfoDto();
                        pmsTaskInfoDto.WareHouseCode = startWhouse;
                        pmsTaskInfoDto.MaterialId = "空架";
                        pmsTaskInfoDto.TargetLocationCode = endPos;

                        locationManager.UpdateStatus(endPos, LocationStatus.Empty);
                        var result = PmsTaskService.Instance.TaskAdd(pmsTaskInfoDto);

                        if(result.code == 20000)
                        {
                            DeviceControl.Instance.CallBoxLightOff(route);
                        }
                    }
                }


            }

        }

        private void Process发动机仓库_产线()
        {
            string startWhouse = "发动机仓库";
            string endPos = "装配1";

            int maxTaskCount = 3;
            int taskCount = PmsTaskService.Instance.GetEndPosTaskCount(endPos);
            if (taskCount >= 0 && taskCount < maxTaskCount)
            {
                string startPos = locationManager.QueryLocation_Out(startWhouse, LocationStatus.Filled, "空架");
                if (!string.IsNullOrEmpty(startPos))
                {
                    PmsTaskInfoDto pmsTaskInfoDto = new PmsTaskInfoDto();
                    pmsTaskInfoDto.WareHouseCode = startWhouse;
                    pmsTaskInfoDto.MaterialId = "空架";
                    pmsTaskInfoDto.TargetLocationCode = endPos;

                    locationManager.UpdateStatus(endPos, LocationStatus.Empty);
                    PmsTaskService.Instance.TaskAdd(pmsTaskInfoDto);
                }
            }

        }

        private void Process发动机热试_仓库()
        {
            string startWhouse = "发动机热试";
            string endWhouse = "发动机仓库";

            string startPos = locationManager.QueryLocation_Out(startWhouse, LocationStatus.Filled, "满架");
            if (!string.IsNullOrEmpty(startPos))
            {
                string endPos = locationManager.QueryLocation_In(endWhouse, LocationStatus.Empty);
                if(!string.IsNullOrEmpty(endPos))
                {
                    PmsTaskInfoDto pmsTaskInfoDto = new PmsTaskInfoDto();
                    pmsTaskInfoDto.WareHouseCode = startWhouse;
                    pmsTaskInfoDto.MaterialId = "满架";
                    pmsTaskInfoDto.TargetWareHouseCode = endWhouse;

                    PmsTaskService.Instance.TaskAdd(pmsTaskInfoDto);
                }
            }

        }
    }
}
