using System.Net;
using dodohold.core.kwaixiaodian;


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

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


        string server = "https://openapi.kwaixiaodian.com";

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

        KwaiXiaodianPlus plus;
        public KsUnionPlus(KsPoolDTO account)
        {
            _account = account;
            _config = TkConfigCore.Get();

            if (!string.IsNullOrEmpty(account.nodeName))
            {
                _proxy = ProxyNodesCore.GetOne(account.nodeName);
            }

            plus = new KwaiXiaodianPlus(server, _account.app_key, _account.app_secret, _account.sign_secret,
                _account.event_secret, string.Empty, 0);
        }

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

                int limit_num = config.ks_limit_per_ip_24h;
                if (limit_num > 0)
                {
                    int num = TkLogCore.getClientRequestTotalByIp(TkChannelEnum.ks, ip);
                    if (num > limit_num)
                    {
                        reason = "IP控制";
                        return true;
                    }
                    num = TkLogCore.getClientRequestTotalByOAID(TkChannelEnum.ks, 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.ksIgnorePercentage;
                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;
        }
    }
}
