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 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);
        }


        /// <summary>
        /// 发送油品信息给云端
        /// </summary>
        /// <param name="requestJson"></param>
        /// <returns></returns>
        public async Task<HttpResponseMessage> SendOilInfo(string requestJson)
        {
            try
            {
                var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
                return await _httpClientService.PostAsync("api/nozzle/uploadProduct", requesStr);
            }
            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> UpdateOilInfo(string requestJson)
        {
            try
            {
                var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
                return await _httpClientService.PutAsync("api/nozzle/UpdateProduct", requesStr);
            }
            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> DeleteOilInfo(string requestJson)
        {
            try
            {
                var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
                return await _httpClientService.DeleteAsync("api/nozzle/DeleteProduct", requesStr);
            }
            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> SendTankInfo(string requestJson)
        {
            try
            {
                var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
                return await _httpClientService.PostAsync("api/nozzle/uploadTanks", requesStr);
            }
            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> UpdateTankInfo(string requestJson)
        {
            try
            {
                var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
                return await _httpClientService.PutAsync("api/nozzle/UpdateTanks", requesStr); ;
            }
            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> DeleteTankInfo(string requestJson)
        {
            try
            {
                var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
                return await _httpClientService.DeleteAsync("api/nozzle/DeleteTanks", requesStr);
            }
            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> SendNozzleInfo(string requestJson)
        {
            try
            {
                var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
                return await _httpClientService.PostAsync("api/nozzle/uploadNozzle", requesStr);
            }
            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> UpdateNozzleInfo(string requestJson)
        {
            try
            {
                var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
                return await _httpClientService.PutAsync("api/nozzle/UpdateNozzle ", requesStr);
            }
            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> DeleteNozzleInfo(string requestJson)
        {
            try
            {
                var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
                return await _httpClientService.DeleteAsync("api/nozzle/DeleteNozzle", requesStr);
            }
            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> SendNozzleStatu(string requestJson)
        {
            try
            {
                var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
                return await _httpClientService.PutAsync("api/nozzle/updateNozzleStatus", requesStr);
            }
            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")
                };

            }
        }

        public async Task<HttpResponseMessage> CreateTransaction(string requestJson)
        {
            try
            {
                var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
                return await _httpClientService.PostAsync("api/Transactions/CreateTransactions", requesStr);
            }
            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")
                };

            }
        }

        
    }
}
