package com.ym.mec.thirdparty.message.vendors;

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

import org.apache.commons.lang.StringUtils;
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.ym.mec.thirdparty.exception.ThirdpartyException;
import com.ym.mec.thirdparty.message.MessageSenderPlugin;
import com.ym.mec.util.http.HttpUtil;

/**
 * 示远短信验证码接口
 */
@Service
public class ShiyuanSMSPlugin implements MessageSenderPlugin, InitializingBean {
	/**
	 * 示远请求地址
	 */
	@Value("${sms.shiyuan.reqURL:1}")
	private String reqURL;
	/**
	 * 示远账号
	 */
	@Value("${sms.shiyuan.account:1}")
	private String account;
	/**
	 * 示远密码
	 */
	@Value("${sms.shiyuan.pwd:1}")
	private String pswd;

	// private boolean needstatus = true;//是否需要状态报告，需要true，不需要false
	// private String product = "";//产品ID(不用填写)
	// private String extno = "";//扩展码(不用填写)

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

	public void setReqURL(String reqURL) {
		this.reqURL = reqURL;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public void setPswd(String pswd) {
		this.pswd = pswd;
	}

	@Override
	public boolean send(String subject, String content, String receiver, String url) throws IOException {
		try {
			Map<String, Object> reqParams = new HashMap<String, Object>();
			reqParams.put("account", account);
			reqParams.put("pswd", pswd);
			reqParams.put("mobile", receiver);
			reqParams.put("msg", content);
			reqParams.put("needstatus", true);
			reqParams.put("product", "");
			reqParams.put("extno", "");
			reqParams.put("resptype", "json");
			String resultParams = HttpUtil.postForHttp(reqURL, reqParams);
			JSONObject jsonObject = JSONObject.parseObject(resultParams);
			if (jsonObject.get("result").equals("0")) {
				return true;
			}
		} catch (Exception e) {
			throw new ThirdpartyException("Failed to invoke shiyuan service", e);
		}
		return false;
	}

	@Override
	public boolean batchSend(String subject, String content, String[] receivers, String url) 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]);
		try {
			Map<String, Object> reqParams = new HashMap<>();
			reqParams.put("account", account);
			reqParams.put("pswd", pswd);
			reqParams.put("mobile", stringBuilder.toString().trim());
			reqParams.put("msg", content);
			reqParams.put("needstatus", true);
			reqParams.put("product", "");
			reqParams.put("extno", "");
			reqParams.put("resptype", "json");
			// 发起请求
			String resultParams = HttpUtil.postForHttp(reqURL, reqParams);
			JSONObject jsonObject = JSONObject.parseObject(resultParams);
			if (jsonObject.get("result").equals("0")) {
				return true;
			}
			throw new ThirdpartyException("示远科技短信验证码发送失败");
		} catch (Exception e) {
			throw new ThirdpartyException("Failed to invoke shiyuan service", e);
		}
	}

}
