using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Elasticsearch.Net;
using JiaZhiQuan.Common.Config;
using JiaZhiQuan.Common.ElasticSearch;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Senparc.Weixin.MP.Containers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Dynamic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using Wicture.DbRESTFul;
using Wicture.DbRESTFul.Cache;
using Wicture.DbRESTFul.Infrastructure.Repository;

namespace JiaZhiQuan.Common
{
    public class TextScanResult
    {
        public string RequestId { get; set; }
        /// <summary>
        /// 英文标签
        /// </summary>
        public string Label { get; set; }
        /// <summary>
        /// 标签中文名称
        /// </summary>
        public string LabelCN { get; set; }
        /// <summary>
        /// 原Suggestion内容，pass/block/preview
        /// </summary>
        public string Suggestion { get; set; }
        public string ResponseText { get; set; }
        /// <summary>
        /// -1 出错了 0 通过 1 阻止 2 人工复核
        /// </summary>
        public int State { get; set; }


        public static Dictionary<string, string> TextCheckLabelDic = new Dictionary<string, string>()
        {
            ["abuse"] = "辱骂",
            ["terrorism"] = "暴恐",
            ["porn"] = "鉴黄",
            ["contraband"] = "违禁",
            ["ad"] = "广告",
            ["politics"] = "敏感",
        };
    }

    public static partial class RepositoryExtension
    {

        /// <summary>
        /// 检查文本内容合法性
        /// </summary>
        public static async Task<TextScanResult> CheckText(this DbRESTFulRepository repository, string text, ConfigFromDb config)
        {
            TextScanResult lastResp = null;
            var left = text.Length;
            var rst = string.Empty;
            if (string.IsNullOrWhiteSpace(text))
            {
                lastResp = new TextScanResult
                {
                    RequestId = string.Empty,
                    Label = "normal",
                    LabelCN = "正常",
                    State = 0,
                    Suggestion = string.Empty,
                    ResponseText = string.Empty,
                };
            }
            while (left > 0)
            {
                var subText = text.Substring(text.Length - left, left > 10000 ? 10000 : left);

                IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", config.AliVisionAccessKeyId, config.AliVisionAccessSecret);
                DefaultAcsClient client = new DefaultAcsClient(profile);
                CommonRequest request = new CommonRequest();
                request.Method = MethodType.POST;
                request.Domain = "imageaudit.cn-shanghai.aliyuncs.com";
                request.Version = "2019-12-30";
                request.Action = "ScanText";
                request.AddQueryParameters("Tasks.1.Content", subText);
                var idx = 1;
                TextScanResult.TextCheckLabelDic.Keys.ForEach(e =>
                {
                    request.AddQueryParameters($"Labels.{idx}.Label", e);
                    idx++;
                });
                CommonResponse response = client.GetCommonResponse(request);
                var respTxt = Encoding.Default.GetString(response.HttpResponse.Content);
                JToken token = JToken.Parse(respTxt);
                var reqId = token.Value<string>("RequestId") ?? string.Empty;
                var checkRst = token.Value<JObject>("Data")?.Value<JArray>("Elements")?.FirstOrDefault()?.Value<JArray>("Results")?.FirstOrDefault();
                var label = checkRst?.Value<string>("Label");
                var suggestion = checkRst?.Value<string>("Suggestion");
                var labelCN = label == null ? "" : TextScanResult.TextCheckLabelDic.ContainsKey(label) ? TextScanResult.TextCheckLabelDic[label] : "正常";
                var state = suggestion == "block" ? 1 : suggestion == "review" ? 2 : suggestion == "pass" ? 0 : -1;
                lastResp = new TextScanResult
                {
                    RequestId = reqId,
                    Label = label,
                    LabelCN = labelCN,
                    State = state,
                    Suggestion = suggestion ?? string.Empty,
                    ResponseText = respTxt
                };
                if (lastResp.State == -1 || lastResp.State == 1) break;
                left -= 10000;
            }
            if (!string.IsNullOrEmpty(lastResp?.ResponseText))
            {
                // 保存最后一次的结果到数据库
                await repository.QuerySingleOrDefaultAsync<dynamic>("insert ignore into s_yundun_text_records(id, resp, createAt) values(@id, @resp, @createAt)", new { id = lastResp.RequestId, resp = lastResp.ResponseText, createAt = DateTime.Now }, null, null, false);
            }
            return await Task.FromResult(lastResp);
        }



    }

}
