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

namespace SlnMesnac.TouchSocket
{
    public class MeshttpClient
    {
        private readonly AppConfig _appConfig;
        private readonly ILogger<MeshttpClient> _logger;
        public static Action<string, string> RefreshStateEvent;
        public static Action<string> _RefreshLogMessageAction;
        public MeshttpClient(AppConfig appConfig, ILogger<MeshttpClient> logger)
        {
            _appConfig = appConfig;
            _logger = logger;
        }

        public static WebApiClient MESHttpClient;

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


        /// <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>
        /// 主动上报
        /// </summary>
        /// <param name="requestValue"></param>
        /// <returns></returns>
        public ApiResponse<MesParaData> autoread(MesParaData requestValue)
        {
            try
            {
                if (MESHttpClient == null)
                {
                    return null;
                }
                JToken responseValue = MESHttpClient.InvokeT<JToken>("POST:/autoread", null, requestValue);
                //JToken responseValue = MESHttpClient.InvokeT<JToken>("POST:/ApiServer/autoread", null, requestValue);
                _RefreshLogMessageAction?.Invoke("发送MES自动读取指令"+JsonSerializer.Serialize(requestValue));
                _logger.LogInformation("发送MES自动读取指令{0}", JsonSerializer.Serialize(requestValue));

                RefreshStateEvent?.Invoke(requestValue.EQUID,requestValue.EPCID);
                return JTokenToEntity<ApiResponse<MesParaData>>(responseValue);
            }
            catch (Exception ex)
            {
                _logger.LogError("ERROR: " + ex.Message);
                return new ApiResponse<MesParaData>();
            }
           
        }

    }

}
