using Edge.Core.HttpClient;
using HengshanPaymentTerminal.Http.Request;
using Microsoft.Extensions.DependencyInjection;
using Org.BouncyCastle.Asn1.Ocsp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace HengshanPaymentTerminal.Http
{
    public enum SEND_MOTHOD
    {
        GET,
        POST,
        PUT,
        DELETE,
    }

    public class HttpClientUtils:IHttpClientUtil
    {
        static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
        private IHttpClient _httpClientService;

        [ActivatorUtilitiesConstructor]
        public HttpClientUtils(IHttpClient httpClientService)
        {
            _httpClientService = httpClientService;
        }

        public HttpClientUtils()
        {
            ServiceCollection serviceCollection = new ServiceCollection();
            serviceCollection.AddHttpClient();
            ServiceProvider serviceProvider = serviceCollection.BuildServiceProvider();
            IHttpClientFactory httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
            _httpClientService = new HttpClientService(httpClientFactory);
        }

        public async Task<HttpResponseMessage> SendRquest(string api,string requestJson,SEND_MOTHOD mothod)
        {
            try
            {
                var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
                switch(mothod)
                {
                    case SEND_MOTHOD.POST:
                        return await _httpClientService.PostAsync(api, requesStr);
                    case SEND_MOTHOD.PUT:
                        return await _httpClientService.PutAsync(api, requesStr);
                    case SEND_MOTHOD.DELETE:
                        return await _httpClientService.DeleteAsync(api, requesStr);
                }

                throw new Exception("未定义的请求方式");
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                // 创建自定义错误对象
                var error = new
                {
                    Message = "发生错误",
                    Error = ex.Message,
                    StackTrace = ex.StackTrace
                };

                // 返回 500 Internal Server Error 和 JSON 内容
                return new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content = new StringContent(JsonConvert.SerializeObject(error), Encoding.UTF8, "application/json")
                };

            }
        }

        /// <summary>
        /// 发送油品信息给云端
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> SendOilInfo(string requestJson)
        {
            return await SendRquest("api/nozzle/uploadProduct", requestJson,SEND_MOTHOD.POST);
        }

        /// <summary>
        /// 更新油品信息给云端
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> UpdateOilInfo(string requestJson)
        {
            return await SendRquest("api/nozzle/UpdateProduct", requestJson, SEND_MOTHOD.PUT);
        }

        /// <summary>
        /// 删除油品信息发送给云端
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> DeleteOilInfo(string requestJson)
        {
            return await SendRquest("api/nozzle/DeleteProduct", requestJson, SEND_MOTHOD.DELETE);
        }

        /// <summary>
        /// 发送油罐信息给云端
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> SendTankInfo(string requestJson)
        {
            return await SendRquest("api/nozzle/uploadTanks", requestJson, SEND_MOTHOD.POST);
        }

        /// <summary>
        /// 更新油罐信息给云端
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> UpdateTankInfo(string requestJson)
        {
            return await SendRquest("api/nozzle/UpdateTanks", requestJson, SEND_MOTHOD.PUT);
        }

        /// <summary>
        /// 删除油罐信息发送给云端
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> DeleteTankInfo(string requestJson)
        {
            return await SendRquest("api/nozzle/DeleteTanks", requestJson, SEND_MOTHOD.DELETE);
        }

        /// <summary>
        /// 发送油枪信息给云端
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> SendNozzleInfo(string requestJson)
        {
            return await SendRquest("api/nozzle/uploadNozzle", requestJson, SEND_MOTHOD.POST);
        }

        /// <summary>
        /// 更新油枪信息给云端
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> UpdateNozzleInfo(string requestJson)
        {
            return await SendRquest("api/nozzle/UpdateNozzle", requestJson, SEND_MOTHOD.PUT);
        }

        /// <summary>
        /// 删除油枪信息发送给云端
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> DeleteNozzleInfo(string requestJson)
        {
            return await SendRquest("api/nozzle/DeleteNozzle", requestJson, SEND_MOTHOD.DELETE);
        }



        /// <summary>
        /// 更新油枪状态
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> SendNozzleStatu(string requestJson)
        {
            return await SendRquest("api/nozzle/updateNozzleStatus", requestJson, SEND_MOTHOD.PUT);
        }

        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> CreateTransaction(string requestJson)
        {
            return await SendRquest("api/Transactions/CreateTransactions", requestJson, SEND_MOTHOD.POST);
        }

        /// <summary>
        /// 发送授权结果
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<HttpResponseMessage> SendAuthorizationResult(string requestJson)
        {
            return await SendRquest("api/Nozzle/UpdateNozzleAuthorization", requestJson, SEND_MOTHOD.PUT);
        }

        /// <summary>
        /// 发送取消授权结果
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> SendUnAuthorizationResult(string requestJson)
        {
            return await SendRquest("api/Nozzle/UpdateCancelNozzleAuthorization", requestJson, SEND_MOTHOD.PUT);
        }

        /// <summary>
        /// 发送当前已收到云端发送的订单支付信息
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> SendRecievePaidNotice(string requestJson)
        {
            return await SendRquest("", requestJson, SEND_MOTHOD.POST);
        }

        /// <summary>
        /// 发送当前已收到云端发送的订单退款信息
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> SendRecieveRefundNotice(string requestJson)
        {
            return await SendRquest("", requestJson, SEND_MOTHOD.POST);
        }
    }
}
