/**
 * 
 */
package org.springblade.common.unciom;

import java.util.HashMap;
import java.util.Map;

import org.springblade.core.tool.utils.Func;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.lang.Tuple;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.symmetric.AES;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wookvn
 *
 */
@Slf4j
public final class UnicomUtil {
	final static String SMS_CPID = "changshi";
	final static String SMS_CHANNEL = "byyzmsend";
	final static String SMS_PWD = "aKd!3gwe";
	//旧接口地址：http://csapi.mige.tv:809/videoif/sendSmsCode.do
	final static String SMS_SEND_URL = "http://172.16.1.81:809/videoif/sendSmsCode.do";
//	final static String SMS_SEND_URL = "http://testapi.mige.tv/videoif/sendSmsCode.do";
	//旧接口地址：http://csapi.mige.tv:809/videoif/smsNumber.do
	final static String SMS_CHECK_URL = "http://172.16.1.81:809/videoif/smsNumber.do";
//	final static String SMS_CHECK_URL = "http://testapi.mige.tv/videoif/smsNumber.do";
	final static byte[] USER_ID_AES_KEY_BYTES = "9b58bdc9a3c4410eb0f93da01c5f0730".getBytes();
	
	/**
	 * 
	 * @param userId
	 * @return
	 */
	public static String encryptAes(Long userId, String mobile) {
		AES userIdAes = new AES(USER_ID_AES_KEY_BYTES);
		return userIdAes.encryptBase64(userId.toString() + "," + mobile);
	}
	
	/**
	 * 
	 * @param userIdSign
	 * @return
	 */
	public static Tuple decryptAes(String userIdSign) {
		AES userIdAes = new AES(USER_ID_AES_KEY_BYTES);
		String decryptStr = userIdAes.decryptStr(userIdSign);
		String[] strArr = decryptStr.split(",");
		return new Tuple(Func.toLong(strArr[0], 0L), strArr[1]);
	}
	
	/**
	 * 发送短信 
	 * 验证码有效时间为1分钟，1分钟内不会重新发送短信。验证码为6位数字。
	 * @param mobile
	 * @return 剩余多少秒
	 */
	public static String sendSms(String mobile) {
		Map<String, Object> paramMap = new HashMap<>(); 
		paramMap.put("cpid", SMS_CPID);
		paramMap.put("userid", MobileDesUtil.urlEncode(mobile, SMS_PWD));
		paramMap.put("apptype", 2);
		paramMap.put("spid", "");
		paramMap.put("channel", SMS_CHANNEL);
		paramMap.put("entype", 1);
		
		//{resultcode: "0", errorinfo: "", remaintime: 60}
		//remaintime 单位秒，离上次发送短信间隔时间，如果无或者大于60秒，则返回60
		//resultcode 0-验证码发送成功
		String res = HttpUtil.get(SMS_SEND_URL, paramMap, 10000);
		log.info("sendSmsCode|mobile={},resp={}", mobile, res);
		JSONObject jsonObject = JSON.parseObject(res);
		return "0".equals(jsonObject.getString("resultcode")) ? null : jsonObject.getString("errorinfo");
	}
	
	/**
	 * 验证验证码正确性
	 * @param mobile
	 * @param verCode
	 * @return
	 * @throws Exception 
	 */
	public static boolean checkSmsCode(String mobile, String verCode) {
		Map<String, Object> paramMap = new HashMap<>(); 
		paramMap.put("cpid", SMS_CPID);
		paramMap.put("userid", MobileDesUtil.urlEncode(mobile, SMS_PWD));
		paramMap.put("apptype", 2);
		paramMap.put("channel", SMS_CHANNEL);
		paramMap.put("vcode", verCode);
		paramMap.put("entype", 1);
		//{resultcode: "0", errorinfo: "", userid: "", token: ""}
		//userid 手机伪码des加密后的
		//resultcode 0-验证码发送成功
		String res = HttpUtil.get(SMS_CHECK_URL, paramMap, 10000);
		log.info("checkSmsCode|mobile={},resp={}", mobile, res);
		JSONObject jsonObject = JSON.parseObject(res);
		try {
			return "0".equals(jsonObject.getString("resultcode")) 
					&& StrUtil.isNotEmpty(jsonObject.getString("userid"))
					&& mobile.equals(MobileDesUtil.decode(jsonObject.getString("userid"), SMS_PWD));
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
