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 Google.Protobuf.WellKnownTypes;
using System.Threading.Channels;
using TencentCloud.Tione.V20211111.Models;


namespace molilian.core
{
    public partial class UnionCpsCore
    {
        private static string _end_point;
        static UnionCpsCore()
        {
            _end_point = Environment.GetEnvironmentVariable("EndPoint");
        }

        public static UnionCpsDTO GetFormattedObject(string channel, string ip = "", string oaid = "")
        {
            return new()
            {
                channel = channel,
                end_point = _end_point,
                ip = ip,
                oaid = oaid
            };
        }

        #region CPS 相关 
        public static async Task<APIResult> CpsCouponAsync(string channel, string ip, string oaid)
        {
            UnionCpsDTO result = GetFormattedObject(channel, ip, oaid);
            if (string.IsNullOrEmpty(result.channel))
            {
                result.success = false;
                result.message = "放弃转链";
                result.reason = "无效渠道";
                _ = TkLogCore.CpsLogAsync(result);
                return new APIResult(new
                {
                    result.success,
                    result.message,
                    result.reason,
                    channel,
                });
            }

            var config = TkConfigCore.Get();
            //============================== 放弃转链-地区过滤 ==============================
            if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
            {
                result.success = false;
                result.message = "流量控制";
                result.reason = reason;
                _ = TkLogCore.CpsLogAsync(result);
                return new APIResult(new
                {
                    result.success,
                    result.message,
                    result.reason,
                    result.channel,
                });
            }

            var account = CpsPoolCore.GetOne(result.channel);
            if (account == null)
            {
                result.success = false;
                result.message = "放弃转链";
                result.reason = "没有匹配账号";
                _ = TkLogCore.CpsLogAsync(result);
                return new APIResult(new
                {
                    result.success,
                    result.message,
                    result.reason,
                    result.channel,
                });
            }
            result.accountId = account.id;
            result.accountName = account.name;
            result.deeplink_url = account.deeplink;
            result.token = account.token;
            result.category = account.category;
            result.pid = account.pid;
            result.success = true;
            result.message = "OK";

            _ = TkLogCore.CpsLogAsync(result);
            return new APIResult(new
            {
                result.success,
                result.message,
                result.deeplink_url,
                result.token,
                result.channel,
            });
        }


        //public static async Task<APIResult> EleAsync(string ip, string oaid)
        //{
        //    UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.eleme, ip, oaid);
        //    var config = TkConfigCore.Get();

        //    //============================== 放弃转链-地区过滤 ==============================
        //    if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
        //    {
        //        result.success = false;
        //        result.message = "流量控制";
        //        result.reason = reason;
        //        _ = TkLogCore.CpsLogAsync(result);
        //        return new APIResult(new
        //        {
        //            result.success,
        //            result.message,
        //            result.reason,
        //            channel = result.channel.ToString(),
        //        });
        //    }

        //    var account = ElePoolCore.GetOne();
        //    if (account == null)
        //    {
        //        result.success = false;
        //        result.message = "放弃转链";
        //        result.reason = "没有匹配账号";
        //        _ = TkLogCore.CpsLogAsync(result);
        //        return new APIResult(new
        //        {
        //            result.success,
        //            result.message,
        //            result.reason,
        //            channel = result.channel.ToString(),
        //        });
        //    }
        //    result.accountId = account.id;
        //    result.accountName = account.name;
        //    result.deeplink_url = account.deeplink;
        //    result.success = true;
        //    result.message = "OK";

        //    _ = TkLogCore.CpsLogAsync(result);
        //    return new APIResult(new
        //    {
        //        result.success,
        //        result.message,
        //        result.deeplink_url,
        //        channel = result.channel.ToString(),
        //    });
        //}

        //public static async Task<APIResult> MeituanAsync(string ip, string oaid)
        //{
        //    UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.meituan, ip, oaid);
        //    var config = TkConfigCore.Get();

        //    //============================== 放弃转链-地区过滤 ==============================
        //    if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
        //    {
        //        result.success = false;
        //        result.message = "流量控制";
        //        result.reason = reason;
        //        _ = TkLogCore.CpsLogAsync(result);
        //        return new APIResult(new
        //        {
        //            result.success,
        //            result.message,
        //            result.reason,
        //            channel = result.channel.ToString(),
        //        });
        //    }

        //    var account = MeituanPoolCore.GetOne();
        //    if (account == null)
        //    {
        //        result.success = false;
        //        result.message = "放弃转链";
        //        result.reason = "没有匹配账号";
        //        _ = TkLogCore.CpsLogAsync(result);
        //        return new APIResult(new
        //        {
        //            result.success,
        //            result.message,
        //            result.reason,
        //            channel = result.channel.ToString(),
        //        });
        //    }
        //    result.accountId = account.id;
        //    result.accountName = account.name;
        //    result.deeplink_url = account.deeplink;
        //    result.success = true;
        //    result.message = "OK";

        //    _ = TkLogCore.CpsLogAsync(result);
        //    return new APIResult(new
        //    {
        //        result.success,
        //        result.message,
        //        result.deeplink_url,
        //        channel = result.channel.ToString(),
        //    });
        //}

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

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



    }
}
