package com.iamberry.wechat.tools;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.stereotype.Component;

/**
 * 校验工具类
 * @company	深圳爱贝源科技有限公司
 * @website www.iamberry.com
 * @author 	献
 * @tel		18271840547
 * @date	2017年2月22日
 */
@Component
public class ValidatorUtil {
	
	/**
	 * 注解校验使用了@ValidatorInterface的实体类
	 * @param obj
	 * @return
	 */
	@SuppressWarnings(value = {"unchecked", "rawtypes"})
	public boolean validatorObject(Object obj) {
		if (obj == null) {
			return false;
		}
		try {
			//类加载
			Class c = obj.getClass();
			// 获取c是否使用ValidatorInterface这个注解
			boolean flag = c.isAnnotationPresent(ValidatorInterface.class);
			//判断是否注解了
			if (flag) {
				return false;
			}
			Field[] fields = c.getDeclaredFields();
			for (Field field : fields) {
				ValidatorInterface interface1 = field.getAnnotation(ValidatorInterface.class);
				if (interface1 == null) {
					continue;
				}
				if (!interface1.isEnable()) {
					continue;
				}
				// 校验一个字段和其他字段
				if (!interface1.equalsOtherField().isEmpty()) {
					Field field2 = c.getDeclaredField(interface1.equalsOtherField());
					if (field2 == null) {
						return false;
					}
					// 注解属性的value
					String method = "get" + captureName(field.getName());
					Method m1 = c.getDeclaredMethod(method);
					Object object1 = m1.invoke(obj);
					if (object1 == null) {
						return false;
					}
					// 需要匹配的值
					method = "get" + captureName(field2.getName());
					Method m2 = c.getDeclaredMethod(method);
					Object object2 = m2.invoke(obj);
					if (object2 == null) {
						return false;
					}
					if (!object1.equals(object2)) {
						return false;
					}
				}
				// 校验字段长度范围
				if (interface1.fieldLengthScope()[0] != interface1.fieldLengthScope()[1]) {
					String method = "get" + captureName(field.getName());
					Method m2 = c.getDeclaredMethod(method);
					Object object2 = m2.invoke(obj);
					if (object2 == null) {
						return false;
					}
					if (!(object2 instanceof String)) {
						return false;
					}
					int length = ((String) object2).length();
					if (!(length >= interface1.fieldLengthScope()[0] && length <= interface1.fieldLengthScope()[1])) {
						return false;
					}
					System.out.println("true");
				}
				// 需要根据正则校验字段
				if (!interface1.validatorIsRule().isEmpty()) {
					String method = "get" + captureName(field.getName());
					Method m2 = c.getDeclaredMethod(method);
					Object object2 = m2.invoke(obj);
					if (object2 == null) {
						return false;
					}
					if (!(object2 instanceof String)) {
						return false;
					}
					Pattern pattern = Pattern.compile(interface1.validatorIsRule());
					Matcher matcher = pattern.matcher((String)object2);
					if (!matcher.find()) {
						return false;
					}
				}
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	public String captureName(String name) {
		if (!Character.isLowerCase(name.charAt(0))) {
			return name;
		}
		char[] cs=name.toCharArray();
		cs[0]-=32;
		return String.valueOf(cs);
	}

	public static boolean isEmptyIgnoreBlank(String str){
		if(str == null || "".equals(str) || "".equals(str.trim()))
			return true;
		return false;
	}
}
