package com.qnfhq.aliyun;

import com.alibaba.fastjson2.JSON;
import com.aliyun.captcha20230305.models.VerifyIntelligentCaptchaResponse;
import com.aliyun.tea.TeaException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CaptchaConfig {

    public static com.aliyun.captcha20230305.Client createClient(String accessKeyId, String accessKeySecret, String endpoint) throws Exception {

        com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
                .setCredential(credential)
                .setAccessKeyId(accessKeyId)
                .setAccessKeySecret(accessKeySecret)
                .setType("access_key");

        config.endpoint = endpoint;
        return new com.aliyun.captcha20230305.Client(config);
    }


    /**
     * 智能验证
     * @param accessKeyId
     * @param accessKeySecret
     * @param sceneId
     * @param verifyParam
     * @return
     */
    public static VerifyIntelligentCaptchaResponse verifyIntelligentCaptcha(String accessKeyId, String accessKeySecret, String sceneId,
                                                  String endpoint, String verifyParam) {
        com.aliyun.captcha20230305.Client client = null;
        try {
            client = createClient(accessKeyId, accessKeySecret, endpoint);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        com.aliyun.captcha20230305.models.VerifyIntelligentCaptchaRequest verifyIntelligentCaptchaRequest = new com.aliyun.captcha20230305.models.VerifyIntelligentCaptchaRequest()
                .setCaptchaVerifyParam(verifyParam)
                .setSceneId(sceneId);
        try {
             return client.verifyIntelligentCaptchaWithOptions(verifyIntelligentCaptchaRequest,
                    new com.aliyun.teautil.models.RuntimeOptions());
        } catch (TeaException error) {
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            log.error(error.getMessage(),error);
            com.aliyun.teautil.Common.assertAsString(error.message);
        } catch (Exception _error) {
            TeaException error = new TeaException(_error.getMessage(), _error);
            // 此处仅做打印展示，请谨慎对待异常处理，在工程项目中切勿直接忽略异常。
            // 错误 message
            log.error(error.getMessage(), error);
            com.aliyun.teautil.Common.assertAsString(error.message);
        }
        return null;
    }

}
