package com.njcn.cloud.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.njcn.cloud.config.Message;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.user.enums.MessageEnum;
import com.njcn.user.enums.UserResponseEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.context.annotation.Configuration;

/**
 * 类的介绍：
 *
 * @author xuyang
 * @version 1.0.0
 * @createTime 2023/8/25 13:51
 */
@Data
@Configuration
@AllArgsConstructor
public class SmsUtil {

    private final Message message;

    /**
     * 短信API产品名称（短信产品名固定，无需修改）
     */
    private static final String PRODUCT = "Dysmsapi";

    /**
     * 短信API产品域名（接口地址固定，无需修改）
     */
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";

    /**
     * 区域
     */
    private static final String AREA = "cn-hangzhou";

    /**
     * 超时时间参数
     */
    private static final String DEFAULTCONNECTTIMEOUT = "sun.net.client.defaultConnectTimeout";

    /**
     * 过期时间
     */
    private static final String DEFAULTREADTIMEOUT = "sun.net.client.defaultReadTimeout";


    public static String getMessageTemplate(String type) {
        String msgTemplate = null;
        switch (type) {
            case "0":
                msgTemplate = MessageEnum.getTemplateByCode(0);
                break;
            case "1":
                msgTemplate = MessageEnum.getTemplateByCode(1);
                break;
            case "2":
                msgTemplate = MessageEnum.getTemplateByCode(2);
                break;
            case "3":
                msgTemplate = MessageEnum.getTemplateByCode(3);
                break;
            case "4":
                msgTemplate = MessageEnum.getTemplateByCode(4);
                break;
            case "5":
                msgTemplate = MessageEnum.getTemplateByCode(5);
                break;
            case "6":
                msgTemplate = MessageEnum.getTemplateByCode(6);
                break;
            case "7":
                msgTemplate = MessageEnum.getTemplateByCode(7);
                break;
            case "8":
                msgTemplate = MessageEnum.getTemplateByCode(8);
                break;
            default:
                throw new BusinessException(UserResponseEnum.CODE_TYPE_ERROR);
        }
        return msgTemplate;
    }

    /**
     * 发送消息
     * @param phone     手机号
     * @param template  模板号
     * @param smsValue     短信模板中定义的参数名称
     * @return          sendSmsResponse 发送消息结果
     * @throws ClientException
     */
    public SendSmsResponse sendSms(String phone, String template,String smsValue) throws ClientException {
        System.setProperty(DEFAULTCONNECTTIMEOUT, message.getTime());
        System.setProperty(DEFAULTREADTIMEOUT, message.getTime());
        //初始化ascClient,暂时不支持多region（请勿修改）
        IClientProfile profile = DefaultProfile.getProfile(AREA, message.getAccessKeyId(), message.getAccessKeySecret());
        DefaultProfile.addEndpoint(AREA, AREA, PRODUCT, DOMAIN);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setMethod(MethodType.POST);
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(message.getAutoGraph());
        //必填:短信模板-可在短信控制台中找到，发送国际/港澳台消息时，请使用国际/港澳台短信模版
        request.setTemplateCode(template);
        request.setTemplateParam(smsValue);
        //请求失败这里会抛ClientException异常
        return acsClient.getAcsResponse(request);
    }

}
