package org.jeecg.modules.app.ezt;

import com.alibaba.fastjson.JSON;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.GetEidTokenConfig;
import com.tencentcloudapi.faceid.v20180301.models.GetEidTokenRequest;
import com.tencentcloudapi.faceid.v20180301.models.GetEidTokenResponse;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class GetEidToken {
    private static final String SECRET_ID = "AKIDjKjvugEajfOuCGBtQjpkSxEIBqIWZO0g "; // TODO 您账号的腾讯云密钥
    private static final String SECRET_KEY = "Xt30nNkuvlOo0zlbMwIh1AasxfBUZS7Z"; // TODO 您账号的腾讯云密钥
    private static final String REGION = "ap-guangzhou";
    public static String getEidToken(String idCard, String name) throws Exception {
        // Step 1. 初始化客户端实例
        Credential credential = new Credential(SECRET_ID, SECRET_KEY);
        FaceidClient faceidClient = new FaceidClient(credential, REGION);
        // Step 2. 使用Tencent Cloud API SDK组装请求体，填充参数
        // 详细的接口文档请参考：https://cloud.tencent.com/document/product/1007/54089
        GetEidTokenRequest request = new GetEidTokenRequest();
        request.setMerchantId("00EI2509301314141875"); // 商户ID，请在控制台查看已经申请的商户ID
        GetEidTokenConfig config = new GetEidTokenConfig();
        config.setInputType("4"); // 指定姓名和身份证号输入方式
        request.setConfig(config);
        request.setIdCard(idCard);
        request.setName(name);
        try {
            // Step 3. 调用接口
            GetEidTokenResponse response = faceidClient.GetEidToken(request);
            return GetEidTokenResponse.toJsonString(response);
        } catch (TencentCloudSDKException e) {
            // 调用接口异常在这里处理
            System.err.println("invoke error, code: " + e.getErrorCode() + "message: " + e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    /**
     * <p>获取E证通结果信息</p>
     * @author SheepHy
     * @param eidToken，参数为类则包含{@link Object}
     * @return 返回值描述，如无返回值则为void 返回值为类则包含{@link String}
     */
    public static Map<String,String> getEidResult(String eidToken){
        Map<String,String> map = new HashMap<>();
        try {
            Credential credential = new Credential(SECRET_ID, SECRET_KEY);
            FaceidClient faceidClient = new FaceidClient(credential, REGION);
            com.tencentcloudapi.faceid.v20180301.models.GetEidResultRequest request =
                    new com.tencentcloudapi.faceid.v20180301.models.GetEidResultRequest();
            request.setEidToken(eidToken);
            request.setInfoType("1");
            com.tencentcloudapi.faceid.v20180301.models.GetEidResultResponse response =
                    faceidClient.GetEidResult(request);
            if (response.getText() != null) {
                map.put("ErrCode", String.valueOf(response.getText().getErrCode()));
                map.put("LiveMsg", response.getText().getLiveMsg());
                map.put("Comparemsg", response.getText().getComparemsg());
            }
            return map;
        } catch (Exception e) {
            e.printStackTrace();
            return map;
        }
    }
}
