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 System.Threading.Channels;


namespace molilian.core
{
    public partial class ChartDataCore
    {
        public class ChartDataItem
        {
            public string name { get; set; } = string.Empty;
            public int value { get; set; } = 0;
            public decimal increases { get; set; } = 0;
        }
        public class ChartDataRes
        {
            public List<ChartDataItem> data { get; set; } = [];
            public List<ChartDataItem> data2 { get; set; } = [];
            public List<ChartDataItem> data3 { get; set; } = [];
            public int total { get; set; } = 0;
        }

        public static ChartDataRes GetJdData(string total_key, string accountName, string report_date)
        {
            ChartDataRes result = new();
            total_key = "parse_total";
            if (string.IsNullOrEmpty(accountName) || "all".Equals(accountName)) accountName = "jd";
            string cacheKey = $":{total_key}:{accountName}:{report_date}";
            result.total = TkLogCore.GetTotal(cacheKey);
            if (result.total > 0)
            {
                string[] keys = [];

                cacheKey = $":{total_key}:{accountName}:message:{report_date}";
                string[] message_keys = TkLogCore.GetTotalKeys(cacheKey);

                var combinedKeys = keys.Union(message_keys).ToList();
                combinedKeys.RemoveAll(item => item == "fail");
                combinedKeys.RemoveAll(item => item == "OK");

                foreach (var keyname in combinedKeys)
                {
                    cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
                    int value = TkLogCore.GetTotal(cacheKey);
                    if (value == 0) continue;
                    result.data.Add(new ChartDataItem
                    {
                        name = keyname,
                        value = value,
                        increases = Math.Round((decimal)value / result.total * 100, 2)
                    });
                }
                result.data = result.data.OrderByDescending(item => item.value).ToList();

                cacheKey = $":{total_key}:{accountName}:reason:{report_date}";
                string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey);

                foreach (var keyname in reason_keys)
                {
                    if ("没有优惠券".Equals(keyname)) continue;
                    cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
                    int value = TkLogCore.GetTotal(cacheKey);
                    //if (value < 50) continue;
                    result.data2.Add(new ChartDataItem
                    {
                        name = keyname,
                        value = value,
                        increases = Math.Round((decimal)value / result.total * 100, 2)
                    });
                }
                result.data2 = result.data2.OrderByDescending(item => item.value).ToList();

                string[] deeplink_keys = ["none", "home", "success", "fail"];
                foreach (var keyname in deeplink_keys)
                {
                    cacheKey = $":{total_key}:dp_{keyname}:{accountName}:{report_date}";
                    int value = TkLogCore.GetTotal(cacheKey);
                    if (value == 0) continue;
                    result.data3.Add(new ChartDataItem
                    {
                        name = keyname,
                        value = value,
                        increases = Math.Round((decimal)value / result.total * 100, 2)
                    });
                }
                result.data3 = result.data3.OrderByDescending(item => item.value).ToList();
            }

            return result;
        }
        public static ChartDataRes GetTaobaoData(string total_key, string accountName, string report_date)
        {
            ChartDataRes result = new();
            if (string.IsNullOrEmpty(accountName) || "all".Equals(accountName)) accountName = "tb";
            string cacheKey = $":{total_key}:{accountName}:{report_date}";
            result.total = TkLogCore.GetTotal(cacheKey);
            if (result.total > 0)
            {
                string[] keys = ["success"];

                cacheKey = $":{total_key}:{accountName}:message:{report_date}";
                string[] message_keys = TkLogCore.GetTotalKeys(cacheKey);

                var combinedKeys = keys.Union(message_keys).ToList();
                combinedKeys.RemoveAll(item => item == "fail");
                combinedKeys.RemoveAll(item => item == "OK");

                foreach (var keyname in combinedKeys)
                {
                    cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
                    int value = TkLogCore.GetTotal(cacheKey);
                    if (value == 0) continue;
                    result.data.Add(new ChartDataItem
                    {
                        name = keyname,
                        value = value,
                        increases = Math.Round((decimal)value / result.total * 100, 2)
                    });
                }
                result.data = result.data.OrderByDescending(item => item.value).ToList();

                cacheKey = $":{total_key}:{accountName}:reason:{report_date}";
                string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey);

                foreach (var keyname in reason_keys)
                {
                    if ("没有优惠券".Equals(keyname)) continue;

                    cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
                    int value = TkLogCore.GetTotal(cacheKey);
                    //if (value < 50) continue;
                    result.data2.Add(new ChartDataItem
                    {
                        name = keyname,
                        value = value,
                        increases = Math.Round((decimal)value / result.total * 100, 2)
                    });
                }
                result.data2 = result.data2.OrderByDescending(item => item.value).ToList();

                string[] deeplink_keys = ["none", "home", "success", "fail"];
                foreach (var keyname in deeplink_keys)
                {
                    cacheKey = $":{total_key}:dp_{keyname}:{accountName}:{report_date}";
                    int value = TkLogCore.GetTotal(cacheKey);
                    if (value == 0) continue;
                    result.data3.Add(new ChartDataItem
                    {
                        name = keyname,
                        value = value,
                        increases = Math.Round((decimal)value / result.total * 100, 2)
                    });
                }
                result.data3 = result.data3.OrderByDescending(item => item.value).ToList();
            }

