using Microsoft.Extensions.Logging;
using SlnMesnac.Repository.service;
using System;
using System.Collections.Generic;
using SlnMesnac.Repository;
using SlnMesnac.Repository.service.Impl;
using TouchSocket.Sockets;
using SlnMesnac.Model.domain;
using SlnMesnac.TouchSocket;
using System.Timers;

namespace SlnMesnac.Business.@base
{
    public class BaseTaskInfoBusiness
    {
        public Action<AirportTask> _Taskaction;
        private static BaseTaskInfoBusiness instance;
        private ILogger<BaseTaskInfoBusiness> _logger;
        private IAirportTaskService _Taskservice;
        private IAGVStateService _AGVStateService;
        public BaseTaskInfoBusiness(ILogger<BaseTaskInfoBusiness> logger, IAirportTaskService Taskservice, IAGVStateService agvService)
        {
            _logger = logger;
            _Taskservice = Taskservice;
            _AGVStateService = agvService;
            InitClearTimer();
        }
        public static BaseTaskInfoBusiness GetInstance(ILogger<BaseTaskInfoBusiness> logger, IAirportTaskService Taskservice, IAGVStateService agvService)
        {
            if (instance == null)
            {
                instance = new BaseTaskInfoBusiness(logger, Taskservice,agvService);
            }
            return instance;
        }
        private void InitClearTimer()
        {
            System.Timers.Timer timer = new System.Timers.Timer
            {
                Interval = 1000, // 每天执行一次
                AutoReset = false,
                
            };
            timer.Elapsed += GetAGVTaskInfo;
            timer.Start();

        }
        /// <summary>
        /// 获取任务列表
        /// </summary>
        private void GetAGVTaskInfo(object source, ElapsedEventArgs e)
        {
            try
            {
                List<AirportTask> Task = _Taskservice.GetAGVTaskInfos();
                if (Task.Count > 0 && Task != null)
                {
                    foreach (var TaskItem in Task)
                    {
                        _logger.LogInformation($"获取任务信息列表");
                        //查询AGV设备状态及AMR设备状态
                        //这里要看AGV是否会立刻更新状态
                        List<AGVState> State = GetAgvState();
                        if (State != null && State.Count > 0)
                        {
                            foreach (var AgvItem in State)
                            {
                                //根据规则筛选最优AGV
                                if (AgvItem.agvworkstate == "任务空闲")
                                {
                                    _logger.LogInformation($"获取空闲AGV列表");
                                    //调用派发任务接口,向任务状态为任务空闲的AGV派发任务
                                    AgvItem.taskno = TaskItem.taskno;
                                    AgvItem.agvworkstate = "任务执行中";
                                    AgvItem.refreshtime = DateTime.Now;
                                    _AGVStateService.UpdateAsync(AgvItem);

                                    //更新任务信息表状态为执行中
                                    TaskItem.agvno = AgvItem.agvno;
                                    TaskItem.taskstate = "执行中";
                                    _Taskservice.UpdateAsync(TaskItem);
                                    _Taskaction?.Invoke(TaskItem);

                                }
                            }
                           

                        }
                    }
                    
                }
                
              

            }
            catch (Exception ex)
            {

            }
        }

        private List<AGVState> GetAgvState()
        {
            List<AGVState> State = null;
            try
            {
                State = _AGVStateService.GetAgvState();
                return State;
            }
            catch (Exception ex)
            {
                return State;
            }

        }


    }
}