using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SlnMesnac.Config;
using SlnMesnac.Model.AirportApiEntity;
using SQLitePCL;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Text.Json;
using TouchSocket.Rpc;
using TouchSocket.Sockets;
using TouchSocket.WebApi;
using JsonSerializer = System.Text.Json.JsonSerializer;
using Microsoft.Extensions.Logging;

namespace SlnMesnac.TouchSocket
{
    public class AirPorthttpClient : BackgroundService
    {
        private readonly AppConfig _appConfig;
        private readonly ILogger<AirPorthttpClient> _logger;

        public AirPorthttpClient(AppConfig appConfig, ILogger<AirPorthttpClient> logger)
        {
            _appConfig = appConfig;
            _logger = logger;
        }

        public static WebApiClient AirportAGVClient;
        public static WebApiClient AirportManipulatorClient;

        private WebApiClient CreateWebApiClient(string IpHost)
        {
            var client = new WebApiClient();
            try
            {
                client.Connect(IpHost);
                _logger.LogInformation(IpHost + "连接成功");
            }
            catch (Exception ex)
            {
                _logger.LogError("ERROR: " + ex.Message);
                return null;
            }
            return client;
        }

        protected override Task ExecuteAsync(CancellationToken stoppingToken)
        {
            try
            {
                AirportAGVClient = CreateWebApiClient(_appConfig.AGVIpConfig);
                AirportManipulatorClient = CreateWebApiClient(_appConfig.ManipulatorIpConfig);
            }
            catch (Exception ex)
            {
                _logger.LogError("ERROR: " + ex.Message);
                return Task.FromException(ex);
            }
            return Task.CompletedTask;
        }

        /// <summary>
        /// AGV呼叫请求
        /// </summary>
        /// <returns></returns>
        public static string AGVCallRequest()
        {
            JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:", null, new AGVArrivalSingalEntity());

            return responseValue.ToString();
        }

        /// <summary>
        /// 机械臂开始工作请求
        /// </summary>
        /// <returns></returns>
        public static string ManipulatorStartRequest()
        {
            JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:", null, new AGVArrivalSingalEntity());

            return responseValue.ToString();
        }

        /// <summary>
        /// AGV状态请求
        /// </summary>
        /// <param name="aGVStateRequest"></param>
        /// <returns></returns>
        public static AGVStateResponseEntity AGVStateRequest(AGVStateRequestEntity aGVStateRequest)
        {
            JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:/m1/5051269-4712337-default/apitest/AGVtest", null, aGVStateRequest);
            if (responseValue == null)
            {
                return new AGVStateResponseEntity();
            }
            string json = responseValue.ToString();
            if (string.IsNullOrEmpty(json))
            {
                return new AGVStateResponseEntity();
            }
            AGVStateResponseEntity aGVStateResponseEntity = new AGVStateResponseEntity();
            aGVStateResponseEntity = JsonSerializer.Deserialize<AGVStateResponseEntity>(json);
            return aGVStateResponseEntity;
        }

        /// <summary>
        /// 机械臂状态请求
        /// </summary>
        /// <param name="manipulatorStateRequest"></param>
        /// <returns></returns>
        public static ManipulatorStateResponseEntity ManipulatorStateRequest(ManipulatorStateRequestEntity manipulatorStateRequest)
        {
            JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:/m1/5051269-4712337-default/apitest/manipulatortest", null, manipulatorStateRequest);
            if (responseValue == null)
            {
                return new ManipulatorStateResponseEntity();
            }
            string json = responseValue.ToString();
            if (string.IsNullOrEmpty(json))
            {
                return new ManipulatorStateResponseEntity();
            }
            ManipulatorStateResponseEntity manipulatorState = new ManipulatorStateResponseEntity();
            manipulatorState = JsonSerializer.Deserialize<ManipulatorStateResponseEntity>(json);
            return manipulatorState;
        }
    }
}
