package com.jpsoft.shinestar.modules.common.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import java.util.Date;
import java.util.Random;


public class SMSUtil {

	public final static String REG_TEMPLATE_CODE = "SMS_49390047";
	public static final String NUMBERCHAR = "0123456789";

	private final static String APP_KEY = "24698874";
	private final static String APP_SECRET = "5f5b21d1dc93e124aa803f95d71c00b8";
	private final static String APP_URL = "https://eco.taobao.com/router/rest";
	private final static String SIGN_NAME = "小鹏管家"; //校信达
	private final static String SMS_TYPE = "normal";

	/**
	 *
	 * @param phones 手机号,多个以英文逗号","隔开
	 * @param smsTemplateCode 短信模版ID
	 * @param jsonStr 短信模板变量，传参规则{"key":"value"}，key的名字须和申请模板中的变量名一致，多个变量之间以逗号隔开。示例：针对模板“验证码${code}，您正在进行${product}身份验证，打死不要告诉别人哦！”，传参时需传入{"code":"1234","product":"alidayu"}
	 * @return
	 */
	public static MessageResult send(String phones, String smsTemplateCode, String jsonStr){

		boolean success = true;
		String msg = "发送成功";

		TaobaoClient client = new DefaultTaobaoClient(APP_URL, APP_KEY, APP_SECRET);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setSmsType(SMS_TYPE);
		req.setSmsFreeSignName(SIGN_NAME);
		if(jsonStr != null){
			req.setSmsParamString(jsonStr);
		}
		req.setRecNum(phones);
		req.setSmsTemplateCode(smsTemplateCode);
		AlibabaAliqinFcSmsNumSendResponse rsp;
		try {
			rsp = client.execute(req);

			JSONObject ret = JSONObject.fromObject(rsp.getBody());

			System.out.println("ret>>>>" + ret);

			if(ret.containsKey("error_response")){
				success = false;
				msg = ret.getJSONObject("error_response").getString("sub_msg");
			}else if(ret.containsKey("alibaba_aliqin_fc_sms_num_send_response")){

				JSONObject result = ret.getJSONObject("alibaba_aliqin_fc_sms_num_send_response").getJSONObject("result");

				if(!result.getBoolean("success")){
					msg = result.getString("msg");
				}
			}else{
				success = false;
				msg = ret.toString();
			}
		} catch (ApiException e) {
			success = false;
			msg = e.getErrMsg();
		} catch (JSONException ex) {
			success = false;
			msg = ex.getMessage();
		}

		MessageResult retMessage = new MessageResult();

		retMessage.setMessage(msg);
		retMessage.setResult(success);

		return retMessage;
	}

	/**
	 *
	 * @param phones 手机号,多个以英文逗号","隔开
	 * @param signName 签名：目前仅支持"荆鹏云平台","速乐购"两种签名
	 * @param smsTemplateCode 短信模版ID
	 * @param json 短信模板变量，传参规则{"key":"value"}，key的名字须和申请模板中的变量名一致，多个变量之间以逗号隔开。示例：针对模板“验证码${code}，您正在进行${product}身份验证，打死不要告诉别人哦！”，传参时需传入{"code":"1234","product":"alidayu"}
	 * @return
	 */
	public static MessageResult send(String phones, String signName, String smsTemplateCode, JSONObject json){

		boolean success = true;
		String msg = "发送成功";

		TaobaoClient client = new DefaultTaobaoClient(APP_URL, APP_KEY, APP_SECRET);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setSmsType(SMS_TYPE);
		req.setSmsFreeSignName(signName);
		if(json != null){
			req.setSmsParamString(json.toString());
		}
		req.setRecNum(phones);
		req.setSmsTemplateCode(smsTemplateCode);
		AlibabaAliqinFcSmsNumSendResponse rsp;
		try {
			rsp = client.execute(req);

			JSONObject ret = JSONObject.fromObject(rsp.getBody());

			System.out.println("ret>>>>" + ret);

			if(ret.containsKey("error_response")){
				success = false;
				msg = ret.getJSONObject("error_response").getString("sub_msg");
			}else if(ret.containsKey("alibaba_aliqin_fc_sms_num_send_response")){

				JSONObject result = ret.getJSONObject("alibaba_aliqin_fc_sms_num_send_response").getJSONObject("result");

				if(!result.getBoolean("success")){
					msg = result.getString("msg");
				}
			}else{
				success = false;
				msg = ret.toString();
			}
		} catch (ApiException e) {
			success = false;
			msg = e.getErrMsg();
		} catch (JSONException ex) {
			success = false;
			msg = ex.getMessage();
		}

		MessageResult retMessage = new MessageResult();

		retMessage.setMessage(msg);
		retMessage.setResult(success);

		return retMessage;
	}



	public static String generateNumberString(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < length; i++) {
			sb.append(NUMBERCHAR.charAt(random.nextInt(NUMBERCHAR.length())));
		}
		return sb.toString();
	}

	public static void main(String[] args) {
		Week week =  DateUtil.dayOfWeekEnum(new Date());


		System.out.println(week);
	}
}
