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 Serilog;
using Serilog.Events;
using Microsoft.Extensions.Logging;

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

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

        public static WebApiClient AirportAGVClient;

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

        public void init()
        {
            try
            {
                AirportAGVClient = CreateWebApiClient(_appConfig.AGVIpConfig);
            }
            catch (Exception ex)
            {
                _logger.LogError("ERROR: " + ex.Message);
            }
        }

        /// <summary>
        /// 获取到的JToken类型转换为实体类
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public T JTokenToEntity<T>(JToken value) where T : class
        {
            if (value == null)
            {
                return null;
            }
            string json = value.ToString();
            if (string.IsNullOrEmpty(json))
            {
                return null;
            }
            T ResponseEntity;
            ResponseEntity = JsonSerializer.Deserialize<T>(json);
            return ResponseEntity;
        }

        /// <summary>
        /// AGV下发任务请求
        /// </summary>
        /// <param name="requestValue"></param>
        /// <returns></returns>
        public AGVResponseEntity<ResponseAddTaskDataEntity> AGVAddTaskRequest(AGVRequestAddTaskEntity requestValue)
        {
            if (AirportAGVClient == null)
            {
                return null;
            }
            JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:/api/task/addTask", null, requestValue);
            return JTokenToEntity<AGVResponseEntity<ResponseAddTaskDataEntity>>(responseValue);
        }

        /// <summary>
        /// 取消/终止任务请求
        /// </summary>
        /// <param name="requestValue"></param>
        /// <returns></returns>
        public AGVResponseEntity<object> AGVOperationalTaskRequest(AGVRequestOperationalTaskEntity requestValue)
        {
            if (AirportAGVClient == null)
            {
                return null;
            }
            JToken responseValue = AirportAGVClient.InvokeT<JToken>("POST:/api/task/operationalTask", null, requestValue);
            return JTokenToEntity<AGVResponseEntity<object>>(responseValue);
        }

        /// <summary>
        /// 查询任务状态请求
        /// </summary>
        /// <param name="requestValue"></param>
        /// <returns></returns>
        public AGVResponseEntity<ResponseTaskStateDetailDataEntity> AGVGetTaskStateDetailRequest(AGVRequestTaskStateDetailEntity requestValue)
        {
            if (AirportAGVClient == null)
            {
                return null;
            }
            JToken responseValue = AirportAGVClient.InvokeT<JToken>("PUT:/api/task/getTaskStateDetail", null, requestValue);
            return JTokenToEntity<AGVResponseEntity<ResponseTaskStateDetailDataEntity>>(responseValue);
        }

        /// <summary>
        /// 获取所有AGV信息请求
        /// </summary>
        /// <returns></returns>
        public AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>> AGVAllStateRequest()
        {
            if (AirportAGVClient == null)
            {
                return null;
            }
            JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/task/robot/getRobot", null);
            return JTokenToEntity<AGVResponseEntity<List<ResponseRobotAtrributeDataEntity>>>(responseValue);
        }

        /// <summary>
        /// 获取当前激活地图信息请求
        /// </summary>
        /// <returns></returns>
        public AGVResponseEntity<ResponseActiveMapDataEntity> AGVMapActiveRequest()
        {
            if (AirportAGVClient == null)
            {
                return null;
            }
            JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/v1.0.0/Maps/mapActive", null);
            return JTokenToEntity<AGVResponseEntity<ResponseActiveMapDataEntity>>(responseValue);
        }

        /// <summary>
        /// 获取当前地图所有位置点请求
        /// </summary>
        /// <param name="requestValue"></param>
        /// <returns></returns>
        public AGVResponseEntity<List<ResponseMapPositionDataEntity>> AGVMapPositionRequest(string requestValue)
        {
            if (AirportAGVClient == null)
            {
                return null;
            }
            JToken responseValue = AirportAGVClient.InvokeT<JToken>("GET:/api/v1.0.0/Positions?mapId={0}", null, requestValue);
            return JTokenToEntity<AGVResponseEntity<List<ResponseMapPositionDataEntity>>>(responseValue);
        }

    }

}
