package com.izouma.dingdong.service.sms;
import com.izouma.dingdong.config.Constants;
import com.izouma.dingdong.exception.BusinessException;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
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 org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.regex.Pattern;


@RunWith(SpringRunner.class)
@SpringBootTest
public class TencentSmsTest {
    @Autowired
    private TencentSmsService smsService;

    @Test
    public void test() {
        // 短信应用SDK AppID   // 1400开头
        String appid = "1400405705";
        // 短信应用SDK AppKey
        String appkey = "dd98bf6e6dc2f1f481702d1d5a303ddb";
        // 短信模板ID，需要在短信应用中申请
        String templateId = "676939";
        // 签名，使用的是`签名内容`，而不是`签名ID`
        String smsSign = "弗雷登";
        String[] phone = {"+8618205083565"};

        String[] param = {"1234"};
        try {
            SendSmsRequest req = new SendSmsRequest();
            req.setPhoneNumberSet(phone);
            req.setTemplateID(templateId);
            req.setSign(smsSign);
            req.setSmsSdkAppid(appid);
            req.setTemplateParamSet(param);

            Credential cred = new Credential("AKIDNZU9ZiEJn03rPgNNq1wbhXIo43nFpqRl", "OWZNuCNK2UorFOG5mGXYCakenjfBAsZ0");
            SmsClient client = new SmsClient(cred,"");
            client.SendSms(req);
            SendSmsResponse resp = client.SendSms(req);
            SendStatus sendStatus = resp.getSendStatusSet()[0];
            if (!sendStatus.getCode().equals("Ok")) {
                throw new BusinessException("发送失败，请稍后再试", sendStatus.getCode() + "," + sendStatus.getMessage());
            }
            System.out.println(sendStatus);

        } catch (TencentCloudSDKException e) {
            System.out.println(e.toString());
        }
    }

    @Test
    public void test1(){
      // smsService.pullSmsSend("+8613182976895");
//        System.out.println(smsService.sendVerify("+8618205083565","674851"));
        //8107015381008
        System.out.println(Pattern.matches(Constants.Regex.PHONE2, ""));

    }
}