            return result;
        }

        public static ChartDataRes GetTaobaoCouponData(string total_key, string accountName, string report_date)
        {
            ChartDataRes result = new();
            if (string.IsNullOrEmpty(accountName) || "all".Equals(accountName)) accountName = "tb";
            string cacheKey = $":{total_key}:{accountName}:{report_date}";
            result.total = TkLogCore.GetTotal(cacheKey, false);
            if (result.total > 0)
            {
                string[] keys = ["success"];

                cacheKey = $":{total_key}:{accountName}:message:{report_date}";
                string[] message_keys = TkLogCore.GetTotalKeys(cacheKey, false);

                var combinedKeys = keys.Union(message_keys).ToList();
                combinedKeys.RemoveAll(item => item == "fail");
                combinedKeys.RemoveAll(item => item == "OK");

                foreach (var keyname in combinedKeys)
                {
                    cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
                    int value = TkLogCore.GetTotal(cacheKey, false);
                    if (value == 0) continue;
                    result.data.Add(new ChartDataItem
                    {
                        name = keyname,
                        value = value,
                        increases = Math.Round((decimal)value / result.total * 100, 2)
                    });
                }
                result.data = result.data.OrderByDescending(item => item.value).ToList();

                cacheKey = $":{total_key}:{accountName}:reason:{report_date}";
                string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey, false);

                foreach (var keyname in reason_keys)
                {
                    if ("没有优惠券".Equals(keyname)) continue;

                    cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
                    int value = TkLogCore.GetTotal(cacheKey, false);
                    //if (value < 50) continue;
                    result.data2.Add(new ChartDataItem
                    {
                        name = keyname,
                        value = value,
                        increases = Math.Round((decimal)value / result.total * 100, 2)
                    });
                }
                result.data2 = result.data2.OrderByDescending(item => item.value).ToList();
            }
            return result;
        }

        public static ChartDataRes GetToolData(string total_key, string accountName, string report_date)
        {
            ChartDataRes result = new();

            accountName = "tool";
            total_key = "parse_total";

            string cacheKey = $":{total_key}:{accountName}:{report_date}";
            result.total = TkLogCore.GetTotal(cacheKey);
            if (result.total > 0)
            {
                string[] keys = ["bdpan", "wemeet"];

                foreach (var keyname in keys)
                {

                    cacheKey = $":{total_key}:{keyname}:{report_date}";
                    var total = TkLogCore.GetTotal(cacheKey);

                    cacheKey = $":{total_key}:{keyname}:success:{report_date}";
                    int value = TkLogCore.GetTotal(cacheKey);
                    if (value == 0) continue;
                    result.data.Add(new ChartDataItem
                    {
                        name = $"{keyname}-成功",
                        value = value,
                        increases = Math.Round((decimal)value / total * 100, 2)
                    });

                    cacheKey = $":{total_key}:{keyname}:fail:{report_date}";
                    value = TkLogCore.GetTotal(cacheKey);
                    if (value == 0) continue;
                    result.data.Add(new ChartDataItem
                    {
                        name = $"{keyname}-失败",
                        value = value,
                        increases = Math.Round((decimal)value / total * 100, 2)
                    });
                }

                cacheKey = $":{total_key}:{accountName}:reason:{report_date}";
                string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey);

                foreach (var keyname in reason_keys)
                {
                    cacheKey = $":{total_key}:{accountName}:{keyname}:{report_date}";
                    int value = TkLogCore.GetTotal(cacheKey);
                    if (value == 0) continue;
                    result.data2.Add(new ChartDataItem
                    {
                        name = keyname,
                        value = value,
                        increases = Math.Round((decimal)value / result.total * 100, 2)
                    });
                }
                result.data2 = result.data2.OrderByDescending(item => item.value).ToList();


                string[] deeplink_keys = ["none", "home", "success", "fail"];
                foreach (var keyname in deeplink_keys)
                {
                    cacheKey = $":{total_key}:dp_{keyname}:{accountName}:{report_date}";
                    int value = TkLogCore.GetTotal(cacheKey);
                    if (value == 0) continue;
                    result.data3.Add(new ChartDataItem
                    {
                        name = keyname,
                        value = value,
                        increases = Math.Round((decimal)value / result.total * 100, 2)
                    });
                }
                result.data3 = result.data3.OrderByDescending(item => item.value).ToList();
            }


            //string dp_flag = deeplink switch
            //{
            //    "" => "none",
            //    "tbopen://m.taobao.com/tbopen/index.html" or
            //    "pinduoduo://com.xunmeng.pinduoduo/" or
            //    "snssdk1128://feed?refer=web" or
            //    "bdnetdisk://n/action.EXTERNAL_ACTIVITY" or
            //    "openapp.jdmobile://virtual?params=" => "home",
            //    _ => success ? "success" : "fail",
            //};

            ////关于dp的缓存
            //saveParseAccountCache($"dp_{dp_flag}:all", success, message, reason);
            //saveParseAccountCache($"dp_{dp_flag}:{channel}", success, message, reason);
            //if (accountId != 0)
            //{
            //    saveParseAccountCache($"dp_{dp_flag}:{channel}_{accountId}", success, message, reason);
            //}


            return result;
        }


        //public static void GetTaobaoData(string accountName, string report_date)
        //{
        //    if (string.IsNullOrEmpty(accountName)) accountName = "tb";
        //    if (total_key == "tool_total")
        //    {
        //        accountName = "tool";
        //        total_key = "parse_total";

        //        string cacheKey = $":{total_key}:{accountName}:{_report_date}";
        //        double total = TkLogCore.GetTotal(cacheKey);
        //        if (total > 0)
        //        {
        //            string[] keys = ["bdpan", "wemeet"];

        //            foreach (var keyname in keys)
        //            {

        //                cacheKey = $":{total_key}:{keyname}:{_report_date}";
        //                total = TkLogCore.GetTotal(cacheKey);

        //                cacheKey = $":{total_key}:{keyname}:success:{_report_date}";
        //                int value = TkLogCore.GetTotal(cacheKey);
        //                if (value == 0) continue;
        //                result.Add(new { name = $"{keyname}-成功", value, increases = Math.Round(value / total * 100, 2) });

        //                cacheKey = $":{total_key}:{keyname}:fail:{_report_date}";
        //                value = TkLogCore.GetTotal(cacheKey);
        //                if (value == 0) continue;
        //                result.Add(new { name = $"{keyname}-失败", value, increases = Math.Round(value / total * 100, 2) });
        //            }

        //            cacheKey = $":{total_key}:{accountName}:reason:{_report_date}";
        //            string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey);

        //            foreach (var keyname in reason_keys)
        //            {
        //                cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
        //                int value = TkLogCore.GetTotal(cacheKey);
        //                if (value == 0) continue;
        //                reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
        //            }
        //            reason_result = reason_result.OrderByDescending(item => item.value).ToList();
        //        }
        //        return new APIResult(new { total, data = result, data2 = reason_result });
        //    }
        //    else
        //    {
        //        string cacheKey = $":{total_key}:{accountName}:{_report_date}";
        //        double total = TkLogCore.GetTotal(cacheKey);
        //        if (total > 0)
        //        {
        //            string[] keys = ["success"];

        //            cacheKey = $":{total_key}:{accountName}:message:{_report_date}";
        //            string[] message_keys = TkLogCore.GetTotalKeys(cacheKey);

        //            var combinedKeys = keys.Union(message_keys).ToList();
        //            combinedKeys.RemoveAll(item => item == "fail");

        //            foreach (var keyname in combinedKeys)
        //            {
        //                cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
        //                int value = TkLogCore.GetTotal(cacheKey);
        //                if (value == 0) continue;
        //                result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
        //            }
        //            result = result.OrderByDescending(item => item.value).ToList();

        //            cacheKey = $":{total_key}:{accountName}:reason:{_report_date}";
        //            string[] reason_keys = TkLogCore.GetTotalKeys(cacheKey);

        //            foreach (var keyname in reason_keys)
        //            {
        //                cacheKey = $":{total_key}:{accountName}:{keyname}:{_report_date}";
        //                int value = TkLogCore.GetTotal(cacheKey);
        //                if (value < 50) continue;
        //                reason_result.Add(new { name = keyname, value, increases = Math.Round(value / total * 100, 2) });
        //            }
        //            reason_result = reason_result.OrderByDescending(item => item.value).ToList();
        //        }
        //        return new APIResult(new { total, data = result, data2 = reason_result });
        //    }

        //}


    }

}
