package shop.alien.util.common.netease;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.Consts;
import org.apache.http.client.HttpClient;
import shop.alien.util.common.netease.util.HttpClient4Utils;
import shop.alien.util.common.netease.util.SignatureUtils;
import shop.alien.util.common.netease.util.Utils;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

/**
 * 为本内容检测
 *
 * @author ssk
 * @version 1.0
 * @date 2025/4/15 15:24
 */
public class TextCheckUtil {

    /**
     * 产品密钥ID，产品标识
     */
    private final static String SECRETID = "3d5df66745bfcce3677b3750120b53c6";
    /**
     * 产品私有密钥，服务端生成签名信息使用，请严格保管，避免泄露
     */
    private final static String SECRETKEY = "4e8b4c4bbc063e4d9224b0ada5da40ab";
    /**
     * 业务ID，易盾根据产品业务特点分配
     */
    private final static String BUSINESSID = "5c577cdd30ae1cc9d4cb7968d0e5b7f9";
    /**
     * 易盾反垃圾云服务文本在线检测接口地址
     */
    private final static String API_URL = "https://as.dun.163.com/v5/text/check";
    /**
     * 实例化HttpClient，发送http请求使用，可根据需要自行调参
     */
    private static HttpClient httpClient = HttpClient4Utils.createHttpClient(100, 20, 2000, 2000, 2000);

    /**
     * 检测文本内容
     *
     * @param content 内容
     * @return map
     */
    public static Map<String, String> check(String content) {
        Map<String, String> resultMap = new HashMap<>();
        Map<String, String> params = new HashMap<>();
        // 1.设置公共参数
        params.put("secretId", SECRETID);
        params.put("businessId", BUSINESSID);
        params.put("version", "v5");
        params.put("timestamp", String.valueOf(System.currentTimeMillis()));
        params.put("nonce", String.valueOf(new Random().nextInt()));
        // MD5, SM3, SHA1, SHA256
        params.put("signatureMethod", "MD5");

        // 2.设置私有参数
        params.put("dataId", UUID.randomUUID().toString());
        params.put("content", content);

        // 预处理参数
        params = Utils.pretreatmentParams(params);
        // 3.生成签名信息
        String signature = SignatureUtils.genSignature(SECRETKEY, params);
        params.put("signature", signature);

        // 4.发送HTTP请求，这里使用的是HttpClient工具包，产品可自行选择自己熟悉的工具包发送请求
        String response = HttpClient4Utils.sendPost(httpClient, API_URL, params, Consts.UTF_8);

        // 5.解析接口返回值
        JsonObject jObject = new JsonParser().parse(response).getAsJsonObject();
        int code = jObject.get("code").getAsInt();
        String msg = jObject.get("msg").getAsString();
        if (code == 200) {
            if (jObject.has("result")) {
                JsonObject resultObject = jObject.getAsJsonObject("result");
                // 内容安全结果
                if (resultObject.has("antispam")) {
                    JsonObject antispam = resultObject.getAsJsonObject("antispam");
                    if (antispam != null) {
                        //建议动作，0：通过，1：嫌疑，2：不通过
                        int suggestion = antispam.get("suggestion").getAsInt();
                        switch (suggestion) {
                            case 0:
                                resultMap.put("suggestion", "通过");
                                resultMap.put("result", "0");
                                break;
                            case 1:
                                resultMap.put("suggestion", "嫌疑");
                                resultMap.put("result", "1");
                                break;
                            case 2:
                                resultMap.put("suggestion", "不通过");
                                resultMap.put("result", "1");
                                break;
                        }
                        //结果类型，1：机器结果，2：人审结果
                        int resultType = antispam.get("resultType").getAsInt();
                        switch (resultType) {
                            case 1:
                                resultMap.put("resultType", "机器结果");
                                break;
                            case 2:
                                resultMap.put("resultType", "人审结果");
                                break;
                        }
                        //审核模式，0：纯机审，1：机审+部分人审，2：机审+全量人审
                        int censorType = antispam.get("censorType").getAsInt();
                        switch (censorType) {
                            case 0:
                                resultMap.put("censorType", "纯机审");
                                break;
                            case 1:
                                resultMap.put("censorType", "机审+部分人审");
                                break;
                            case 2:
                                resultMap.put("censorType", "机审+全量人审");
                                break;
                        }
                    }
                }
            }
        } else {
            resultMap = null;
        }
        return resultMap;
    }

}
