using Edge.Core.Domain.FccTankInfo.Input;
using Edge.Core.Domain.FccTankInfo.Output;
using FccLite.Web.Repositories.FccOilInfo;
using FccLite.Web.Repositories.FccTankInfo;

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

        private readonly ITankReposity _tankReposity;

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

        /// <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)
        {
            int row = await _tankReposity.AddDetailsTankAsync(addTankInput);
            Logger.Info($"add tank info change:{row}");
            if(row > 0)
            {
                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)
        {
            int row = await _tankReposity.UpdateDetailsTankAsync(updateTankInput);
            Logger.Info($"update tank info change:{row}");
            if (row > 0)
            {
                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)
        {
            int row = await _tankReposity.DeleteTankById(id);
            if (row > 0)
            {
                return new SetTankInfoOutput()
                {
                    Result = true,
                    Message = "删除成功"
                };
            }

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