package com.lerx.v6.util;

import javax.servlet.http.HttpSession;

import org.springframework.context.support.ResourceBundleMessageSource;

import com.lerx.sys.obj.TimeSpanUnit;
import com.lerx.sys.util.RandomNumUtil;
import com.lerx.sys.util.SecurityUtil;
import com.lerx.sys.util.StringUtil;
import com.lerx.sys.util.TimeUtil;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.Week;

public class SysUtil {
	
	/*
	 * 根据配置文件的设置，生成验证码
	 */
	public static String vcode(ResourceBundleMessageSource messageSource) {
		/*
		 * 生成验证字符串 authStr
		 */
		int charMode; // 1纯数字 2纯字母 0混合
		String vartmp = messageSource.getMessage("vcode.sendchar.mode", null, "0", null);
		if (StringUtil.isNumber(vartmp)) {
			charMode = Integer.valueOf(vartmp);
		} else {
			charMode = 0;
		}

		int charLength; // 长度或字符数量
		vartmp = messageSource.getMessage("vcode.sendchar.length", null, "4", null);
		if (StringUtil.isNumber(vartmp)) {
			charLength = Integer.valueOf(vartmp);
		} else {
			charLength = 4;
		}

		String authStr = RandomNumUtil.getRandStr(charLength, charMode);
		
		return authStr;
	}
	
	/*
	 * 生成加密字符串
	 */
	
	public static String encrypt(String vcode,ResourceBundleMessageSource messageSource,HttpSession session) {
		String securityStr = SecurityUtil.readWords(messageSource);
		/*
		 * 根据配置文件获得允许的截止时间剩余
		 */
		String vartmp = messageSource.getMessage("vcode.sendchar.effective", null, "30m", null);
		TimeSpanUnit tsu = TimeUtil.unixLenAtFmt(vartmp);
		long endTime = System.currentTimeMillis() + tsu.getSeconds();
		
		/*
		 * 根据配置文件获得允许的截止时间剩余
		 */

		if (session.getAttribute("lastSend") != null) {
			long lastSend = (long) session.getAttribute("lastSend");
			if (lastSend > 0 && (System.currentTimeMillis() - lastSend < tsu.getSeconds())) {
				return null;
			}
		}
		
		/*
		 * 生成加密字符
		 */
		String md5 = StringUtil.md5(String.valueOf(endTime) + "|" + vcode + "|" + securityStr);
		String encryptedStr = String.valueOf(endTime) + "|" + vcode + "|" + md5;
		
		session.setAttribute("validateEnd", endTime);
		session.setAttribute("encryptedStr", encryptedStr);
		
		return encryptedStr;
	}
	
	public static String validateAss(String str,String filterWords, boolean rep, String repTarget,boolean nullfilter) {
		if (nullfilter) {
			str=StringUtil.nullAndHtmlFilter(str);
		}else {
			str=StringUtil.htmlFilter(str);
		}
		
		str=StringUtil.filterByWords(str, filterWords, rep, repTarget);
		/*if (str!=null && !str.trim().equals("")) {
			str = StringUtil.escape(str);
		}*/
		return str;
	}
	
	public static String currdate(ResourceBundleMessageSource messageSource) {
		String fmt=messageSource.getMessage("format.data.chinese", null, "yyyy-MM-dd", null);
		Week w;
		w = DateUtil.dayOfWeekEnum(TimeUtil.getCurDate());
		String ws = w.toChinese();
		ws=StringUtil.strReplace(ws, "星期", "");
		fmt=StringUtil.strReplace(fmt, "{weekday}", ws);
		fmt=StringUtil.strReplace(fmt, "{weekday0}", ""+(w.getValue()-1));
		
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(fmt);
		return formatter.format(System.currentTimeMillis());
	}
	

}
