package cn.hmsoft.frame.util;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.hmsoft.frame.exception.ValidFailException;
import cn.hmsoft.helper.RandomHelper;
import cn.hmsoft.helper.RandomHelper.RandomImageEntity;
import cn.hmsoft.helper.StringHelper;

/************************************
 * @note 随机数辅助工具
 * @author revisit@126.com
 * @Company: www.hmsoft.cn
 * @create 2017-05-07 21:57:38
 * @version 4.0.0
 */
public abstract class FrameRandomCodeUtil {
	/**************************
	 * 生成随机图片
	 */
	public static void generateRandomCode(HttpServletRequest request, HttpServletResponse response,
			RandomImageEntity entity) {
		HttpSession session = request.getSession();
		String randomString = RandomHelper.getRandomValue(entity);
		session.setAttribute(FrameParamUtil.SessionName_RandomCode, randomString);
		try {
			ImageIO.write(entity.getRandom_image(), "JPEG", response.getOutputStream());// 将内存中的图片通过流动形式输出到客户端
		} catch (Exception e) {
			//e.printStackTrace();
		}
	}

	/**************************
	 * 生成随机图片
	 */

	public static void generateRandomCode(HttpServletRequest request, HttpServletResponse response) {
		generateRandomCode(request, response, 4);
	}

	/**************************
	 * 生成随机图片
	 */
	public static void generateRandomCode(HttpServletRequest request, HttpServletResponse response, int random_length) {
		RandomImageEntity entity = new RandomImageEntity();
		entity.setRandom_length(random_length);
		generateRandomCode(request, response, entity);
	}

	/**************************
	 * 从指定的随机种子中生成随机图片
	 */

	public static void generateRandomCode(HttpServletRequest request, HttpServletResponse response,
			String random_string) {
		generateRandomCode(request, response, random_string, 4);
	}

	/**************************
	 * 从指定的随机种子中生成随机图片
	 */
	public static void generateRandomCode(HttpServletRequest request, HttpServletResponse response,
			String random_string, int random_length) {
		RandomImageEntity entity = new RandomImageEntity();
		entity.setRandom_length(random_length);
		entity.setRandom_string(random_string);
		generateRandomCode(request, response, entity);
	}

	/****************************************
	 * 验证码检核
	 * 
	 * @param request
	 * @param code
	 *            用户填写的验证码
	 * @return
	 */
	public static boolean validRandomCode(HttpServletRequest request, String random_string) {
		return validRandomCode(request, random_string, null, false);
	}

	/*******************************
	 * 验证码检核
	 * 
	 * @param request
	 * @param code
	 *            用户填写的验证码
	 * @param timeout
	 *            验证后是否将当前验证码失效
	 * @return
	 */
	public static boolean validRandomCode(HttpServletRequest request, String random_string, boolean timeout) {
		return validRandomCode(request, random_string, null, timeout);
	}

	/*******************************
	 * 验证码检核
	 * 
	 * @param request
	 * @param code
	 *            用户填写的验证码
	 * @param name
	 *            系统保存验证码的变量
	 * @return
	 */
	public static boolean validRandomCode(HttpServletRequest request, String random_string, String session_name) {
		if (StringHelper.isEmpty(random_string))
			throw new ValidFailException();
		String key = StringHelper.isEmpty(session_name) ? FrameParamUtil.SessionName_RandomCode : session_name;
		String c = request.getSession().getAttribute(key) == null ? ""
				: request.getSession().getAttribute(key).toString();
		if (random_string.equalsIgnoreCase(c))
			return true;
		else
			throw new ValidFailException();

	}

	/*********************************
	 * 验证码检核
	 * 
	 * @param request
	 * @param code
	 *            用户填写的验证码
	 * @param name
	 *            系统保存验证码的变量
	 * @param timeout
	 *            验证后是否将当前验证码失效
	 * @return
	 */
	public static boolean validRandomCode(HttpServletRequest request, String random_string, String session_name,
			boolean timeout) {
		if (StringHelper.isEmpty(random_string))
			throw new ValidFailException();
		String key = StringHelper.isEmpty(session_name) ? FrameParamUtil.SessionName_RandomCode : session_name;
		String c = request.getSession().getAttribute(key) == null ? ""
				: request.getSession().getAttribute(key).toString();
		// 如果session没有缓存，不需要验证
		if (StringHelper.isEmpty(c))
			return true;
		if (timeout)
			request.getSession().setAttribute(key, null);
		if (random_string.equalsIgnoreCase(c))
			return true;
		else
			return false;
			//throw new ValidFailException();

	}

	/********************************************************
	 * 设置验证码失效
	 */
	public static void setSessionTimeout(HttpServletRequest request) {
		setSessionTimeout(request, null);

	}

	/********************************************************
	 * 设置验证码失效
	 */
	public static void setSessionTimeout(HttpServletRequest request, String session_name) {
		String key = StringHelper.isEmpty(session_name) ? FrameParamUtil.SessionName_RandomCode : session_name;
		request.getSession().setAttribute(key, null);

	}
}
