/**
 * 
 */
package org.springblade.message.sms;

import org.springblade.message.model.MsgResponse;
import org.springblade.message.model.SmsInfo;
import org.springblade.message.model.SmsMsgRequest;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson2.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySmsTemplateResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wookvn
 *
 */
@Slf4j
@Component
public class AliyunSmsTemplate extends SmsMsgTemplate {
	
	private static final String OK = "ok";

	@Override
	public MsgResponse sendMessage(SmsMsgRequest smsMsgRequest) {
		SmsInfo smsInfo = smsMsgRequest.getSmsInfo();
		SendSmsRequest request = new SendSmsRequest();
		request.setPhoneNumbers(String.join(",", smsMsgRequest.getPhones()));
		request.setSignName(smsInfo.getSignName());
		request.setTemplateCode(smsInfo.getTemplateId());
		request.setSysRegionId(smsInfo.getRegionId());
		request.setTemplateParam(JSON.toJSONString(smsMsgRequest.getParams()));
		try {
			IClientProfile profile = DefaultProfile.getProfile(smsInfo.getRegionId(), smsInfo.getAccessKey(), smsInfo.getSecretKey());
			IAcsClient acsClient = new DefaultAcsClient(profile);
			SendSmsResponse response = acsClient.getAcsResponse(request);
			if (response != null) {
				String code = response.getCode();
				if(code.equalsIgnoreCase(OK)) {
					return MsgResponse.success(200, "发送成功");
				}else {
					return MsgResponse.failure(response.getMessage());
				}
			}
			return MsgResponse.failure("发送失败");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return MsgResponse.failure(e.getMessage());
		}
	}

	@Override
	public MsgResponse queryTemplateContent(SmsInfo smsInfo) {
		QuerySmsTemplateRequest request = new QuerySmsTemplateRequest();
        request.setTemplateCode(smsInfo.getTemplateId());
        
		try {
			IClientProfile profile = DefaultProfile.getProfile(smsInfo.getRegionId(), smsInfo.getAccessKey(), smsInfo.getSecretKey());
			IAcsClient acsClient = new DefaultAcsClient(profile);
			QuerySmsTemplateResponse response = acsClient.getAcsResponse(request);
			if (response != null) {
				String code = response.getCode();
				Integer templateStatus = response.getTemplateStatus();
				if(code.equalsIgnoreCase(OK) && templateStatus == 1) { //0：审核中。 1：审核通过。 2：审核未通过，请在返回参数
					return MsgResponse.success(200, response.getTemplateContent());
				}else {
					return MsgResponse.failure(response.getReason());
				}
			}
			return MsgResponse.failure("未查询到该模版");
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return MsgResponse.failure(e.getMessage());
		}
	}
	
	public static void main(String[] args) {
		QuerySmsTemplateRequest request = new QuerySmsTemplateRequest();
        request.setTemplateCode("SMS_268701016");
        
		try {
//			IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tLfu7nYMrkiid7hhqN8", "GYxltm6oKpTpbU1mKWeXfZEgXqyXcT");
//			IAcsClient acsClient = new DefaultAcsClient(profile);
//			QuerySmsTemplateResponse response = acsClient.getAcsResponse(request);
//			System.out.println(JSON.toJSONString(response));
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}
	}
}
