using System.Security.Policy;
using Common;
using DAL.PGModels;
using LinqToDB.Common.Internal.Cache;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.VisualBasic;
using Newtonsoft.Json;
using RestSharp;

namespace BLW_Log.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class BlockIPController : ControllerBase
    {

        public readonly IMemoryCache _memoryCache;
        private readonly PostgresContext dbContext;
        public BlockIPController(PostgresContext db, IMemoryCache memoryCache)
        {
            this.dbContext = db;
            this._memoryCache = memoryCache;
        }
        /// <summary>
        /// 获取黑名单
        /// </summary>
        /// <returns></returns>
        [HttpPost()]
        public async Task<ReturnInfo> GetBlockLWSetData()
        {

            ReturnInfo info = new ReturnInfo();
            info.isok = true;
            try
            {
                ReturnInfo data = await Http.HttpPostSendData(new { }, "api/GetBlockLWSetData");
                info.response = data;
            }
            catch (Exception ex)
            {
                info.isok = false;
                info.message = ex.Message;
            }
            return info;
        }

        /// <summary>
        /// 设置黑名单
        /// </summary>
        /// <returns></returns>
        [HttpPost()]
        public async Task<ReturnInfo> BlockLWSet([FromBody] Block_NameList L)
        {

            ReturnInfo info = new ReturnInfo();
            info.isok = true;
            try
            {
                ReturnInfo data = await Http.HttpPostSendData(L, "api/BlockLWSet");
                info.response = data;

                BlockIplog blockIplog = new BlockIplog();
                if (string.IsNullOrEmpty(L.Human))
                {
                    L.Human = "admin";
                }
                else
                {

                }
                blockIplog.Human = L.Human;
                blockIplog.BlockContent = JsonConvert.SerializeObject(L);
                blockIplog.CreateTime = DateTime.Now;
                dbContext.BlockIplogs.Add(blockIplog);
                await dbContext.SaveChangesAsync();

            }
            catch (Exception ex)
            {
                info.isok = false;
                info.message = ex.Message;
            }
            return info;
        }

        /// <summary>
        /// 删除黑名单
        /// 
        /// 字典里面需要有个hotelcode
        /// </summary>
        /// <returns></returns>
        [HttpPost()]
        public async Task<ReturnInfo> BlockLWRemove([FromBody] Dictionary<string, string> L)
        {

            ReturnInfo info = new ReturnInfo();
            info.isok = true;
            try
            {
                ReturnInfo data = await Http.HttpPostFormSendData(L, "api/BlockLWRemove");
                info.response = data;
                info.response = "";

                bool bf = L.TryGetValue("Human", out string Human);

                BlockIplog blockIplog = new BlockIplog();
                if (!bf)
                {
                    blockIplog.Human = "admin";
                }
                blockIplog.Human = Human;
                blockIplog.BlockContent = JsonConvert.SerializeObject(L);
                blockIplog.CreateTime = DateTime.Now;
                dbContext.BlockIplogs.Add(blockIplog);
                await dbContext.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                info.isok = false;
                info.message = ex.Message;
            }
            return info;
        }


        /// <summary>
        ///  UDP 超时不再处理数据的时间
        /// </summary>
        /// <returns></returns>
        [HttpPost()]
        public async Task<ReturnInfo> GetDefineUDPLostDataInterval()
        {

            ReturnInfo info = new ReturnInfo();
            info.isok = true;
            try
            {
                ReturnInfo data = await Http.HttpPostSendData(new { }, "api/GetDefineUDPLostDataInterval");
                info.response = data;
            }
            catch (Exception ex)
            {
                info.isok = false;
                info.message = ex.Message;
            }
            return info;
        }

        /// <summary>
        /// 获取UDP多少包丢一包的数据
        /// </summary>
        /// <returns></returns>
        [HttpPost()]
        public async Task<ReturnInfo> GetDefineUDPLostDataRate()
        {

            ReturnInfo info = new ReturnInfo();
            info.isok = true;
            try
            {
                ReturnInfo data = await Http.HttpPostSendData(new { }, "api/GetDefineUDPLostDataRate");
                info.response = data;
            }
            catch (Exception ex)
            {
                info.isok = false;
                info.message = ex.Message;
            }
            return info;
        }

        /// <summary>
        /// 获取 统计的时间间隔
        /// </summary>
        /// <returns></returns>
        [HttpPost()]
        public async Task<ReturnInfo> GetTongJiTimerInterval()
        {

            ReturnInfo info = new ReturnInfo();
            info.isok = true;
            try
            {
                ReturnInfo data = await Http.HttpPostSendData(new { }, "api/GetTongJiTimerInterval");
                info.response = data;
            }
            catch (Exception ex)
            {
                info.isok = false;
                info.message = ex.Message;
            }
            return info;
        }


        /// <summary>
        /// 三合一
        /// </summary>
        /// <returns></returns>
        public async Task<ReturnInfo> GetConfigParameterList()
        {

            ReturnInfo info = new ReturnInfo();
            info.isok = true;
            try
            {

                Task<ReturnInfo> data1 = Http.HttpPostSendData(new { }, "api/GetDefineUDPLostDataInterval");
                Task<ReturnInfo> data2 = Http.HttpPostSendData(new { }, "api/GetDefineUDPLostDataRate");
                Task<ReturnInfo> data3 = Http.HttpPostSendData(new { }, "api/GetTongJiTimerInterval");
                await Task.WhenAll(data1,data2,data3);

                var U= new {超时08=data1.Result.response, 丢弃比例=data2.Result.response,统计周期=data3.Result.response};
                info.response = U;
                
            }
            catch (Exception ex)
            {
                info.isok = false;
                info.message = ex.Message;
            }
            return info;
        }

        [HttpGet()]
        public async Task<string> GetRoomCount()
        {

            //ReturnInfo r = new ReturnInfo();
            //r.isok = true;
            try
            {
                string Key = "GetRoomCount";
                object ooo = _memoryCache.Get(Key);
                if (ooo != null)
                {
                    //r.isok = true;
                    //r.response = ooo.ToString();
                    return ooo.ToString();
                }
                else
                {

                    var options = new RestClientOptions("http://www.boonlive-rcu.com:7000/");
                    var client = new RestClient(options);

                    var request = new RestRequest("api/values/GetRoomCount");
                    RestResponse response = await client.GetAsync(request);
                    string? str1 = response.Content;
                    _memoryCache.Set(Key, str1, DateTimeOffset.Now.AddHours(2));
                    return str1;
                    //r.isok = true;
                    //r.response = str1;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
                //r.isok = false;
                //r.message = ex.Message;
            }
        }

    }
}
