using BPA.AGV;
using BPA.MES.Base.Application.Services.AGVService.Dtos;
using MQTTnet.Client;
using MQTTnet.Client.Publishing;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BPA.MES.Base.Application.Services.AGVService.Services
{

    [AllowAnonymous, NonUnify]
    [ApiDescriptionSettings("AGV管理", Name = "AgvThirdParty", Tag = "AGV第三方api", KeepName = true, SplitCamelCase = true, KeepVerb = true)]
    public class AGVThirdPartyService:IAGVThirdPartyService, ITransient, IDynamicApiController
    {
        private readonly AGVHandler _aGVHandler;
        private readonly IMQTTService _MQTTService;
        public AGVThirdPartyService(AGVHandler aGVHandler,  IMQTTService MQTTService)
        {
            _aGVHandler = aGVHandler;
            _MQTTService = MQTTService;
        }

        /// <summary>
        /// 下发任务
        /// </summary>
        /// <param name="cExecuteRequest"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<KCResponse> ExecuteAsync(KCExecuteRequest cExecuteRequest)
        {

            if (cExecuteRequest.JobData != null)
                cExecuteRequest = new KCExecuteRequest
                {
                    Url = cExecuteRequest.Url,//http://[IP:Port]/api/quicktron/wcs/standardized.robot.job.submit
                    RobotJobId = Guid.NewGuid().ToString(),
                    WarehouseId = cExecuteRequest.WarehouseId,
                    JobPriority = cExecuteRequest.JobPriority,
                    JobPriorityType = cExecuteRequest.JobPriorityType,
                    JobData = new KCJobDataRequest
                    {
                        StartPoint = cExecuteRequest.JobData.StartPoint,

                        EndPoint = cExecuteRequest.JobData.EndPoint,
                    }
                };
            return await _aGVHandler.ExecuteAsync(cExecuteRequest);
        }

        /// <summary>
        /// 取消任务
        /// </summary>
        /// <param name="kCCancelRequest"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<KCResponse> CancelAsync(KCCancelRequest kCCancelRequest)
        {
            //http://[IP:Port]/api/quicktron/wcs/standardized.robot.job.cancel
            return await _aGVHandler.CancelAsync(kCCancelRequest);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rollerJobRequest"></param>
        /// <returns></returns>
        public async Task<RollerJobResponse> RollerJobExecuteAsync(RollerJobRequest rollerJobRequest)
        {
            //http://[IP:Port]/api/quicktron/wcs/standardized.roller.job.upstream.response
            return  await _aGVHandler.RollerJobExecuteAsync(rollerJobRequest);
        }

        /// <summary>
        /// 任务回调
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> ExecuteReplyAsync(AGVExecuteReplyRequest input)
        {
            try
            {
               
                var data = new AGVExecuteReplyMQTT()
                {
                    Event = input.Event,
                };
              return  await _MQTTService.MqttPublish(data, Topics.ExecuteReplyTopic, MessageID.TaskState);
            }
            catch (Exception e)
            {
               return false;
            }
        }


        /// <summary>
        /// AGV上下料交互请求
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> LoadAndUnloadAsync(AGVLoadAndUnloadRequest input)
        {
            try
            {
                var data = new AGVLoadAndUnloadMQTT()
                {
                    Event = input.Event,
                };
                return await _MQTTService.MqttPublish(data, Topics.LoadAndUnloadTopic, MessageID.LoadAndUnload);
            }
            catch (Exception e)
            {
                return false;
            }
        }

    }
}
