package ink.wgink.module.sms.factory.sms.impl;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.exceptions.base.SystemException;
import ink.wgink.module.sms.factory.sms.ISmsSend;
import ink.wgink.properties.sms.SmsTencentProperties;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: TencentSendSmsImpl
 * @Description: 腾讯发短信
 * @Author: wanggeng
 * @Date: 2021/5/1 4:35 下午
 * @Version: 1.0
 */
public class TencentSmsSendImpl implements ISmsSend {

    private static final Logger LOG = LoggerFactory.getLogger(TencentSmsSendImpl.class);
    private SmsTencentProperties smsTencentProperties;

    public TencentSmsSendImpl(SmsTencentProperties smsTencentProperties) {
        this.smsTencentProperties = smsTencentProperties;
    }

    @Override
    public void code(String phone, String code) {
        try {
            Credential credential = new Credential(smsTencentProperties.getSecretId(), smsTencentProperties.getSecretKey());
            SmsClient client = new SmsClient(credential, "ap-guangzhou");
            SendSmsRequest sendSmsRequest = new SendSmsRequest();
            // 设置短信应用ID
            sendSmsRequest.setSmsSdkAppId(smsTencentProperties.getSdkAppId());
            // 设置签名
            sendSmsRequest.setSignName(smsTencentProperties.getSignName());
            // 设置短信模板ID
            sendSmsRequest.setTemplateId(smsTencentProperties.getVerificationCodeTemplateId());
            // 设置模板参数
            String[] templateParamSet = {code, "2"};
            sendSmsRequest.setTemplateParamSet(templateParamSet);
            // 设置接受猴急
            String[] phoneNumberSet = {"+86" + phone};
            sendSmsRequest.setPhoneNumberSet(phoneNumberSet);
            SendSmsResponse response = client.SendSms(sendSmsRequest);
            // 获取返回结果
            String responseJsonString = SendSmsResponse.toJsonString(response);
            LOG.debug("Tencent sms response: {}", responseJsonString);
        } catch (TencentCloudSDKException e) {
            throw new SystemException(String.format("ErrorCode: %s, Message: %s", e.getErrorCode(), e.getMessage()), e);
        }
    }

    @Override
    public void content(String phone, String content) {
        String[] contentArray = content.split(":");
        String templateId;
        String params = "";
        if (contentArray.length == 2) {
            templateId = contentArray[0];
            params = contentArray[1];
        } else {
            templateId = contentArray[0];
        }
        if (StringUtils.isBlank(templateId)) {
            throw new ParamsException("内容格式异常：templateId:param1,param2");
        }
        String[] paramArray = StringUtils.isBlank(params) ? new String[0] : params.split(",");
        content(templateId, phone, paramArray);
    }

    /**
     * 发送短息
     *
     * @param templateId
     * @param phone
     * @param templateParams
     */
    private void content(String templateId, String phone, String... templateParams) {
        try {
            Credential credential = new Credential(smsTencentProperties.getSecretId(), smsTencentProperties.getSecretKey());
            SmsClient client = new SmsClient(credential, "ap-guangzhou");
            SendSmsRequest sendSmsRequest = new SendSmsRequest();
            // 设置短信应用ID
            sendSmsRequest.setSmsSdkAppId(smsTencentProperties.getSdkAppId());
            // 设置签名
            sendSmsRequest.setSignName(smsTencentProperties.getSignName());
            // 设置短信模板ID
            sendSmsRequest.setTemplateId(templateId);
            // 设置模板参数
            String[] templateParamSet = templateParams;
            sendSmsRequest.setTemplateParamSet(templateParamSet);
            // 设置接受猴急
            String[] phoneNumberSet = {"+86" + phone};
            sendSmsRequest.setPhoneNumberSet(phoneNumberSet);
            SendSmsResponse response = client.SendSms(sendSmsRequest);
            // 获取返回结果
            String responseJsonString = SendSmsResponse.toJsonString(response);
            LOG.debug("Tencent sms response: {}", responseJsonString);
        } catch (TencentCloudSDKException e) {
            throw new SystemException(String.format("ErrorCode: %s, Message: %s", e.getErrorCode(), e.getMessage()), e);
        }
    }
}
