package com.yonge.toolset.thirdparty.message.provider;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonge.toolset.base.exception.ThirdpartyException;
import com.yonge.toolset.thirdparty.message.MessageSenderPlugin;
import com.yonge.toolset.utils.http.HttpUtil;

/**
 * MO信通短信验证码接口
 */
@Service
public class MOxintongSMSPlugin implements MessageSenderPlugin, InitializingBean {

	private static ObjectMapper MAPPER = new ObjectMapper();

	@Value("${Moxintong.username:1}")
	private String username;
	
	@Value("${Moxintong.pwd:1}")
	private String pwd;
	
	@Value("${Moxintong.reqUrl:1}")
	private String reqUrl;

	public static String getName() {
		return "moxintong";
	}

	@Override
	public boolean send(String subject, String content, String receiver, String url, String jpushType,String sound,String channelId) throws IOException {
		Map<String, Object> reqParams = new HashMap<String, Object>();
		reqParams.put("username", username);
		reqParams.put("pwd", pwd);
		reqParams.put("mobile", receiver);
		reqParams.put("content", "【袋鼠卡】" + content);

		String result = "";
		try {
			result = HttpUtil.postForHttps(reqUrl, reqParams);
		} catch (Exception e) {
			throw new ThirdpartyException("短信发送失败！", e);
		}
		HashMap jsonObject = MAPPER.readValue(result, HashMap.class);
		if (jsonObject.get("code").toString().equals("0")) {
			return true;
		} else {
			throw new ThirdpartyException(jsonObject.get("msg").toString());
		}
	}

	@Override
	public boolean batchSend(String subject, String content, String[] receivers, String url, String jpushType,String sound,String channelId) throws IOException {
		StringBuilder stringBuilder = new StringBuilder();
		for (int i = 0; i < receivers.length - 1; i++) {
			stringBuilder.append(receivers[i]).append(",");
		}
		stringBuilder.append(receivers[receivers.length - 1]);

		Map<String, Object> reqParams = new HashMap<String, Object>();
		reqParams.put("username", username);
		reqParams.put("pwd", pwd);
		reqParams.put("mobile", stringBuilder.toString());
		reqParams.put("content", "【兰鲸分期】" + content);

		String result = "";
		try {
			result = HttpUtil.postForHttps(reqUrl, reqParams);
		} catch (Exception e) {
			throw new ThirdpartyException("短信发送失败！", e);
		}
		HashMap jsonObject = MAPPER.readValue(result, HashMap.class);
		if (jsonObject.get("code").toString().equals("0")) {
			return true;
		} else {
			throw new ThirdpartyException(jsonObject.get("msg").toString());
		}
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// 参数检查
		if (StringUtils.isBlank(reqUrl)) {
			throw new RuntimeException("Init parameter [reqUrl] can not blank");
		}
		if (StringUtils.isBlank(username)) {
			throw new RuntimeException("Init parameter [username] can not blank");
		}
		if (StringUtils.isBlank(pwd)) {
			throw new RuntimeException("Init parameter [pwd] can not blank");
		}
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public void setReqUrl(String reqUrl) {
		this.reqUrl = reqUrl;
	}

}
