using Edge.Core.Core.database;
using Edge.Core.Domain.FccStationInfo.Input;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.EntityFrameworkCore;
using System.Net;
using System.Text.Json;
using System.Xml.Linq;

namespace FccLite.Web.Repositories.FccStationInfo
{
    public class StationRepository : IStationRepository
    {
        static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();

        private readonly MysqlDbContext _dbContext;
        public StationRepository(MysqlDbContext dbContext)
        {
            _dbContext = dbContext;
        }


        /// <summary>
        /// 通过id查找站点信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Edge.Core.Domain.FccStationInfo.FccStationInfo?> FindByID(long id)
        {
            Edge.Core.Domain.FccStationInfo.FccStationInfo? station = await _dbContext.FccStationInfos.FindAsync(id);
            return station;
        }

        /// <summary>
        /// 条件分页查询
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="pageSize">页数</param>
        /// <param name="id">过滤条件——id</param>
        /// <param name="name">过滤条件——站名</param>
        /// <param name="longitude">过滤条件——经度</param>
        /// <param name="latitude">过滤条件——纬度</param>
        /// <returns></returns>
        public async Task<IEnumerable<Edge.Core.Domain.FccStationInfo.FccStationInfo>> GetPage(int page, int pageSize, long? id, string? name, decimal? longitude, decimal? latitude)
        {
            IQueryable<Edge.Core.Domain.FccStationInfo.FccStationInfo> query = _dbContext.FccStationInfos.AsQueryable();
            query = query.WhereIf(id != null, info => info.Id == id);
            query = query.WhereIf(!string.IsNullOrEmpty(name), info => info.Name.Contains(name));
            query = query.WhereIf(longitude != null, info => info.Longitude == longitude);
            query = query.WhereIf(latitude != null, info => info.Latitude == latitude);

            return await query.Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
        }

        /// <summary>
        /// 新增/修改站点信息
        /// </summary>
        /// <param name="stationInfoInput">站点信息</param>
        /// <returns></returns>
        public async Task<Edge.Core.Domain.FccStationInfo.FccStationInfo?> Save(StationInfoInput stationInfoInput)
        {
           
            if (stationInfoInput == null || stationInfoInput.IsNotNorm())
            {
                Logger.Info($"stationInput not norm,it is {JsonSerializer.Serialize(stationInfoInput)}");
                return null;
            }
            if (stationInfoInput.Id != null)
            {
                Edge.Core.Domain.FccStationInfo.FccStationInfo? station = await _dbContext.FccStationInfos.FindAsync(stationInfoInput.Id);

                if (station == null)
                {
                    Logger.Info($"find station info with id:{stationInfoInput.Id} for update,result is null");
                    return null;
                }

                station.BuildId = stationInfoInput.BuildId;
                station.Name = stationInfoInput.Name != null ? stationInfoInput.Name : "";
                station.SecretId = stationInfoInput.SecretId;
                station.AcessKey = stationInfoInput.AcessKey;
                station.Longitude = (decimal)(stationInfoInput.Longitude != null ? stationInfoInput.Longitude : new decimal(0));
                station.Latitude = (decimal)(stationInfoInput.Latitude != null ? stationInfoInput.Latitude : new decimal(0));
                station.SmallProgram = stationInfoInput.SmallProgram;
                station.CloudService = stationInfoInput.CloudService;
                station.MqttService = stationInfoInput.MqttService;
                station.IcardService = stationInfoInput.IcardService != null ? stationInfoInput.IcardService : "";
                station.WebSocketPort = stationInfoInput.WebSocketPort;
                _dbContext.SaveChanges();
                return station;
            }
            Edge.Core.Domain.FccStationInfo.FccStationInfo? fccStationInfo = stationInfoInput.ToComponent();
            _dbContext.FccStationInfos.Add(fccStationInfo);
            _dbContext.SaveChanges();
            return fccStationInfo;
        }

        /// <summary>
        /// 通过id 删除站点信息
        /// </summary>
        /// <param name="id">站点id</param>
        /// <returns></returns>
        public async Task<bool> DeleteByID(long id)
        {
            Edge.Core.Domain.FccStationInfo.FccStationInfo? info = await _dbContext.FccStationInfos.FindAsync(id);
            if (info == null)
            {
                Logger.Info($"find station info with id:{id} for delete,result is null");
                return false;
            }
            _dbContext.FccStationInfos.Remove(info);
            _dbContext.SaveChanges();
            return true;
        }
    }
}
