package com.slodon.b2b2c.sms;

import com.slodon.b2b2c.core.constant.SMSConst;
import com.slodon.b2b2c.core.exception.MallException;
import com.slodon.b2b2c.core.random.RandomUtil;
import com.slodon.b2b2c.core.sms.SmsRequest;
import com.slodon.b2b2c.core.sms.SmsResponse;
import com.slodon.b2b2c.sms.base.SmsSender;
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.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import com.tencentcloudapi.sms.v20190711.models.SendStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * 腾讯云短信发送
 */
@Slf4j
public class TencentSmsSender extends SmsSender {

    public TencentSmsSender(StringRedisTemplate stringRedisTemplate) {
        super(stringRedisTemplate);

    }

    /**
     * 腾讯云发送短信方法
     *
     * @param smsRequest
     * @return
     */
    public SmsResponse sendSms(SmsRequest smsRequest) {
        //获取腾讯云配置信息
        String secretId = stringRedisTemplate.opsForValue().get("notification_sms_secret");
        String secretKey = stringRedisTemplate.opsForValue().get("notification_sms_secret_key");
        String appId = stringRedisTemplate.opsForValue().get("notification_sms_app_id");
        String signName = stringRedisTemplate.opsForValue().get("notification_sms_content_signature");

        //验证码
        String verifyCode = null;
        //验证码类短信
        if (smsRequest.getSmsType() == SMSConst.VERIFY_SMS) {
            //获取验证码模板id
            String smsTplid = stringRedisTemplate.opsForValue().get("notification_sms_tplid");
            smsRequest.setTemplateId(smsTplid);
            verifyCode = RandomUtil.randomNumber(6);
            String[] templateParam = {verifyCode};
            smsRequest.setTemplateParam(templateParam);
        }
        try {
            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);

            SendSmsRequest sendSmsRequest = new SendSmsRequest();
            //暂时没有群发需求，默认一个手机号
            String[] phoneNumberSet = {"+86" + smsRequest.getMobile()};
            sendSmsRequest.setPhoneNumberSet(phoneNumberSet);

            sendSmsRequest.setSmsSdkAppid(appId);
            sendSmsRequest.setTemplateID(smsRequest.getTemplateId());
            sendSmsRequest.setTemplateParamSet(smsRequest.getTemplateParam());
            sendSmsRequest.setSign(signName);

            SendSmsResponse sendSmsResponse = client.SendSms(sendSmsRequest);
            SendStatus sendStatusSet = sendSmsResponse.getSendStatusSet()[0];
            if (!sendStatusSet.getCode().equals("Ok")) {
                log.error("[TencentSmsSender][sendSms]短信发送错误[code]:" + sendStatusSet.getCode() + "[msg]:" + sendStatusSet.getMessage());
                throw new MallException("短信发送异常，请稍后重试");
            }
            return new SmsResponse(sendStatusSet.getCode(), sendStatusSet.getMessage(), verifyCode);
        } catch (TencentCloudSDKException e) {
            log.error("[TencentSmsSender][sendSms]:", e);
            throw new MallException("发送短信失败：" + e.getMessage());
        }
    }

}
