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

namespace ProjectManagementSystem.TaskBookEvent
{
    public class TaskBookBehavior_63007 : BaseTaskBookBehaviorV2
    {
        LocationPropertyManager locationManager = new LocationPropertyManager();

        public TaskBookBehavior_63007() : base(63007, "更改步骤库位")
        {
        }

        public override bool TaskBookOperator(TaskData taskDetailInfo, StepData stepInfo)
        {
            try
            {
                string allNextLocation = stepInfo.WareHouseID.GetLocationMember("下个工位");
                
                string[] nextLocations = allNextLocation.ToValueArray<string>();
                string[] nextAllowLocations = nextLocations.Where(d =>
                {
                    string plcAllowEnter = d.GetLocationMember("PLC允许进入");
                    if (string.IsNullOrEmpty(plcAllowEnter))
                    {
                        return false;
                    }

                    var config = ExcelConfig.Instance.GetRouteConfig(d);
                    if (config == null
                    || config.PlcReadWrite == null
                    || config.PlcConnected == false)
                    {
                        return false;
                    }

                    var read = config.PlcReadWrite.ReadBool(plcAllowEnter);
                    return read.IsSuccess && read.Content;

                }).ToArray();

                if(nextAllowLocations == null || nextAllowLocations.Length == 0)
                {
                    Log(taskDetailInfo, stepInfo, $"{allNextLocation} 中未找到PLC允许进入的工位");
                    return false;
                }

                string nextLocation = null;
                for (int i = 0; i < nextAllowLocations.Length; i++)
                {
                    string tempLocation = nextAllowLocations[i];
                    if (PackControl.Instance.StationDictionary.ContainsKey(tempLocation)
                        && PackControl.Instance.StationDictionary[tempLocation].区域有Agv == false)
                    {
                        nextLocation = tempLocation;
                        break;
                    }
                }

                if (string.IsNullOrEmpty(nextLocation))
                {
                    Log(taskDetailInfo, stepInfo, $"{allNextLocation} 中未找到空位");
                    return false;
                }

                Log(taskDetailInfo, stepInfo, $"检测到可以分配的位置 {nextLocation}");

                //更改库位
                bool result = true;
                int startStepId = taskDetailInfo.CurrentStepID + 1;
                int endStepId = taskDetailInfo.StepList.Max(d => d.StepID);

                for (int i = startStepId; i <= endStepId; i++)
                {
                    result &= PmsApi.UpdateStepWarehouse(taskDetailInfo.TaskID, i, nextLocation);

                    if (!result)
                    {
                        Log(taskDetailInfo, stepInfo, $"更改库位失败");
                        return false;
                    }
                }

                locationManager.UpdateStatusAndStatusBackup(nextLocation, LocationStatus.Locked, LocationStatus.Empty);
                Log(taskDetailInfo, stepInfo, $"已更改库位 {nextLocation}");
                return true;
            }
            catch (Exception ex)
            {
                LogException(ex);
            }
            return false;
        }
    }
}
