using Edge.Core.Domain.FccNozzleInfo.Input;
using Edge.Core.Domain.FccNozzleInfo.Output;
using Edge.Core.HttpClient;
using FccLite.Web.Repositories.FccNozzleInfo;
using FccLite.Web.Repositories.FccTankInfo;
using HengshanPaymentTerminal.Http;
using HengshanPaymentTerminal.Http.Request;
using Newtonsoft.Json;
using Org.BouncyCastle.Asn1.Ocsp;
using System.Text;

namespace FccLite.Web.Services.FccNozzleInfo
{
    public class NozzleInfoServiceImpl : INozzleInfnService
    {
        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

        private readonly INozzleInfoReposity _nozzleInfoReposity;
        private readonly ITankReposity _tankReposity;
        private readonly IHttpClientUtil _httpClientUtils;
        //private readonly IHttpClient _httpClient;
        public NozzleInfoServiceImpl(INozzleInfoReposity nozzleInfoReposity,ITankReposity tankReposity, IHttpClientUtil httpClientUtils)
        {
            _nozzleInfoReposity = nozzleInfoReposity;
            _tankReposity = tankReposity;
            _httpClientUtils = httpClientUtils;
            //_httpClient = httpClient;
        }
        /// <summary>
        /// 新增、修改油枪信息
        /// </summary>
        /// <param name="nozzleInfoInput">油枪信息</param>
        /// <returns></returns>
        public async Task<SetNozzleInfoOutput> UploadDetailsNozzleInfo(UploadNozzleInfoInput nozzleInfoInput)
        {
            int row = await _nozzleInfoReposity.UploadNozzleInfo(nozzleInfoInput);
            Logger.Info($"upload nozzle info ,chage row {row}");

            if(row > 0)
            {
                try
                {
                    var type = nozzleInfoInput.Id == null ? 1 : 2;
                    SendNozzleInfo sendNozzleInfo = await UploadNozzleInfoInput2SendNozzleInfo(nozzleInfoInput, type);
                    var requestJson = JsonConvert.SerializeObject(sendNozzleInfo);
                    HttpResponseMessage httpResponseMessage = await _httpClientUtils.SendNozzleInfo(requestJson);
                    Logger.Info($"send nozzle response : {httpResponseMessage.Content}");
                }
                catch (Exception e)
                {
                    Logger.Error($"send nozzle to cloud error:{e.Message}", e);
                }
                return new SetNozzleInfoOutput()
                {
                    Result = true,
                    Message = "保存成功"
                };
            }

            return new SetNozzleInfoOutput()
            {
                Result = true,
                Message = "无更新"
            };
        }

        private async Task<SendNozzleInfo> UploadNozzleInfoInput2SendNozzleInfo(UploadNozzleInfoInput uploadNozzleInfoInput,int typeValue)
        {
            Edge.Core.Domain.FccTankInfo.Output.DetailTankInfoForCloud? detailTank = await _tankReposity.GetTankDetail(uploadNozzleInfoInput.TankId);
            return new SendNozzleInfo()
            {
                PumpId = uploadNozzleInfoInput.FuelPoint,
                InternalGunNumber = uploadNozzleInfoInput.InternalNum,
                ExternalGunNumber = uploadNozzleInfoInput.NozzleNum,
                TankNumber = uploadNozzleInfoInput.TankNum,
                TankCapacity = detailTank?.TankCapacity ?? new decimal(0.00),
                ProductName = detailTank?.ProductName ?? "",
                ProductCode = detailTank?.ProductCode ?? 0,
                ProductPrice = detailTank?.ProductPrice ?? new decimal(0.00),
                type = typeValue
            };
        }

        /// <summary>
        /// 根据id获取油枪信息
        /// </summary>
        /// <param name="id">油枪id</param>
        /// <returns></returns>
        public async Task<DetailsNozzleInfoOutput?> GetDetailsNozzleInfoById(long id)
        {
            Edge.Core.Domain.FccNozzleInfo.FccNozzleInfo? fccNozzleInfo = await _nozzleInfoReposity.GetNozzleInfoById(id);
            if (fccNozzleInfo == null)
            {
                Logger.Info($"find nozzle Info with id {id} for details ,result is null");
                return null;
            }
            List<DetailsNozzleInfoOutput.RelatableTank> tanks = await _tankReposity.GetTankInfo();
            Logger.Info($"find all tanks info for nozzle info details,count is {tanks.Count}");

            return new DetailsNozzleInfoOutput()
            {
                Id = fccNozzleInfo.Id,
                NozzleNum = fccNozzleInfo.ExternalNum,
                Port = fccNozzleInfo.Port,
                FuelPoint = fccNozzleInfo.FuelPoint,
                InternalNum = fccNozzleInfo.InternalNum,
                TankId = fccNozzleInfo.TankId,
                TankNum = fccNozzleInfo.TankNum,
                RelatableTanks = tanks,
            };
            
        }



        /// <summary>
        /// 根据油枪id删除油枪信息
        /// </summary>
        /// <param name="id">油枪id</param>
        /// <returns></returns>
        public async Task<SetNozzleInfoOutput> DeleteNozzleInfoByID(long id)
        {
            Edge.Core.Domain.FccNozzleInfo.FccNozzleInfo? fccNozzleInfo = await _nozzleInfoReposity.DeleteNozzleInfoById(id);
            Logger.Info($"delete nozzle info ,chage {fccNozzleInfo}");
            if (fccNozzleInfo != null)
            {
                var nozzleInfoInput = new UploadNozzleInfoInput()
                {
                    TankId = fccNozzleInfo.TankId,
                    FuelPoint = fccNozzleInfo.FuelPoint,
                    InternalNum = fccNozzleInfo.InternalNum,
                    NozzleNum = fccNozzleInfo.ExternalNum,
                    TankNum = fccNozzleInfo.TankNum
                };
                SendNozzleInfo sendNozzleInfo = await UploadNozzleInfoInput2SendNozzleInfo(nozzleInfoInput, 3);
                var requestJson = JsonConvert.SerializeObject(sendNozzleInfo);
                HttpResponseMessage httpResponseMessage = await _httpClientUtils.SendNozzleInfo(requestJson);
                //var requesStr = new StringContent(requestJson, Encoding.UTF8, "application/json");
                //HttpResponseMessage httpResponseMessage = await _httpClient.PostAsync("api/nozzle/uploadNozzle", requesStr);
                Logger.Info($"send nozzle response : {httpResponseMessage.Content}");
                return new SetNozzleInfoOutput()
                {
                    Result = true,
                    Message = "删除成功"
                };
            }

            return new SetNozzleInfoOutput()
            {
                Result = false,
                Message = "删除失败"
            };
        }
    }
}
