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

import com.github.qcloudsms.SmsMultiSender;
import com.github.qcloudsms.SmsMultiSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import ink.wgink.exceptions.base.SystemException;
import ink.wgink.module.sms.factory.sms.ISmsSend;
import ink.wgink.properties.sms.SmsTencentProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 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) {
        String[] params = {code};
        SmsMultiSender sender = new SmsMultiSender(Integer.parseInt(smsTencentProperties.getAppId()), smsTencentProperties.getAppKey());
        try {
            SmsMultiSenderResult result = sender.sendWithParam("86", new String[]{phone}, Integer.parseInt(smsTencentProperties.getVerificationCodeTemplateId()), params, smsTencentProperties.getSmsSign(), "", "");
            LOG.debug("Tencent sms result: {}", result);
        } catch (HTTPException e) {
            LOG.error(e.getMessage(), e);
            throw new SystemException("发送验证码失败");
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
            throw new SystemException("发送验证码失败");
        }
    }

    @Override
    public void content(String phone, String content) {
        throw new SystemException("短信发送失败，未配置腾讯短信服务");
    }
}
