using Edge.Core.Domain.FccOilInfo.Input;
using Edge.Core.Domain.FccOilInfo.Ouput;
using FccLite.Web.Repositories.FccOilInfo;
using HengshanPaymentTerminal.Http;
using HengshanPaymentTerminal.Http.Request;
using Newtonsoft.Json;
using System.Text.Json;

namespace FccLite.Web.Services.FccOilInfo
{
    public class OilInfoServiceImpl : IOilInfoService
    {
        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
        private readonly IOilInfoReposity _oilInfoReposity;
        private readonly IHttpClientUtil _httpClientUtils;
        public OilInfoServiceImpl(IOilInfoReposity oilInfoReposity, IHttpClientUtil httpClientUtils) 
        {
            _oilInfoReposity = oilInfoReposity;
            _httpClientUtils = httpClientUtils;
        }

        /// <summary>
        /// 通过 id 删除油品信息
        /// </summary>
        /// <param name="id">id</param>
        /// <returns></returns>
        public async Task<OilInfoSetOutput> DeleteById(long id)
        {
            Edge.Core.Domain.FccOilInfo.FccOilInfo? fccOilInfo = await _oilInfoReposity.DeleteByID(id);
            Logger.Info($"delete oil info by id result:{JsonConvert.SerializeObject(fccOilInfo)}");
            if (fccOilInfo != null)
            {
                try
                {
                    DeleteOilInfo deleteOilInfo = new DeleteOilInfo(fccOilInfo);
                    string requestJson = JsonConvert.SerializeObject(deleteOilInfo);
                    HttpResponseMessage httpResponseMessage = await _httpClientUtils.DeleteOilInfo(requestJson);
                    Logger.Info($"delete oilInfo response : {httpResponseMessage.Content}");
                } catch (Exception e)
                {
                    Logger.Error($"delete nozzle to cloud error:{e.Message}", e);
                }
                return new OilInfoSetOutput()
                {
                    Result = true,
                    Message = "删除成功"
                };
            }

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

        /// <summary>
        /// 分页获取油品信息
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="size">页数</param>
        /// <param name="id">过滤条件——id</param>
        /// <param name="name">过滤条件——油品名</param>
        /// <param name="code">过滤条件——油品码</param>
        /// <returns></returns>
        public async Task<OilInfoOutput> GetPage(int page, int size, long? id, string? name, string? code)
        {
            IEnumerable<Edge.Core.Domain.FccOilInfo.FccOilInfo> enumerable = await _oilInfoReposity.GetPage(page, size, id, name, code);

            List<OilInfoOutput.OilInfo> oilInfos = enumerable.Select(info =>
                new OilInfoOutput.OilInfo()
                {
                    Id = info.Id,
                    Name = info.Name,
                    Code = info.Code,
                    Price = info.Price
                }
            ).ToList();

            Logger.Info($"get page of oil info,count is {oilInfos.Count}");
            return new OilInfoOutput()
            {
                Total = oilInfos.Count,
                OilInfoList = oilInfos
            };
        }

        /// <summary>
        /// 新增、修改油品信息
        /// </summary>
        /// <param name="oilInfoInput">油品信息</param>
        /// <returns></returns>
        public async Task<OilInfoSetOutput> Save(OilInfoInput oilInfoInput)
        {
            long id = await _oilInfoReposity.UploadOilInfo(oilInfoInput);
            Logger.Info($"add or update oil info,id {id}");
            if (id <= 0)
            {
                return new OilInfoSetOutput()
                {
                    Result = true,
                    Message = "无更新"
                };
            }

            try
            {
                if (oilInfoInput.Id == null)
                {
                    SendOilInfo sendOilInfo = new SendOilInfo(id, oilInfoInput);
                    var requestJson = JsonConvert.SerializeObject(sendOilInfo);
                    HttpResponseMessage httpResponseMessage = await _httpClientUtils.SendOilInfo(requestJson);
                    Logger.Info($"send oilInfo response : {httpResponseMessage.Content}");
                }
                else
                {
                    UpdateOilInfo updateOilInfo = new UpdateOilInfo(id, oilInfoInput);
                    var requestJson = JsonConvert.SerializeObject(updateOilInfo);
                    HttpResponseMessage httpResponseMessage = await _httpClientUtils.UpdateOilInfo(requestJson);
                    Logger.Info($"update oilInfo response : {httpResponseMessage.Content}");
                }
            }
            catch (Exception e)
            {
                Logger.Error($"send nozzle to cloud error:{e.Message}", e);
            }
            return new OilInfoSetOutput()
            {
                Result = true,
                Message = "保存成功"
            };
        }
    }
}
