using Common;
using CommonEntity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NLog;

namespace UseSQLQueryData.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MonitorController : ControllerBase
    {
        public static Logger logger = LogManager.GetCurrentClassLogger();
        [HttpPost()]
        public ReturnInfo SetMonitor([FromBody] MonitorData lll)
        {
            ReturnInfo r = new ReturnInfo();
            try
            {
                lll.DataList.RemoveAll(x => string.IsNullOrEmpty(x.IPAddress) || x.Port <= 0);
                var osa = lll.DataList.Select(x => { x.AddDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); return x; }).ToList();
                if (lll.Handler.Equals("add"))
                {
                    var QQQ = CSRedisCacheHelper.Get<List<MonitorEndPoint>>("MonitorEndPointList");

                    foreach (var item in osa)
                    {
                        bool bbb= QQQ.Any(A=>A.IPAddress.Equals(item.IPAddress)&&A.Port==item.Port);
                        if (bbb==false)
                        {
                            QQQ.Add(item);
                        }
                    }
                    CSRedisCacheHelper.Forever<List<MonitorEndPoint>>("MonitorEndPointList",QQQ);
                }
                else if (lll.Handler.Equals("remove"))
                {
                    var QQQ = CSRedisCacheHelper.Get<List<MonitorEndPoint>>("MonitorEndPointList");
                    foreach (var item in lll.DataList)
                    {
                        QQQ.RemoveAll(A => A.IPAddress.Equals(item.IPAddress) && A.Port == item.Port);
                    }

                    CSRedisCacheHelper.Forever<List<MonitorEndPoint>>("MonitorEndPointList", QQQ);
                }

                r.isok = true;
                r.response = "sucess";
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                r.isok = false;
                r.response = ex.Message;
            }
            return r;
        }

        [HttpPost()]
        public ReturnInfo GetMonitorData()
        {
            ReturnInfo r = new ReturnInfo();
            try
            {
                var QQQ = CSRedisCacheHelper.Get<List<MonitorEndPoint>>("MonitorEndPointList");
                r.isok = true;
                r.response = QQQ;
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                logger.Error(ex.StackTrace);
                r.isok = false;
                r.message = ex.Message;
            }
            return r;
        }
    }
    public class MonitorData
    {
        public string? Handler { get; set; }
        public List<MonitorEndPoint?> DataList { get; set; }
    }
    public class MonitorEndPoint
    {
        public string? IPAddress { get; set; }
        public int? Port { get; set; } = 0;
        public string? AddDateTime { get; set; }
    }
}
