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

namespace ProjectManagementSystem.TaskEvent
{
    public class ProcessCallboxTask : ICustomProcess
    {
        private string location1;
        private string location2;

        private List<RouteConfig> routeConfigs = ExcelConfig.Instance.RouteConfigList
            .Where(d =>
            {
                var location1 = d.LocationCode.GetLocationMember("续接任务取货点");
                if (string.IsNullOrWhiteSpace(location1)) return false;

                var location2 = d.LocationCode.GetLocationMember("续接任务放货点");
                if (string.IsNullOrWhiteSpace(location2)) return false;

                var routes = d.LocationCode.GetLocationMember("放行叫料路线").ToValueArray<int>();
                if (routes.Length != 1) return false;

                return routes[0] > 0;
            })
            .ToList();

        private LocationPropertyManager locationManager = new LocationPropertyManager();

        public void CustomProcess(List<TaskData> taskDataList)
        {
            location1 = null;
            location2 = null;

            var infoBoxDict = DeviceControl.Instance.Communication.InfoBoxDictionary;

            for (int i = 0; i < routeConfigs.Count; i++)
            {
                var config = routeConfigs[i];
                int route = config.LocationCode.GetLocationMember("放行叫料路线").ToValue<int>();

                if (!infoBoxDict.ContainsKey(route))
                {
                    continue;
                }

                var infoBox = infoBoxDict[route];
                if (!infoBox.DataValid
                    || !infoBox.BoxCalled)
                {
                    continue;
                }

                string pos = config.LocationCode.GetLocationMember("续接任务取货点");
                if (PmsTaskService.Instance.LocationHasTask(pos))
                {
                    //该点已有任务，不能产生任务
                    continue;
                }

                location1 = config.LocationCode.GetLocationMember("续接任务取货点");
                location2 = config.LocationCode.GetLocationMember("续接任务放货点");

                break;
            }

            if (!string.IsNullOrEmpty(location1)
                && !string.IsNullOrEmpty(location2))
            {
                locationManager.UpdateStatusNotLocked(location2, LocationStatus.Empty);

                PmsTaskInfoDto data = new PmsTaskInfoDto();
                data.LocationCode = location1;
                data.TargetLocationCode = location2;

                var result = PmsTaskService.Instance.TaskAdd(data);
            }
        }
    }
}
