using molilian.core;
using dodohold.core;
using Microsoft.AspNetCore.Mvc;
using System.Text.Json;
using static molilian.core.ChartDataCore;
using Org.BouncyCastle.Asn1;
namespace molilian.api.Controllers
{
    [ApiController]
    [MyAuthorize("admin")]
    [Route("api/[controller]/[action]")]
    public class MessageController : ControllerBase
    {
        readonly IAuthorizationProvider provider = new AdminProvider();
        protected IHttpContextAccessor _accessor;
        public MessageController(IHttpContextAccessor accessor)
        {
            _accessor = accessor;
        }
        [HttpGet]
        public async Task<ActionResult> AccountWarning()
        {
            var config = TkConfigCore.Get();
            IEnumerable<TkPoolDTO> tb_list = await TkPoolCore.AllListAsync();

            List<string> typeKeys = new List<string>();
            List<string> riskStrategyKeys = new List<string>();

            Dictionary<string, List<int>> total = new();
            foreach (TkPoolDTO tb in tb_list)
            {
                if (!string.IsNullOrEmpty(tb.parse_type) && !typeKeys.Contains(tb.parse_type)) typeKeys.Add(tb.parse_type);
                if (!string.IsNullOrEmpty(tb.riskStrategy))
                {
                    string key = $"{tb.riskStrategy}-{tb.launchScene}";
                    if (!riskStrategyKeys.Contains(key)) riskStrategyKeys.Add(key);
                }
            }
            // 统计 typeKeys 的在线/离线状态
            foreach (string typeKey in typeKeys)
            {
                var typeAccounts = tb_list.Where(tb => tb.parse_type == typeKey);
                int totalCount = typeAccounts.Count();
                int onlineCount = typeAccounts.Count(tb => tb.status);

                total[typeKey] = new List<int> { totalCount, onlineCount };
            }

            // 统计 riskStrategyKeys 的在线/离线状态
            foreach (string riskStrategyKey in riskStrategyKeys)
            {
                // riskStrategyKey 格式为 "riskStrategy-launchScene"
                string[] parts = riskStrategyKey.Split('-');
                if (parts.Length >= 2)
                {
                    string riskStrategy = parts[0];
                    if (int.TryParse(parts[^1], out int launchScene))
                    {
                        var riskAccounts = tb_list.Where(tb =>
                            tb.riskStrategy == riskStrategy &&
                            tb.launchScene == launchScene);

                        int totalCount = riskAccounts.Count();
                        int onlineCount = riskAccounts.Count(tb => tb.status);

                        total[riskStrategyKey] = [totalCount, onlineCount];
                    }
                }
            }

            total = AccountWarningConfigCore.FilterStats("tb", total, config?.account_warning_ignore_groups);

            IEnumerable<JdPoolDTO> jd_list = await JdPoolCore.AllListAsync();

            List<string> jdTypeKeys = new List<string>();
            List<string> jdRiskStrategyKeys = new List<string>();

            Dictionary<string, List<int>> jdTotal = new();

            // 收集京东的 typeKeys 和 riskStrategyKeys
            foreach (JdPoolDTO jd in jd_list)
            {
                if (jd.id < 38) continue;
                if (!string.IsNullOrEmpty(jd.parse_type) && !jdTypeKeys.Contains(jd.parse_type)) jdTypeKeys.Add(jd.parse_type);
                if (!string.IsNullOrEmpty(jd.riskStrategy))
                {
                    string key = $"{jd.riskStrategy}-{jd.launchScene}";
                    if (!jdRiskStrategyKeys.Contains(key)) jdRiskStrategyKeys.Add(key);
                }
            }

            // 统计京东 typeKeys 的在线/离线状态
            foreach (string typeKey in jdTypeKeys)
            {
                var typeAccounts = jd_list.Where(jd => jd.parse_type == typeKey);
                int totalCount = typeAccounts.Count();
                int onlineCount = typeAccounts.Count(jd => jd.status);

                jdTotal[typeKey] = new List<int> { totalCount, onlineCount };
            }

            // 统计京东 riskStrategyKeys 的在线/离线状态
            foreach (string riskStrategyKey in jdRiskStrategyKeys)
            {
                // riskStrategyKey 格式为 "riskStrategy-launchScene"
                string[] parts = riskStrategyKey.Split('-');
                if (parts.Length >= 2)
                {
                    string riskStrategy = parts[0];
                    if (int.TryParse(parts[^1], out int launchScene))
                    {
                        var riskAccounts = jd_list.Where(jd =>
                            jd.riskStrategy == riskStrategy &&
                            jd.launchScene == launchScene);

                        int totalCount = riskAccounts.Count();
                        int onlineCount = riskAccounts.Count(jd => jd.status);
                        jdTotal[riskStrategyKey] = [totalCount, onlineCount];
                    }
                }
            }

            jdTotal = AccountWarningConfigCore.FilterStats("jd", jdTotal, config?.account_warning_ignore_groups);


            return new APIResult(new
            {
                data = new
                {
                    tb = total,
                    jd = jdTotal
                }
            });
        }

        [HttpPost]
        public async Task<ActionResult> list([FromBody] JsonElement form)
        {
            return new APIResult(new { data = string.Empty });
        }
    }
}
