package com.tourism.webadmin.back.util;

import com.alibaba.fastjson.JSON;
import com.tourism.webadmin.config.SmsSwitch;
import com.volcengine.model.request.SmsSendRequest;
import com.volcengine.model.response.SmsSendResponse;
import com.volcengine.service.sms.SmsService;
import com.volcengine.service.sms.SmsServiceInfoConfig;
import com.volcengine.service.sms.impl.SmsServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class SMSUtils {

    @Autowired
    private SmsSwitch smsSwitch;

    private static SmsService smsService;

    public void sendMsg(String countryCode, String phoneNumber, String code){
        smsService = SmsServiceImpl.getInstance(new SmsServiceInfoConfig(smsSwitch.getAK(), smsSwitch.getSK()));

        SmsSendRequest req = new SmsSendRequest();
        req.setPhoneNumbers(countryCode.replaceAll("\\+", "") + phoneNumber);
        req.setSmsAccount(smsSwitch.getSmsAccount());
        if(countryCode.equals("86") || StringUtils.isBlank(countryCode)){
            req.setTemplateId(smsSwitch.getChinaTemplateId());
            req.setSign(smsSwitch.getChinaSign());
        }else {
            req.setTemplateId(smsSwitch.getOtherTemplateId());
            req.setSign(smsSwitch.getOtherSign());
        }

        Map<String,String> param = new HashMap<>();
        param.put("code", code);
        req.setTemplateParamByMap(param);

        try {
            SmsSendResponse response = smsService.sendV2(req);
            System.out.println(JSON.toJSONString(response));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
