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)
        {
            await _httpClientUtils.SendNozzleStatu("[{\"NozzleId\":68,\"Status\":3},{\"NozzleId\":63,\"Status\":3},{\"NozzleId\":64,\"Status\":3},{\"NozzleId\":65,\"Status\":3},{\"NozzleId\":66,\"Status\":3},{\"NozzleId\":69,\"Status\":3}]");
            long id = await _nozzleInfoReposity.UploadNozzleInfo(nozzleInfoInput);
            Logger.Info($"upload nozzle info , id {id}");

            if(id > 0)
            {
                try
                {
                    Edge.Core.Domain.FccTankInfo.FccTankInfo? fccTankInfo = await _tankReposity.GetDetailsTankByTankIdAsync(nozzleInfoInput.TankId);
                    if (nozzleInfoInput.Id == null)
                    {
                        SendNozzleInfo sendNozzleInfo = new SendNozzleInfo(id,nozzleInfoInput, fccTankInfo);
                        var requestJson = JsonConvert.SerializeObject(sendNozzleInfo);
                        HttpResponseMessage httpResponseMessage = await _httpClientUtils.SendNozzleInfo(requestJson);
                        Logger.Info($"send nozzle response : {httpResponseMessage.Content}");
                    } else
                    {
                        UpdateNozzleInfo updateNozzleInfo = new UpdateNozzleInfo(id, nozzleInfoInput, fccTankInfo);
                        var requestJson = JsonConvert.SerializeObject(updateNozzleInfo);
                        HttpResponseMessage httpResponseMessage = await _httpClientUtils.UpdateNozzleInfo(requestJson);
                        Logger.Info($"update 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 = "无更新"
            };
        }

        /// <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 {JsonConvert.SerializeObject(fccNozzleInfo)}");
            if (fccNozzleInfo != null)
            {
                try
                {
                    DeleteNozzleInfo deleteNozzleInfo = new DeleteNozzleInfo(fccNozzleInfo);
                    var requestJson = JsonConvert.SerializeObject(deleteNozzleInfo);
                    HttpResponseMessage httpResponseMessage = await _httpClientUtils.DeleteNozzleInfo(requestJson);
                    Logger.Info($"delete nozzle response : {httpResponseMessage.Content}");
                }
                catch (Exception e)
                {
                    Logger.Error($"delete nozzle to cloud error:{e.Message}", e);
                }
                
                return new SetNozzleInfoOutput()
                {
                    Result = true,
                    Message = "删除成功"
                };
            }

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