using Edge.Core.Core.database;
using Edge.Core.Domain.FccNozzleInfo.Input;
using Microsoft.EntityFrameworkCore;

namespace FccLite.Web.Repositories.FccNozzleInfo
{
    public class NozzleInfoReposity : INozzleInfoReposity
    {
        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

        private readonly MysqlDbContext _dbContext;

        public NozzleInfoReposity(MysqlDbContext dbContext)
        {
            _dbContext = dbContext;
        }

        /// <summary>
        /// 根据油枪 id 找油枪信息
        /// </summary>
        /// <param name="id">油枪id</param>
        /// <returns></returns>
        public async Task<Edge.Core.Domain.FccNozzleInfo.FccNozzleInfo?> GetNozzleInfoById(long id)
        {
            return await _dbContext.NozzleInfos.FirstOrDefaultAsync(info => info.Id == id);
        }

        /// <summary>
        /// 新增、修改油枪信息
        /// </summary>
        /// <param name="uploadNozzleInfoInput">油枪信息</param>
        /// <returns></returns>
        public async Task<int> UploadNozzleInfo(UploadNozzleInfoInput uploadNozzleInfoInput)
        {
            if(uploadNozzleInfoInput.Id != null)
            {
                Edge.Core.Domain.FccNozzleInfo.FccNozzleInfo? fccNozzleInfo = await _dbContext.NozzleInfos.FirstOrDefaultAsync(info => info.Id == uploadNozzleInfoInput.Id);
                if(fccNozzleInfo == null)
                {
                    Logger.Info($"find nozzle info with id : {uploadNozzleInfoInput.Id} for update, result is null");
                    return 0;
                }
                fccNozzleInfo.Port = uploadNozzleInfoInput.Port;
                fccNozzleInfo.FuelPoint = uploadNozzleInfoInput.FuelPoint;
                fccNozzleInfo.InternalNum = uploadNozzleInfoInput.InternalNum;
                fccNozzleInfo.ExternalNum = uploadNozzleInfoInput.NozzleNum;
                fccNozzleInfo.TankId = uploadNozzleInfoInput.TankId;
                fccNozzleInfo.TankNum = uploadNozzleInfoInput.TankNum;
                
                return await _dbContext.SaveChangesAsync();
            }

            Edge.Core.Domain.FccNozzleInfo.FccNozzleInfo info = uploadNozzleInfoInput.ToComponent();
            _dbContext.NozzleInfos.Add(info);
            return await _dbContext.SaveChangesAsync();
        }

        /// <summary>
        /// 根据id 删除油枪信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Edge.Core.Domain.FccNozzleInfo.FccNozzleInfo?> DeleteNozzleInfoById(long id)
        {
            Edge.Core.Domain.FccNozzleInfo.FccNozzleInfo? fccNozzleInfo = await _dbContext.NozzleInfos.FirstOrDefaultAsync(info => info.Id == id);
            if (fccNozzleInfo == null)
            {
                Logger.Info($"find nozzle info with id : {id} for delete,result is null");
                return null;
            }

            _dbContext.NozzleInfos.Remove(fccNozzleInfo);
            await _dbContext.SaveChangesAsync();
            return fccNozzleInfo;
        }
    }
}
