package nlib.sch.util;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import egovframework.rte.psl.dataaccess.util.CamelUtil;

/**
 * @Class Name : DataMap.java
 * @author Kdmistrator
 * @version 1.0
 * DB 리턴 받을때 사용하거나 Map 타입으로 사용할때 사용한다.
 */
public class DataMap extends LinkedHashMap<String, Object> implements Map<String, Object>{
	private static final long serialVersionUID = 1L;
	private static final Logger LOGGER = LoggerFactory.getLogger(DataMap.class);

	public static DataMap getNewDataMap(){
		return new DataMap();
	}
	public static DataMap getNewDataMap(HttpServletRequest request){
		DataMap map = new DataMap();
		map.defaultSetInfo(request, map);
		return map;
	}
	private void defaultSetInfo(HttpServletRequest request, DataMap map){
		map.setString("contentType",request.getContentType());
		map.setString("contextPath",request.getContextPath());
		map.setString("uri",request.getRequestURI());
		map.setString("protocol",request.getProtocol());
		map.setString("ip", NlibCmmUtil.getIpAddress(request));
	}
	@SuppressWarnings("unchecked")
	private static DataMap makePrameterMap(HttpServletRequest request){
		DataMap resultMap = new DataMap();
		Map<String, String[]> maps = request.getParameterMap();
		Set<String> keySet = maps.keySet();
		Iterator<String> keys = keySet.iterator();
		while (keys.hasNext()) {
			String key = keys.next();
			String[] value = maps.get(key);
			if (value.length > 1){
				//for (String val : value) {
					//resultMap.put(key, val);
				//}
				resultMap.put(key, value);
			} else {
				if(value[0] instanceof String) {
					resultMap.put(key, value[0].toString());
				} else {
					resultMap.put(key, value[0]);
				}
			}
		}
		resultMap.defaultSetInfo(request, resultMap);
		return resultMap;
	}
	public static DataMap getParamMap(HttpServletRequest request){
		return makePrameterMap(request);
	}
	private static String nvlTrim(String obj) {
		String result = "";
	    if (obj != null)
    	result = obj.trim();
	    return result;
	}
	public Object get(String key){
		Object o = null;
		try {
			o = super.get(key);	
		} catch (NullPointerException e) {
			LOGGER.error("Error in get() key="+key, e);
		}
		return o;
	}
	public String [] getStringArray(String key) {
		String [] r = null;
		try {
			if(super.get(key) instanceof String[]){
				r = (String [])super.get(key);	
			}else{
				r = new String[1];
				r[0]= getString(key);
			}
		} catch (NullPointerException e) {
			LOGGER.error("Error in getStringArray() key="+key, e);
		} 
		return r;
	}
	public String getString(String key) {
		String r = "";
		try {
			if(super.get(key) != null){
				r = super.get(key).toString();	
			}
		} catch (NullPointerException e) {
			LOGGER.error("Error in getString() key="+key, e);
		}
		return r;
	}
	public int getInt(String key) {
	    return getInt(key, 0);
  	}
	public int getInt(String key, int defaultInt) {
	    int result = 0;
	    String str = getString(key);
	    try {
	    	if(StringUtils.isNotEmpty(str)) {
	    		result = new Integer(str).intValue();
	    	}
	    } catch (NumberFormatException e) {
	    	LOGGER.error("Error in getInt() key="+key+", getString(key)=="+getString(key), e);
	    	result = defaultInt;
	    } 
	    return result;
  	}
	public long getLong(String key) {
	    return getLong(key, 0);
  	}
	public long getLong(String key, long defaultLong) {
	    long result = 0;
	    String str = getString(key);
	    try {
	    	if(StringUtils.isNotEmpty(str)) {
		    	Double tmp = Double.parseDouble(str);
		    	result = tmp.longValue();
	    	}
	    } catch (NumberFormatException e) {
	    	LOGGER.error("Error in getLong() key="+key+", getString(key)=="+getString(key), e);
	    	result = defaultLong;
	    }
	    return result;
  	}
	public double getDouble(String key) {
	    return getDouble(key, 0);
  	}
	public double getDouble(String key, double defaultDouble) {
		double result = 0;
	    String str = getString(key);
	    try {
	    	if(StringUtils.isNotEmpty(str)) {
	    		result = new Long(str).doubleValue();
	    	}
	    } catch (NumberFormatException e) {
	    	LOGGER.error("Error in getDouble() key="+key+", getString(key)="+getString(key), e);
	    	result = defaultDouble;
	    }
	    return result;
  	}
	public boolean getBoolean(String key){
		return getBoolean(key, false);
	}
	public boolean getBoolean(String key, boolean defaltValue){
		boolean result = defaltValue;
		String strObj = nvlTrim(key);
		if (strObj.length() > 0){
			result = strObj.toLowerCase().equals("true"); 
		}
		return result;
	}
	public void setString(String key, String value){
		super.put(key, value);
	}
	public void setInt(String key, int value){
		super.put(key, value);
	}
	public void setInt(String key, String value){
		super.put(key, new Integer(value).intValue());
	}
	public void setLong(String key, long value){
		super.put(key, value);
	}
	public void setLong(String key, String value){
		super.put(key, new Long(value).longValue());
	}
	public void setDouble(String key, double value){
		super.put(key, value);
	}
	public void setDouble(String key, String value){
		super.put(key, new Double(value).doubleValue());
	}
	public void setBoolean(String key, boolean value){
		super.put(key, new Boolean(value));
	}
	public Object put(String key, Object value) {
		return super.put(CamelUtil.convert2CamelCase((String) key), value);
	}
}
