using Edge.Core.Domain.FccNozzleInfo;
using Edge.Core.Domain.FccTankInfo.Input;
using Edge.Core.Domain.FccTankInfo.Output;
using FccLite.Web.Repositories.FccOilInfo;
using FccLite.Web.Repositories.FccTankInfo;
using HengshanPaymentTerminal.Http;
using HengshanPaymentTerminal.Http.Request;
using Newtonsoft.Json;

namespace FccLite.Web.Services.FccTankInfo
{
    public class TankInfoServiceImpl : ITankInfoService
    {
        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

        private readonly ITankReposity _tankReposity;

        private readonly IOilInfoReposity _oiilInfoReposity;

        private readonly IHttpClientUtil _httpClientUtil;
        public TankInfoServiceImpl(ITankReposity tankReposity,IOilInfoReposity oilInfoReposity,IHttpClientUtil httpClientUtil)
        {
            _tankReposity = tankReposity;
            _oiilInfoReposity = oilInfoReposity;
            _httpClientUtil = httpClientUtil;
        }

        /// <summary>
        /// 通过站点 id 获取油罐基本信息
        /// </summary>
        /// <param name="stationId">站点id</param>
        /// <returns></returns>
        public async Task<List<BaseTankInfoOutput>> GetBaseTankByStationIdAsync(long stationId)
        {
            return await _tankReposity.GetBaseTankByStationIdAsync(stationId);
        }

        /// <summary>
        /// 通过油罐id获取油罐详情信息,油罐id为 -1 只返回当前可关联的油品
        /// </summary>
        /// <param name="tankId">油罐id</param>
        /// <returns></returns>
        public async Task<DetailsTankInfoOutput> GetDetailsTankByTankIdAsync(long tankId)
        {
            Edge.Core.Domain.FccTankInfo.FccTankInfo? fccTankInfo = null;
            if (tankId > 0) fccTankInfo = await _tankReposity.GetDetailsTankByTankIdAsync(tankId);
            Logger.Info($"find tank info with id : {tankId} for details,can got result? {fccTankInfo != null} ");

            List<Edge.Core.Domain.FccOilInfo.FccOilInfo> oilInfos = await _oiilInfoReposity.GetOilInfo();
            List<DetailsTankInfoOutput.RelatableOil> relatableOils = oilInfos.Select(info => new DetailsTankInfoOutput.RelatableOil()
            {
                OilId = info.Id,
                OilName = info.Name
            }).ToList();
            Logger.Info($"current oil info count is {relatableOils.Count}");

            return new DetailsTankInfoOutput()
            {
                Id = fccTankInfo?.Id,
                Number = fccTankInfo?.Number,
                AssociatedOilId = fccTankInfo?.OilId,
                AssociatedOilName = fccTankInfo?.OilName,
                Capacity = fccTankInfo?.Capacity,
                RelatableOils = relatableOils
            };
        }

        /// <summary>
        /// 增加油罐
        /// </summary>
        /// <param name="addTankInput">油罐信息</param>
        /// <returns></returns>
        public async Task<SetTankInfoOutput> AddDetailsTankAsync(AddTankInput addTankInput)
        {
            long id = await _tankReposity.AddDetailsTankAsync(addTankInput);
            Logger.Info($"add tank info change: id {id}");
            if(id > 0)
            {
                try
                {
                    SendTankInfo sendTankInfo = new SendTankInfo(id,addTankInput);
                    string requestJson = JsonConvert.SerializeObject(sendTankInfo);
                    HttpResponseMessage httpResponseMessage = await _httpClientUtil.SendTankInfo(requestJson);
                    string responseJson = await httpResponseMessage.Content.ReadAsStringAsync();
                    Logger.Info($"add tank response : {responseJson}");
                } catch(Exception e)
                {
                    Logger.Error($"add tank to cloud error:{e.Message}", e);
                }
                return new SetTankInfoOutput()
                {
                    Result = true,
                    Message = "保存成功"
                };
            }
            return new SetTankInfoOutput()
            {
                Result = false,
                Message = "保存失败"
            };
        }

        /// <summary>
        /// 修改油罐
        /// </summary>
        /// <param name="updateTankInput">油罐信息</param>
        /// <returns></returns>
        public async Task<SetTankInfoOutput> UpdateDetailsTankAsync(UpdateTankInput updateTankInput)
        {
            long id = await _tankReposity.UpdateDetailsTankAsync(updateTankInput);
            Logger.Info($"update tank info change: id {id}");
            if (id > 0)
            {
                try
                {
                    UpdateTankInfo updateTankInfo = new UpdateTankInfo(id, updateTankInput);
                    string requestJson = JsonConvert.SerializeObject(updateTankInfo);
                    HttpResponseMessage httpResponseMessage = await _httpClientUtil.UpdateTankInfo(requestJson);
                    string responseJson = await httpResponseMessage.Content.ReadAsStringAsync();
                    Logger.Info($"update tank response : {responseJson}");
                }
                catch (Exception e)
                {
                    Logger.Error($"update tank to cloud error:{e.Message}", e);
                }
                return new SetTankInfoOutput()
                {
                    Result = true,
                    Message = "保存成功"
                };
            }
            return new SetTankInfoOutput()
            {
                Result = false,
                Message = "保存失败"
            };
        }

        /// <summary>
        /// 通过油罐id删除油罐
        /// </summary>
        /// <param name="id">油罐id</param>
        /// <returns></returns>
        public async Task<SetTankInfoOutput> DeleteTankById(long id)
        {
            Edge.Core.Domain.FccTankInfo.FccTankInfo? fccTankInfo = await _tankReposity.DeleteTankById(id);
            Logger.Info($"delete tank info ,chage {JsonConvert.SerializeObject(fccTankInfo)}");
            if (fccTankInfo != null)
            {
                try
                {
                    DeleteTankInfo deleteTankInfo = new DeleteTankInfo(fccTankInfo);
                    string requestJson = JsonConvert.SerializeObject(deleteTankInfo);
                    HttpResponseMessage httpResponseMessage = await _httpClientUtil.DeleteTankInfo(requestJson);
                    string responseJson = await httpResponseMessage.Content.ReadAsStringAsync();
                    Logger.Info($"delete tank response : {responseJson}");
                }
                catch (Exception e)
                {
                    Logger.Error($"delete tank to cloud error:{e.Message}", e);
                }
                return new SetTankInfoOutput()
                {
                    Result = true,
                    Message = "删除成功"
                };
            }

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