package com.yn.bftl.thirdparty.modules.tencent.util;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.profile.Language;
import com.yn.bftl.common.common.exception.YnceError;
import com.yn.bftl.common.modules.configuration.entity.ThirdPartyPaySet;
import com.yn.bftl.thirdparty.common.exception.YnceErrorException;
import com.yn.bftl.thirdparty.modules.configuration.service.ThirdPartyPaySetService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 腾讯工具类
 *
 * @author huabiao
 * @create 2023/8/9  15:21
 **/
@Component
public class TencentUtil {

    /**
     * 请求域名
     */
    private static String END_POINT = "asr.ap-guangzhou.tencentcloudapi.com";

    @Resource
    private ThirdPartyPaySetService thirdPartyPaySetService;

    /**
     * 腾讯云-初始化认证对象
     *
     * @return Credential
     * @author huabiao
     * @create 2023/8/9  15:50
     */
    public Credential getCredential() {
        ThirdPartyPaySet thirdPartyPaySet = thirdPartyPaySetService.getThirdPartyPaySetByCompany();
        if (thirdPartyPaySet == null) {
            throw new YnceErrorException(YnceError.YNCE_222001, "未配置参数");
        }
        if (StringUtils.isBlank(thirdPartyPaySet.getTencentSecretId())
                || StringUtils.isBlank(thirdPartyPaySet.getTencentSecretKey())) {
            throw new YnceErrorException(YnceError.YNCE_222001, "未配置腾讯云参数");
        }
        return new Credential(thirdPartyPaySet.getTencentSecretId(), thirdPartyPaySet.getTencentSecretKey());
    }

    /**
     * 腾讯云-代理配置
     *
     * @param reqMethod 请求路径:get请求(HttpProfile.REQ_GET)、post请求(HttpProfile.REQ_POST)
     * @return ClientProfile
     * @author huabiao
     * @create 2023/8/9  16:01
     */
    public ClientProfile getClientProfile(String reqMethod) {
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
//        httpProfile.setProxyHost("真实代理ip");
//        httpProfile.setProxyPort(真实代理端口);
        httpProfile.setReqMethod(reqMethod); // get请求(默认为post请求)
        httpProfile.setConnTimeout(30); // 请求连接超时时间，单位为秒(默认60秒)
        httpProfile.setWriteTimeout(30);  // 设置写入超时时间，单位为秒(默认0秒)
        httpProfile.setReadTimeout(30);  // 设置读取超时时间，单位为秒(默认0秒)
        httpProfile.setEndpoint(END_POINT);
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // SDK 支持打印日志。
//        clientProfile.setDebug(true);
        // 支持设置公共参数 Language, 默认不传，选择(ZH_CN or EN_US)
        clientProfile.setLanguage(Language.EN_US);
        return clientProfile;
    }
}
