package com.dbnt.sims.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

@SuppressWarnings({"rawtypes","unchecked", "serial"})
public class ParamMap extends HashMap {

	public ParamMap(){
		super();
	}
	
	public ParamMap(final String key, final Object value) {
		super();

		super.put(key, value);
	}
	
    public ParamMap(final HttpServletRequest request) {
        super();
        
        setRequest(request);
    }
	
	public void setRequest(HttpServletRequest request) {

		Enumeration enums = request.getParameterNames();
		while (enums.hasMoreElements()) {
			String paramName = (String) enums.nextElement();
			String[] parameters = request.getParameterValues(paramName);

			// Parameter媛� 諛곗뿴�씪 寃쎌슦
			if (parameters.length > 1) {
				this.put(paramName, parameters);
				// Parameter媛� 諛곗뿴�씠 �븘�땶 寃쎌슦
			} else {
				this.put(paramName, parameters[0]);
			}
		}
	}

	public void set(String key, Object value) {
		super.put(key, value);
	}
	
	public void setTm(String key, Object value, String weeks) {
		int week = Integer.parseInt(weeks);
		String name = String.format("%02d", week);
		super.put(key+name, value);
	}
	
	public Object get(String key){
		return super.get(key);
	}
	
	public String getString(String key) {
		return getString(key, "");
	}
	
	public String getString(String key, String defaultValue) {
		Object value = super.get(key);
		String result = null;
		if(value == null) {
			result = defaultValue;
		} else if(value instanceof String) {
			result =  (String)value;
		} else if(value instanceof Integer || value instanceof Double || value instanceof Long || value instanceof BigDecimal) {
			result = String.valueOf(value);
		} else { 
			result = (String)value;
		}
		return result;
	}

	public int getInt(String key) {
		return getInt(key, 0);
	}
	
	public int getInt(String key, int defaultValue) {
		Object value = super.get(key);
		int result = 0;
		if(value == null) {
			result = defaultValue;
		} else if(value instanceof String) {
			result =  Integer.parseInt((String)value);
		} else if(value instanceof Integer) {
			result = (Integer)value;
		} else if(value instanceof Double) {
			result = ((Double)value).intValue();
		} else if(value instanceof Long) {
			result = ((Long)value).intValue();
		} else if(value instanceof BigDecimal) {
			result = ((BigDecimal)value).intValue();
		} else {
			result = (int)value;
		}
		return result;
	}

    public long getLong(String key) {
        return getLong(key.replaceAll(",", ""), 0);
    }

    public long getLong(String key, long defaultValue) {
        long val = defaultValue;
        try {
        	if(super.get(key) instanceof BigDecimal) {
        		val =  Long.parseLong(((BigDecimal)super.get(key)).toString());
        	} else {
        		val = Long.parseLong((String)super.get(key));
        	}
        } catch (NumberFormatException e) {}

        return val;
    }

    public byte[] getByte(final String key) {
    	if(null != super.get(key)) {
    		try {
    			return (byte[])super.get(key);
    		} catch(Exception e) {
    			return new byte[0];
    		}
    	}
    	
		return new byte[0];
    }

    @Override
	public String toString() {
    	String s = "";
    	Set<?> set = this.entrySet();
    	for(Iterator<?> it = set.iterator(); it.hasNext();) {
    		String key = it.next().toString();
    		
    		s += "[" + key + "]";
    	}
    	return s;
    }
    public String[] getValues(final String key){
    	if(super.get(key) != null) {
    		try{
    			Object obj = super.get(key);
    			if (obj instanceof ArrayList){
    				ArrayList<?> list = (ArrayList<?>)obj;
    				return (String[])list.toArray(new String[0]);
    			}else if(obj instanceof String){
    				String[] rv = {(String)obj};
    				return rv;
    			}else{
    				return (String[])obj;
    			}
    		}
    		catch(java.lang.ClassCastException e) {
    			String[] rv = {(String)super.get(key)};
    			return rv;
    		}
    		catch(Exception ee) {
    			String[] rv = {(String)super.get(key)};
    			return rv;
    		}
    	}else{
    		return new String[0] ;
    	}
    }


    public String toQueryString() {
    	StringBuffer sb = new StringBuffer();
    	
    	Set<?> set = this.entrySet();
    	Iterator<?> keys = set.iterator();
    	
    	boolean isFirst = true;
    	while (keys.hasNext()) {
    		String sKey = keys.next().toString();
			if (!isFirst) sb.append("&");
			sb.append(sKey);
    		isFirst = false;
    	}
    	
    	return sb.toString();
    }	
}
