package cn.exploring.engine.server.core.notify;

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.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.HashMap;

/*
 * 阿里云短信服务
 */
public class AliyunSmsSender implements SmsSender {
    private final Log logger = LogFactory.getLog(AliyunSmsSender.class);

    private final String product = "Dysmsapi";
    private final String domain = "dysmsapi.aliyuncs.com";
    private final String accessKeyId = "ztCPfI257OJL6Fe0";
    private final String accessKeySecret = "VpsvKMdzo1Ag05L7bWdhNadfTCfE5H";
    private final String signName = "戈十五";

    private final String templateCode1 = "SMS_183245262";  // 戈15报名成功-AB组类型通知
    private final String templateCode2 = "SMS_183240880";  // 戈15报名成功-志愿者组类型通知
    private final String templateCode3 = "SMS_183260898";  // 戈15报名成功-媒体摄影组类型通知
    private final String templateCode4 = "SMS_183265503";  // 戈15报名成功-合作伙伴组类型通知

    private final HashMap<Integer, String > templateCodeMap = new HashMap<Integer, String>(){{
        put(1, templateCode1);
        put(2, templateCode2);
        put(3, templateCode3);
        put(4, templateCode4);
    }};


    private int subType;

    public AliyunSmsSender(int subType) {
        this.subType = subType;
    }

    @Override
    public SmsResult send(String phone, String content) {
        return null;
    }

    @Override
    public SmsResult sendWithTemplate(String phone, int templateId, String[] params) {
        try {
            System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
            System.setProperty("sun.net.client.defaultReadTimeout", "10000");

            //初始化acsClient,暂不支持region化
            IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);

            //组装请求对象-具体描述见控制台-文档部分内容
            SendSmsRequest request = new SendSmsRequest();
            request.setMethod(MethodType.POST);
            //必填:待发送手机号
            request.setPhoneNumbers(phone);
            //必填:短信签名-可在短信控制台中找到
            request.setSignName(signName);
            //必填:短信模板-可在短信控制台中找到

            String templateCode = templateCodeMap.get(subType);
            request.setTemplateCode(templateCode);

            request.setTemplateParam("{\"name\":\""+params[0] +"\"}");

            //hint 此处可能会抛出异常，注意catch
            SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public int getSubType() {
        return subType;
    }

    public void setSubType(int subType) {
        this.subType = subType;
    }
}
