package com.leanwo.activiti.dto.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonIgnore;

/**
 * 工作流参数
 * @author YangZhiJie
 *
 */
public class WorkflowParam {
	/**
	 * String 类型的参数
	 */
	private HashMap<String, String> stringParams;
	
	/**
	 * Long 类型的参数
	 */
	private HashMap<String, Long> longParams;
	
	/**
	 * Integer 类型的参数
	 */
	private HashMap<String, Integer> integerParams;
	

	/**
	 * Collection<String> 类型的参数
	 */
	private HashMap<String, ArrayList<String>> stringListParams;
	
	/**
	 * Collection<Long> 类型的参数
	 */
	private HashMap<String, ArrayList<Long>> longListParams;
	
	/**
	 * Collection<Integer> 类型的参数
	 */
	private HashMap<String, ArrayList<Integer>> integerListParams;

	public HashMap<String, String> getStringParams() {
		return stringParams;
	}

	public void setStringParams(HashMap<String, String> stringParams) {
		this.stringParams = stringParams;
	}

	public HashMap<String, Long> getLongParams() {
		return longParams;
	}

	public void setLongParams(HashMap<String, Long> longParams) {
		this.longParams = longParams;
	}

	public HashMap<String, Integer> getIntegerParams() {
		return integerParams;
	}

	public void setIntegerParams(HashMap<String, Integer> integerParams) {
		this.integerParams = integerParams;
	}
	
	public HashMap<String, ArrayList<String>> getStringListParams() {
		return stringListParams;
	}

	public void setStringListParams(HashMap<String, ArrayList<String>> stringListParams) {
		this.stringListParams = stringListParams;
	}

	public HashMap<String, ArrayList<Long>> getLongListParams() {
		return longListParams;
	}

	public void setLongListParams(HashMap<String, ArrayList<Long>> longListParams) {
		this.longListParams = longListParams;
	}

	public HashMap<String, ArrayList<Integer>> getIntegerListParams() {
		return integerListParams;
	}

	public void setIntegerListParams(HashMap<String, ArrayList<Integer>> integerListParams) {
		this.integerListParams = integerListParams;
	}

	public void addParam(String key, Object value) {
		if(key == null || value == null) {
			return;
		}

		if(value instanceof String) {
			if(stringParams == null) {
				stringParams = new HashMap<>();
			}
			stringParams.put(key, (String)value);
		}
		
		if(value instanceof Long) {
			if(longParams == null) {
				longParams = new HashMap<>();
			}
			longParams.put(key, (Long)value);
		}
		
		if(value instanceof Integer) {
			if(integerParams == null) {
				integerParams = new HashMap<>();
			}
			integerParams.put(key, (Integer)value);
		}
		
		if(isStringListType(value)) {
			if(stringListParams == null) {
				stringListParams = new HashMap<>();
			}
			stringListParams.put(key, (ArrayList<String>)value);
		}

		if(isLongListType(value)) {
			if(longListParams == null) {
				longListParams = new HashMap<>();
			}
			longListParams.put(key, (ArrayList<Long>)value);
		}

		if(isIntegerListType(value)) {
			if(integerListParams == null) {
				integerListParams = new HashMap<>();
			}
			integerListParams.put(key, (ArrayList<Integer>)value);
		}
	}
	
	/**
	 * 是否是List<String>类型
	 * @param object
	 * @return
	 */
	private boolean isStringListType(Object object) {
		if(object instanceof List) {
			List list = (List) object;
			for(Object item : list) {
				if(item instanceof String) {
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * 是否是List<Long>类型
	 * @param object
	 * @return
	 */
	private boolean isLongListType(Object object) {
		if(object instanceof List) {
			List list = (List) object;
			for(Object item : list) {
				if(item instanceof Long) {
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * 是否是List<Integer>类型
	 * @param object
	 * @return
	 */
	private boolean isIntegerListType(Object object) {
		if(object instanceof List) {
			List list = (List) object;
			for(Object item : list) {
				if(item instanceof Integer) {
					return true;
				}
			}
		}
		return false;
	}
	
	@JsonIgnore
	public HashMap<String, Object> findHashMap(){
		HashMap<String, Object> params = new HashMap<String, Object>();
		
		fillParam(params, getStringParams());
		fillParam(params, getLongParams());
		fillParam(params, getIntegerParams());
		fillParam(params, getStringListParams());
		fillParam(params, getLongListParams());
		fillParam(params, getIntegerListParams());
		
		return params;
	}

	/**
	 * 填充报表参数
	 * @param params
	 * @param map
	 */
	private void fillParam(HashMap<String, Object> params, Map map) {
		if(params == null || map == null) {
			return;
		}
		for(Object key : map.keySet()){
			if(key != null){
				params.put((String)key, map.get(key));
			}				
		}
	}
}
