package com.yxcheng.token.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class ValidatorUtil {
//	private static ILog logger = LoggerFactory.getLogger(ValidatorUtil.class);

	public static boolean validatorReg(String reg, String value) {
		Pattern pattern = Pattern.compile(reg);
		Matcher matcher = pattern.matcher(value);
		return matcher.matches();
	}

	public static boolean isNumOrEng(String value) {
		if (value == null || value.isEmpty()) {
			return true;
		} else {
			return validatorReg("^[a-zA-Z0-9]+$", value);
		}
	}
	//校验金额11.11
	public static boolean isMoneyNum(String value) {
		if (value == null || value.isEmpty()) {
			return true;
		} else {
			return validatorReg("^[1-9][0-9]+$", value);
		}
	}

	public static boolean isContainsChinese(String str) {
		Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]");
		Matcher matcher = pattern.matcher(str);
		boolean flg = false;
		if (matcher.find()) {
			flg = true;
		}
		return flg;
	}

	public static void main(String args[]){
		System.out.print(isConSpeCharacters("12345***6*7AASDF8","*"));
	}
	/**
	 * 功能：判断一个字符串是否包含除strs外的特殊字符
	 * 
	 * @param value
	 *            要判断的字符串
	 * @param strs
	 *            要额外排除的字符
	 * @return true 提供的参数value包含特殊字符
	 * @return false 提供的参数value不包含特殊字符 by liuxk
	 */
	public static boolean isConSpeCharacters(String value, String... strs) {
		if (value == null)
			return false;
		String res = value.replaceAll("[\u4e00-\u9fa5]*[a-z]*[A-Z]*\\d*", "");
		for (String str : strs) {
			for (char ch : str.toCharArray()) {
				res = replace(res, String.valueOf(ch), "");
			}
		}
		return res.length() != 0;
	}

	public static boolean isConSpeCharacters2(String value) {
		if (value == null)
			return false;
		String regStrs = "∏¥§℅€℃£℉№℡‰$¢∮※？?<>[]'&";
		for (char ch : regStrs.toCharArray()) {
			if (value.indexOf(String.valueOf(ch)) != -1) // 等于-1表示这个字符串中没有o这个字符
				return true;
		}
		return false;
	}

	public static boolean isConSpeCharacters3(String value) {
		return isConSpeCharacters2(value)||isConSpeCharacters(value, "()（）,.;:+=_%$#@!~*{}/^-\\|\"，。？：");
	}

	// static final String regEmail =
	// "^[A-Za-z0-9\\u4e00-\\u9fa5_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";

	// 邮件格式校验，字符串为空返回true
	public static boolean isEmail(String value) {
		if (value == null || value.isEmpty()) {
			return true;
		} else {
			if (value.indexOf("@") != -1)
				return true;
			else
				return false;
			// return validatorReg(regEmail, value);
		}
	}

	static final String regPhone = "\\d{3,4}-\\d{7,8}";
	static final String regMobile = "^[1][3456789][0-9]{9}$";

	// 联系方式格式校验，字符串为空返回true.type:1 固话或手机,type:2 固话,type:3 手机
	public static boolean isPhoneNum(String value, int type) {
		if (value == null || value.isEmpty()) {
			return true;
		} else {
			if (type == 1)
				return validatorReg(regPhone, value) || validatorReg(regMobile, value);
			else if (type == 2)
				return validatorReg(regPhone, value);
			else
				return validatorReg(regMobile, value);
		}
	}

	/**
	 * 
	 * @param source
	 *            需要处理的字符串
	 * @param oldString
	 *            需要被替换的字符
	 * @param newString
	 *            替换后的字符
	 * @return 比如 需要替换字符串中的? String str = "Who am I ?"; replace(str,"?","%3F");
	 */
	public static String replace(String source, String oldString, String newString) {
		StringBuffer output = new StringBuffer();
		int lengthOfSource = source.length();
		int lengthOfOld = oldString.length();
		int posStart = 0;
		int pos;
		while ((pos = source.indexOf(oldString, posStart)) >= 0) {
			output.append(source.substring(posStart, pos));
			output.append(newString);
			posStart = pos + lengthOfOld;
		}
		if (posStart < lengthOfSource) {
			output.append(source.substring(posStart));
		}
		return output.toString();
	}

	public static boolean isNull(Object obj) {
		boolean isNullFlag = true;
		if (obj != null)
			if (obj instanceof List)
				isNullFlag = isNull((List<?>) obj);
			else if (obj instanceof Set)
				isNullFlag = isNull((Set<?>) obj);
			else if (obj instanceof Object[])
				isNullFlag = isNull((Object[]) (Object[]) obj);
			else if (obj instanceof Map)
				isNullFlag = isNull((Map<?, ?>) obj);
			else if (obj instanceof String)
				isNullFlag = isNull((String) obj);
			else
				isNullFlag = false;

		return isNullFlag;
	}

	public static boolean isNull(List<?> list) {
		return ((list == null) || (list.size() == 0));
	}

	public static boolean isNull(Set<?> set) {
		return ((set == null) || (set.size() == 0));
	}

	public static boolean isNull(Object[] objects) {
		return ((objects == null) || (objects.length == 0));
	}

	public static boolean isNull(Map<?, ?> map) {
		return ((map == null) || (map.isEmpty()));
	}

	public static boolean isNull(String str) {
		return ((str == null) || ("".equals(str.trim())) || ("null".equalsIgnoreCase(str.trim())));
	}
	
	public static boolean isNotNull(Object obj) {
		return !isNull(obj);
	}

	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}
	/**
	 * 判断传入参数是否为1-15位整数，或1-15位整数加两位小数
	 * @return
	 */
	public static boolean isMoneyNum1(String str){
		Pattern pattern = Pattern.compile("^[-]{0,1}[1-9]{0,1}[0-9]{0,14}$|^[-]{0,1}[1-9]{0,1}[0-9]{0,14}[.][0-9]{2}$");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	/*
	 * 判断是否为整数
	 * 
	 * @param str 传入的字符串
	 * 
	 * @return 是整数返回true,否则返回false
	 */
	public static boolean isInteger(String str) {
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
		return pattern.matcher(str).matches();
	}
	
	
	/*
	 * 判断是否为正整数
	 * 
	 * @param str 传入的字符串
	 * 
	 * @return 是整数返回true,否则返回false
	 */
	public static boolean isPositiveInteger(String str) {
		Pattern pattern = Pattern.compile("^[0-9]*[1-9][0-9]*$");
		return pattern.matcher(str).matches();
	}

	/**
	 * 校验是否是数字
	 * @param value
	 * @return
	 */
	public static boolean isNum(String value){
		Pattern pattern = Pattern.compile("^[0-9]*$");
		return pattern.matcher(value).matches();
	}

	/***
	 * 校验名字
	 * @param value
	 * @return
	 */
	public static boolean checkName(String value){
		Pattern pattern = Pattern.compile("^[\\u4e00-\\u9fa5.]+$");
		return pattern.matcher(value).matches();
	}

	/**
	 * 校验邮箱格式
	 * @param value
	 * @return
	 */
	public static boolean checkEmail(String value){
		Pattern pattern = Pattern.compile("^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$");
		return pattern.matcher(value).matches();
	}
	/**
	 * 校验是否是字符和数字组成
	 * @param value
	 * @return
	 */
	public static boolean isNumAndChar(String value){
		Pattern pattern = Pattern.compile("^[a-zA-Z0-9]+$");
		return pattern.matcher(value).matches();
	}

	/***
	 * 判断该字符串是否包含中文和字母外的字符，true代表好含有其他的，false表示不含有
	 * @param value
	 * @return
	 */
	public static  boolean isNotCH(String value){
		String reg="^[\\u4e00-\\u9fa5a-zA-Z()]+$";
		final Pattern compile = Pattern.compile(reg);
		final Matcher matcher = compile.matcher(value);
		if(!matcher.matches()){
			return true;
		}
		return false;

	}
	/**
	 * 校验文件流是否大于给的长度
	 *
	 * @author ChengLuchao
	 * @param zzwjStr
	 * @return String 如果rtnXml == null,则校验成功;如果rtnXml != null,则rtnXml=失败原因;
	 */
	public static boolean wjlCheck(String zzwjStr,int length) {
		if (!ValidatorUtil.isNull(zzwjStr)) {
			if (zzwjStr.length() > length) {
				return false;
			}
		}
		return true;
	}

	/**校验日期字符串
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static  boolean checkDate(String date,String pattern){
		final SimpleDateFormat sf = new SimpleDateFormat(pattern);
		try {
			sf.parse(date);
			return true;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return false;
	}
}
