using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dodohold.core;
using Dataoke;
using System.Text.RegularExpressions;
using Org.BouncyCastle.Ocsp;
using System.Diagnostics;
using TencentCloud.Fmu.V20191213.Models;
using Microsoft.VisualBasic;
using System.Text.Json;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Security.Cryptography;
using System.Security.Policy;
using Google.Protobuf.WellKnownTypes;
using Microsoft.AspNetCore.Components;
using Org.BouncyCastle.Pqc.Crypto.Ntru;
using System.Net;


namespace molilian.core
{
    public partial class PddUnionPlus
    {
        static Random _random = new Random();
        private WebProxy? _proxy = null;

        public TkConfigDTO _config;
        private static PddPoolDTO _account;
        private static string _end_point;

        static PddUnionPlus()
        {
            _end_point = Environment.GetEnvironmentVariable("EndPoint");
        }

        public PddUnionPlus(PddPoolDTO account)
        {
            _account = account;
            _config = TkConfigCore.Get();

            if (!string.IsNullOrEmpty(account.nodeName))
            {
                _proxy = ProxyNodesCore.GetOne(account.nodeName);
            }
        }
           
        public static bool ShouldIgnoreRequest(TkConfigDTO config, string ip, string oaid, out string reason)
        {
            reason = string.Empty;
            try
            {
                // IP和流量控制
                string ignorePercentageCity = config.pddIgnorePercentageCity;
                string? ipInfo = IP2RegionPlus.Search(ip);
                if (AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
                {
                    reason = $"地区控制：{regionInfo}";
                    return true;
                }

                int limit_num = config.pdd_limit_per_ip_24h;
                if (limit_num > 0)
                {
                    int num = TkLogCore.getClientRequestTotalByIp(TkChannelEnum.pdd, ip);
                    if (num > limit_num)
                    {
                        reason = "IP控制";
                        return true;
                    }
                    num = TkLogCore.getClientRequestTotalByOAID(TkChannelEnum.pdd, oaid);
                    if (num > limit_num)
                    {
                        reason = "OAID控制";
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                reason = "配置异常";
            }
            return false;
        }

        public static bool FlowControlIgnoreRequest(TkConfigDTO config, out string reason)
        {
            reason = string.Empty;
            try
            {
                // IP和流量控制
                int ignorePercentage = config.pddIgnorePercentage;
                if (ignorePercentage == 0) return false;


                var randomValue = (decimal)_random.Next(0, 100);
                bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比，则返回 true，表示需要忽略请求
                if (result)
                {
                    reason = "流量控制";
                    return true;
                }
            }
            catch (Exception ex)
            {
                reason = $"配置异常";
            }
            return false;
        }
    }
}
