package com.yida.data.common.core.utils;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONUtil;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import com.yida.data.common.core.entity.system.BaiduApiConfig;
import com.yida.data.common.core.exception.FebsException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 百度api
 *
 * @author ZYJ
 * @date 2023/6/20 19:31
 */
@Slf4j
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class BaiDuUtil {

    /**
     * 百度api返回码
     */
    public static final String ERR_CODE = "error_code";

    /**
     * 百度api对返回码的文本描述内容
     */
    public static final String ERR_MESSAGE = "error_msg";

    /**
     * 百度api接口成功状态码
     */
    public static final String SUCCESS_CODE = "0";

    /**
     * 人脸对比 返回人脸相似度分数
     *
     * @param config 百度api配置信息
     * @param url    原始图片url
     * @param base64 对比图片base64
     * @return java.lang.String
     * @author ZYJ
     * @date 2023/6/21 10:31
     */
    public static String faceMatch(BaiduApiConfig config, String url, String base64) {
        // 初始化一个AipFace
        AipFace client = new AipFace(config.getAppId(), config.getApiKey(), config.getSecretKey());
        // 原始图片url
        MatchRequest originalRequest = new MatchRequest(url, "URL");
        /*
            活体检测控制
            NONE: 不进行控制
            LOW:较低的活体要求(高通过率 低攻击拒绝率)
            NORMAL: 一般的活体要求(平衡的攻击拒绝率, 通过率)
            HIGH: 较高的活体要求(高攻击拒绝率 低通过率)
        */
        originalRequest.setLivenessControl("NONE");
        // 对比图片base64
        MatchRequest contrastRequest = new MatchRequest(new String(base64.getBytes(), StandardCharsets.UTF_8), "BASE64");
        contrastRequest.setLivenessControl("NONE");
        ArrayList<MatchRequest> requests = new ArrayList<>();
        requests.add(originalRequest);
        requests.add(contrastRequest);
        JSONObject result = client.match(requests);
        log.info("人脸对比信息, 返回值: {}", result.toString());
        cn.hutool.json.JSONObject response = JSONUtil.parseObj(result.toString());
        log.info("人脸对比信息: {}", response);

        if (!SUCCESS_CODE.equals(response.containsKey(ERR_CODE) ? response.get(ERR_CODE).toString() : null)) {
            log.error("人脸对比失败，code:{},msg:{},params:{}", response.get(ERR_CODE), response.get(ERR_MESSAGE), url);
            throw new FebsException("人脸对比失败");
        }
        return response.getJSONObject("result").getStr("score");
    }


    public static String faceMatchURl(BaiduApiConfig config, String url, String url2) {
        // 初始化一个AipFace
        AipFace client = new AipFace(config.getAppId(), config.getApiKey(), config.getSecretKey());
        // 原始图片url
        MatchRequest originalRequest = new MatchRequest(url, "URL");
        /*
            活体检测控制
            NONE: 不进行控制
            LOW:较低的活体要求(高通过率 低攻击拒绝率)
            NORMAL: 一般的活体要求(平衡的攻击拒绝率, 通过率)
            HIGH: 较高的活体要求(高攻击拒绝率 低通过率)
        */
        originalRequest.setLivenessControl("NONE");
        // 对比图片base64
        MatchRequest contrastRequest = new MatchRequest(url2, "URL");
        contrastRequest.setLivenessControl("NONE");
        ArrayList<MatchRequest> requests = new ArrayList<>();
        requests.add(originalRequest);
        requests.add(contrastRequest);
        JSONObject result = client.match(requests);
        log.info("人脸对比信息, 返回值: {}", result.toString());
        cn.hutool.json.JSONObject response = JSONUtil.parseObj(result.toString());
        log.info("人脸对比信息: {}", response);

        if (!SUCCESS_CODE.equals(response.containsKey(ERR_CODE) ? response.get(ERR_CODE).toString() : null)) {
            log.error("人脸对比失败，code:{},msg:{},params:{}", response.get(ERR_CODE), response.get(ERR_MESSAGE), url);
            throw new FebsException("人脸对比失败");
        }
        return response.getJSONObject("result").getStr("score");
    }

    public static void main(String[] args) {
        BaiduApiConfig config = new BaiduApiConfig();
        config.setAppId("35081632");
        config.setApiKey("BjKb3C1UbOfFq7Mhd7xfZi6I");
        config.setSecretKey("L7XCtinZl1o3CeX7xBb2of8DnhVhjvfo");

        String url = "http://zbz.yd-data.com:8864/pic/other/20231118/b12b0da7cde14459b1244d1b7406afa4.jpg";
        String url2 = "http://zbz.yd-data.com:8864/pic/other/20231130/8feab7e881544d25892a3b049a0a2387.jpg";

        faceMatchURl(config, url, url2);
    }
}
