package com.ruoyi.applet.utils.tencent;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.faceid.v20180301.FaceidClient;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationRequest;
import com.tencentcloudapi.faceid.v20180301.models.IdCardVerificationResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class TencentUtil {

    @Value("${tencent.secretId}")
    private String secretId;

    @Value("${tencent.secretKey}")
    private String secretKey;

    private Credential credential;

    private Credential getCredential(){
        if (this.credential != null){
            return this.credential;
        }
        this.credential = new Credential(secretId, secretKey);
        return this.credential;
    }

    public IdCardVerificationResponse verificationIdCard(String name, String idCard){
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("faceid.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        FaceidClient client = new FaceidClient(getCredential(), "", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        IdCardVerificationRequest req = new IdCardVerificationRequest();

        req.setName(name);
        req.setIdCard(idCard);

        // 返回的resp是一个IdCardVerificationResponse的实例，与请求对象对应
        IdCardVerificationResponse resp = null;
        try {
            resp = client.IdCardVerification(req);
        } catch (TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }

        return resp;
    }

}
