package cn.hmsoft.frame.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import cn.hmsoft.exception.web.LoginTimeoutException;
import cn.hmsoft.exception.web.MvcParamException;
import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.frame.exception.AdminAuthException;
import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.helper.StringHelper;

/****************
 * 助手
 * 
 * @author: YeFeng
 * @date: 2015-10-19 22:07:19
 * @version: 1.1.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */
public abstract class FrameAssertUtil {
	/**********************
	 * 必须为空
	 */
	public static void isEmpty(String value, String msg) {
		if (StringHelper.isNotEmpty(value))
			throw new BusinessException(msg);
	}

	public static void isEmpty(List<?> list, String msg) {
		if (list != null && list.size() > 0)
			throw new BusinessException(msg);
	}

	/**********************
	 * 不允许为空
	 */
	public static void isNotEmpty(String value, String msg) {
		if (StringHelper.isEmpty(value))
			throw new BusinessException(msg);
	}

	/**********************
	 * 不允许为空
	 */
	public static void isNotNull(Object value, String msg) {
		if (value == null)
			throw new BusinessException(msg);
	}

	
	/**********************
	 * 不允许为空,基础数据错误，直接踢
	 */
	public static void isNotNull(Object value) {
		if (value == null)
			throw new LoginTimeoutException();
	}

	/**********************
	 * 必须为空
	 */
	public static void isNull(Object value, String msg) {
		if (value != null)
			throw new BusinessException(msg);
	}

	/**********************
	 * 必须不相等
	 */
	public static void isNotEqual(Integer k, int v, String msg) {
		if (k == null || k == v)
			throw new BusinessException(msg);
	}

	/**********************
	 * 必须不相等
	 */
	public static void isNotEqual(String k, String v, String msg) {
		if (k != null && k.equals(v))
			throw new BusinessException(msg);
	}

	/**********************
	 * 必须不相等
	 */
	public static void isEqual(String k, String v, String msg) {
		if (k == null || !k.equals(v))
			throw new BusinessException(msg);
	}

	/**********************
	 * 必须相等
	 */
	public static void isEqual(Integer k, int v, String msg) {
		if (k == null || k - v != 0)
			throw new BusinessException(msg);
	}

	/**********************
	 * 必须不相等
	 */
	public static void moreThen(Integer k, String msg) {
		if (k == null || k <= 0)
			throw new BusinessException(msg);
	}

	public static void moreThen(Integer k, int v, String msg) {
		if (k == null || k <= v)
			throw new BusinessException(msg);
	}

	/**********************
	 * 必须不相等
	 */
	public static void lessThen(Integer k, int v, String msg) {
		if (k == null || k >= v)
			throw new BusinessException(msg);
	}

	/*************************
	 * 判断是不是超级管理员
	 */
	public static void adminOptr(FrameOptr optr) {
		if (optr == null)
			throw new LoginTimeoutException();
		if (optr.getOptr_id() == null || optr.getOptr_id() - 0 != 0)
			throw new AdminAuthException();
	}

	/*************************
	 * 必须有记录
	 */
	public static void hasValue(List<?> list, String msg) {
		if (list == null || list.size() == 0)
			throw new BusinessException(msg);
	}

	/*************************
	 * 必须有记录
	 */
	public static void hasValue(Object[] array, String msg) {
		if (array == null || array.length == 0)
			throw new BusinessException(msg);
	}

	/*************************
	 * 必须有记录
	 */
	public static void hasOneValue(List<?> list, String msg) {
		if (list == null || list.size() != 1)
			throw new BusinessException(msg);
	}

	/*************************
	 * 必须有记录
	 */
	public static void hasValue(Map<?, ?> map, Object key, String msg) {
		if (map == null || !map.containsKey(key))
			throw new BusinessException(msg);
	}

	/*************************
	 * 必须有记录
	 */
	public static void hasValue(Map<?, ?> map, String msg) {
		if (map == null || map.keySet().size() == 0)
			throw new BusinessException(msg);
	}

	/*************************
	 * 判断是否提交了参数
	 */
	public static void hasValue(String name) {
		if (StringHelper.isEmpty(name))
			throw new MvcParamException();
	}

	/*************************
	 * 提交的配置类参数在允许的范围内
	 */
	public static void checkConfigName(String config_name, String config_value) {
		if (StringHelper.isEmpty(config_name) || config_value == null)
			throw new MvcParamException();
		// TODO 从字典表查询过滤
		// String config = FrameConfigUtil.getFrameConfigText(config_name,
		// config_value);
		// if (config == null)
		// throw new BusinessException("提交的参数不在允许的范围内，");
	}

	public static void checkParam(String... args) {
		for (String s : args) {
			if (StringHelper.isEmpty(s))
				throw new MvcParamException();
		}
	}

	public static void checkParam(Object... args) {
		for (Object s : args) {
			if (s == null)
				throw new MvcParamException();
		}
	}

	public static void moreThen(LocalDateTime date, String msg) {
		moreThen(date, LocalDateTime.now(), msg);
	}

	public static void moreThen(LocalDate date, String msg) {
		moreThen(date, LocalDate.now(), msg);
	}

	public static void moreThen(LocalDateTime date, LocalDateTime now, String msg) {
		if (date == null || now == null || date.isBefore(now))
			throw new BusinessException(msg);
	}

	public static void moreThen(LocalDate date, LocalDate now, String msg) {
		if (date == null || now == null || date.isBefore(now))
			throw new BusinessException(msg);
	}

	public static void lessThen(LocalDate date, String msg) {
		lessThen(date, LocalDate.now(), msg);
	}

	public static void lessThen(LocalDateTime date, String msg) {
		lessThen(date, LocalDateTime.now(), msg);
	}

	public static void lessThen(LocalDateTime date, LocalDateTime now, String msg) {
		if (date == null || now == null || date.isAfter(now))
			throw new BusinessException(msg);
	}

	public static void lessThen(LocalDate now, LocalDate date, String msg) {
		if (date == null || now == null || date.isAfter(now))
			throw new BusinessException(msg);
	}

	public static boolean verifyRegex(String regex, String value) {
		return verifyRegex(regex, value, "[" + value + "]不符合规则");
	}

	public static boolean verifyRegex(String regex, String value, String msg) {
		if (Pattern.compile(regex).matcher(value).matches())
			return true;
		throw new BusinessException(msg);
	}
}
