using Pms.DataLibrary.Order;
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.TaskEvent
{
    public class ProcessTrafficLight : ICustomProcess
    {
        private bool firstStart = true;

        public void CustomProcess(List<TaskData> taskDataList)
        {
            var agvDataList = PmsApi.GetAllCarrier();
            if (agvDataList == null) return;

            var configList = ExcelConfig.Instance.AreaConfigList;

            for (int i = 0; i < configList.Count; i++)
            {
                var data = configList[i];
                if (data.TrafficLightId <= 0) continue;

                var agvData = agvDataList.FirstOrDefault(d => d.Online &&
                (d.GraphEdge > 0 && data.GraphEdges.Length > 0 && data.GraphEdges.Contains(d.GraphEdge)) ||
                (d.GraphVertex > 0 && data.GraphVertexs.Length > 0 && data.GraphVertexs.Contains(d.GraphVertex)));

                var result = agvData != null;
                if (data.AgvInArea != result || firstStart || data.ReSetTrafficLight)
                {
                    data.SetTrafficLightTime = DateTime.Now;
                    data.AgvInArea = result;
                    DeviceControl.Instance.SetAlarmLight(data.TrafficLightId, data.AgvInArea ? 1 : 0);

                    //CLog.Instance.TaskLog.WriteInfo($"交通灯{data.TrafficLightId} {data.AgvInArea}");
                }

            }

            firstStart = false;
        }
    }
}
