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

import org.springframework.stereotype.Component;

/**
 * @author 何秀刚
 * Introduction to the ： 校验工具类
 * create date: 2016年1月12日
 */
@Component
public class ValidatorUtil2Map {
	
	/**
	 * 注解校验使用了@ValidatorInterface的实体类
	 * @param obj
	 * @return
	 */
	@SuppressWarnings(value = {"unchecked", "rawtypes"})
	public Map validatorObject(Object obj) {
		HashMap<String,Object> resultMap=new HashMap<String,Object>();
		resultMap.put("result","true");
		ArrayList al=new ArrayList();
		if (obj == null) {
			return null;
		}
		try {
			//类加载
			Class c = obj.getClass();
			// 获取c是否使用ValidatorInterface这个注解
			boolean flag = c.isAnnotationPresent(ValidatorInterface.class);
			//判断是否注解了
			if (flag) {
				return null;
			}
			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) {
						resultMap.put("result","false");
						al.add(field.getName()+"找不到匹配的值");
					}
					// 注解属性的value
					String method = "get" + captureName(field.getName());
					Method m1 = c.getDeclaredMethod(method);
					Object object1 = m1.invoke(obj);
					if (object1 == null) {
						resultMap.put("result","false");
						al.add(field.getName()+"找不到匹配的值");
					}
					// 需要匹配的值
					method = "get" + captureName(field2.getName());
					Method m2 = c.getDeclaredMethod(method);
					Object object2 = m2.invoke(obj);
					if (object2 == null) {
						resultMap.put("result","false");
						al.add(field.getName()+"找不到匹配的值");
					}
					if (!object1.equals(object2)) {
						resultMap.put("result","false");
						al.add(field.getName()+"找不到匹配的值");
					}
				}
				// 校验字段长度范围
				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) {
						resultMap.put("result","false");
						al.add(field.getName()+"长度不能为空！");
					}
					if (!(object2 instanceof String)) {
						resultMap.put("result","false");
						al.add(field.getName()+"不是字符串！");
					}
					int length = ((String) object2).length();
					if (!(length >= interface1.fieldLengthScope()[0] && length <= interface1.fieldLengthScope()[1])) {
						resultMap.put("result","false");
						al.add(field.getName()+"长度不能超过指定范围");
					}
					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) {
						resultMap.put("result","false");
						al.add(field.getName()+"不能为空");
					}
					
					Pattern pattern = Pattern.compile(interface1.validatorIsRule());
					Matcher matcher = null;
					
					if (!(object2 instanceof String)) {
						matcher = pattern.matcher(String.valueOf(object2));
					}else{
						matcher = pattern.matcher((String)object2);
					}
				
					if (!matcher.find()) {
						resultMap.put("result","false");
						al.add(field.getName()+"格式不正确，请确认");
					}
				}
				//检查是否为空
				if (!interface1.isNotNull().isEmpty()) {
					String method = "get" + captureName(field.getName());
					Method m2 = c.getDeclaredMethod(method);
					Object object2 = m2.invoke(obj);
					if (object2 == null) {
						resultMap.put("result","false");
						al.add(field.getName()+"不能为空");
					}
				}
				
			}
			resultMap.put("message",al);
			return resultMap;
		} catch (Exception e) {
			resultMap.put("result","false");
			return resultMap;
		}
	}
	
	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;
	}
}
