using Edge.Core.Domain.FccStationInfo;
using Edge.Core.Domain.FccStationInfo.Input;
using Edge.Core.Domain.FccStationInfo.Output;
using FccLite.Web.Repositories.FccStationInfo;
using System.Text.Json;

namespace FccLite.Web.Services.FccStaionInfo
{
    public class StationServiceImpl : IStationService
    {
        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

        private readonly IStationRepository _stationRepository;

        public StationServiceImpl(IStationRepository stationRepository)
        {
            _stationRepository = stationRepository;
        }


        /// <summary>
        /// 分页查询站点信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <param name="id">过滤条件——id</param>
        /// <param name="name">过滤条件——站名</param>
        /// <param name="longitude">过滤条件——经度</param>
        /// <param name="latitude">过滤条件——纬度</param>
        /// <returns></returns>
        public async Task<StationInfoOutput> GetBaseInfo(int page, int size, long? id, string? name, decimal? longitude, decimal? latitude)
        {
            IEnumerable<FccStationInfo> enumerable = await _stationRepository.GetPage(page, size, id, name, longitude, latitude);
            
            List<StationInfo> fccStationInfos = enumerable.Select(stationInfo =>
                new StationInfo(stationInfo)
            ).ToList();
            Logger.Info($"get staion info，count is {fccStationInfos.Count}");
            return new StationInfoOutput()
            {
                Total = fccStationInfos.Count,
                StationList = fccStationInfos
            };
        }

        /// <summary>
        /// 新增/修改站点信息
        /// </summary>
        /// <param name="stationInfoInput"></param>
        /// <returns></returns>
        public async Task<StationSetOutput> UploadBaseInfo(StationInfoInput stationInfoInput)
        {
            FccStationInfo info = await _stationRepository.Save(stationInfoInput);
            Logger.Info($"add or update result: {JsonSerializer.Serialize(info)}");
            if (info == null)
            {
                return new StationSetOutput()
                {
                    Result = false,
                    Message = "入参格式错误" + stationInfoInput.ToString()
                };
            }

            return new StationSetOutput()
            {
                Result = true,
                Message = "保存成功"
            };
        }

        /// <summary>
        /// 上传站点信息
        /// </summary>
        /// <param name="id">站点id</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<StationSetOutput> DeleteBaseInfo(long id)
        {
            Boolean result = await _stationRepository.DeleteByID(id);
            Logger.Info($"deleted station info by id:{id},result = {result}");
            if(result)
            {
                return new StationSetOutput()
                {
                    Result = true,
                    Message = "删除成功"
                };
            }

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


    }
}
