package com.keao.edu.thirdparty.message.provider;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.keao.edu.thirdparty.exception.ThirdpartyException;
import com.keao.edu.thirdparty.message.MessageSenderPlugin;
import com.keao.edu.thirdparty.message.MessageSenderPluginContext;
import com.keao.edu.thirdparty.message.SendMode;
import com.keao.edu.util.date.DateUtil;
import com.keao.edu.util.http.HttpUtil;

/**
 * 亿美短信
 */
@Service
public class YimeiSmsPlugin implements MessageSenderPlugin, InitializingBean {

	private Logger logger = LoggerFactory.getLogger(YimeiSmsPlugin.class);

	@Value("${com.properties.sms-appId:EUCP-EMY-SMS1-DX0Y9}")
	private String appId;
	@Value("${com.properties.sms-secretKey:B0C8230D0F2ACC64}")
	private String secretKey;
	@Value("${com.properties.sms-host-dev:http://bjmtn.b2m.cn}")
	private String host;
	
	public final static String PLUGIN_NAME = "YiMei";

	@Override
	public String getName() {
		return PLUGIN_NAME;
	}

	@Override
	public SendMode getSendMode() {
		return SendMode.SMS;
	}

	private String getParam(String subject, String content, String receiver, String host) {
		try {
			HashMap<String, Object> param = new HashMap<String, Object>();
			param.put("appId", appId);
			String timestamp = DateUtil.format(new Date(), new SimpleDateFormat("yyyyMMddHHmmss"));
			param.put("timestamp", timestamp);
			param.put("sign", DigestUtils.md5Hex(appId + secretKey + timestamp));
			param.put("content", content);
			param.put("mobiles", receiver);

			String result = HttpUtil.postForHttp(host, param);

			return result;
		} catch (Exception e) {
			throw new ThirdpartyException("调用发送短信接口出现异常", e);
		}
	}

	@Override
	public boolean send(String subject, String content, String receiver, String url, String jpushType) throws Exception {
		String result = getParam(subject, content, receiver, host + "/simpleinter/sendSMS");
		JSONObject json = JSONObject.parseObject(result);
		if ("SUCCESS".equals(json.get("code"))) {

			return true;
		} else {
			throw new Exception(json.getString("code"));
		}
	}

	@Override
	public boolean batchSend(String subject, String content, String[] receivers, String url, String jpushType) throws Exception {
		String join = StringUtils.join(receivers, ",");
		String result = getParam(subject, content, join, host + "/simpleinter/sendSMS");
		logger.info("调用短信接口返回:{}", result);
		JSONObject json = JSONObject.parseObject(result);
		if ("SUCCESS".equals(json.get("code"))) {
			return true;
		} else {
			throw new Exception(json.getString("code"));
		}
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		MessageSenderPluginContext.addMessageSender(this);
	}
}
