package com.ruoyi.common.valid.liveness;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.common.utils.http.AliHttpUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 活体检测
 * https://market.aliyun.com/apimarket/detail/cmapi00064393?spm=5176.730005.result.10.2271414a01R1ZI#sku=yuncode5839300001
 * @author wjh
 * 2024/9/20
 */
public class LivenessUtils {

    public static final String APP_KEY = SpringUtils.getRequiredProperty("liveness.appKey");
    public static final String APP_CODE = SpringUtils.getRequiredProperty("liveness.appCode");
    public static final String APP_SECRET = SpringUtils.getRequiredProperty("liveness.appSecret");
    public static final String JUMP_URL = SpringUtils.getRequiredProperty("liveness.jumpUrl");


    /**
     * 获取活体检测token
     * @param returnUrl 前端跳转地址
     * @param colorLiveParam 自定义颜色，逗号分隔
     * @param actionLiveParam 自定义动作，逗号分隔
     */
    public static LivenessResponseTokenData getToken(String returnUrl, String colorLiveParam, String actionLiveParam) {
        String host = "https://smkjhtjc.market.alicloudapi.com";
        String path = "/liveness/h5/token";
        String method = "GET";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + APP_CODE);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("returnUrl", returnUrl);
        querys.put("colorLiveParam", colorLiveParam);
        querys.put("actionLiveParam", actionLiveParam);

        try {
            HttpResponse res = AliHttpUtils.doGet(host, path, method, headers, querys);

            TypeReference<LivenessResponseBody<LivenessResponseTokenData>> typeRef
                    = new TypeReference<LivenessResponseBody<LivenessResponseTokenData>>() {};
            LivenessResponseBody<LivenessResponseTokenData> body = JSON.parseObject(EntityUtils.toString(res.getEntity()), typeRef);
            ServiceUtil.assertion(body.getCode() == null || !body.getCode().equals("200"), body.getMsg());
            return body.getData();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 查询活体检测结果
     */
    public static LivenessResponseQueryData queryResult(String token) {
        String host = "https://smkjhtjc.market.alicloudapi.com";
        String path = "/liveness/h5/result";
        String method = "GET";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + APP_CODE);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("token", token);

        try {
            HttpResponse res = AliHttpUtils.doGet(host, path, method, headers, querys);

            TypeReference<LivenessResponseBody<LivenessResponseQueryData>> typeRef
                    = new TypeReference<LivenessResponseBody<LivenessResponseQueryData>>() {};
            LivenessResponseBody<LivenessResponseQueryData> body = JSON.parseObject(EntityUtils.toString(res.getEntity()), typeRef);
            ServiceUtil.assertion(body.getCode() == null || !body.getCode().equals("200"), body.getMsg());
            return body.getData();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
