package org.fouram.core.base.model;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.fouram.core.base.constants.SysConstants;
import org.fouram.core.util.Tools;
import org.fouram.entity.SysUser;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@SuppressWarnings("rawtypes")
public class PageData extends HashMap implements Map {

	private static final long serialVersionUID = 1L;

	Map map = null;
	HttpServletRequest request;

	@SuppressWarnings("unchecked")
	public PageData(HttpServletRequest request) {
		this.request = request;

		Map properties = request.getParameterMap();
		Map returnMap = new HashMap();
		Iterator entries = properties.entrySet().iterator();
		Map.Entry entry;
		String name = "";

		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		SysUser user = (SysUser) session.getAttribute(SysConstants.ConstSession.SESSION_CMS_USER); // cms
		if (user != null && !"".equals(user)) {
			returnMap.put("USERID", user.getId());
			returnMap.put("DATA_LEVEL",
					request.getSession().getAttribute(SysConstants.ConstSession.SESSION_DATA_LEVEL));
			returnMap.put("USER_PHONE", user.getPhone());
		}
		// 将请求IP自动添加到请求参数列表中
		returnMap.put("IP", Tools.getIpAddress(request));

		while (entries.hasNext()) {
			String value = "";
			entry = (Map.Entry) entries.next();
			name = (String) entry.getKey();
			Object valueObj = entry.getValue();
			if (null == valueObj) {
				value = "";
			} else if (valueObj instanceof String[]) {
				String[] values = (String[]) valueObj;
				if (values.length == 1) {
					value = values[0];
				} else {
					value = JSONArray.toJSONString(values);
				}
			} else if (valueObj instanceof Date) {
				SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				value = sdfTime.format(valueObj);
			} else {
				value = valueObj.toString();
			}
			returnMap.put(name, value);
		}
		map = returnMap;
	}

	public PageData() {
		map = new HashMap();
	}

	public PageData(JSONObject json) {
		map = json;
	}

	// 获取list
	public List<String> getList(Object key) {
		List<String> list = new ArrayList<>();
		String value = map.get(key) + "";
		if (Tools.isEmpty(value)) {
			return list;
		}
		if (value.contains("[")) {
			JSONArray array = JSON.parseArray(value);
			for (int i = 0; i < array.size(); i++) {
				list.add(array.getString(i));
			}
		} else {
			list.add(value);
		}
		return list;
	}

	@Override
	public Object get(Object key) {
		Object obj = null;
		if (map.get(key) instanceof Object[]) {
			Object[] arr = (Object[]) map.get(key);
			obj = request == null ? arr : (request.getParameter((String) key) == null ? arr : arr[0]);
		} else if (map.get(key) instanceof Timestamp || map.get(key) instanceof Date) {
			SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			obj = sdfTime.format(map.get(key));
		} else {
			obj = map.get(key);
		}
		return obj;
	}

	public String getString(Object key) {
		Object value = get(key);
		if (value != null) {
			return value.toString();
		} else {
			return null;
		}
	}

	public Boolean getBoolean(Object key) {
		Object value = get(key);
		if (value != null && StringUtils.isNotBlank(value.toString())) {
			return "true".equals(value.toString()) || "1".equals(value.toString());
		} else {
			return null;
		}
	}

	public Double getDouble(Object key) {
		try {
			return Double.valueOf(getString(key));
		} catch (Exception e) {
			return null;
		}
	}

	public Integer getInteger(Object key) {
		try {
			return Integer.valueOf(getString(key));
		} catch (Exception e) {
			return null;
		}
	}

	public Long getLong(Object key) {
		try {
			return Long.valueOf(getString(key));
		} catch (Exception e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object put(Object key, Object value) {
		return map.put(key, value);
	}

	@Override
	public Object remove(Object key) {
		return map.remove(key);
	}

	@Override
	public void clear() {
		map.clear();
	}

	@Override
	public boolean containsKey(Object key) {
		return map.containsKey(key);
	}

	@Override
	public boolean containsValue(Object value) {
		return map.containsValue(value);
	}

	@Override
	public Set entrySet() {
		return map.entrySet();
	}

	@Override
	public boolean isEmpty() {
		return map.isEmpty();
	}

	@Override
	public Set keySet() {
		return map.keySet();
	}

	@Override
	@SuppressWarnings("unchecked")
	public void putAll(Map t) {
		map.putAll(t);
	}

	@Override
	public int size() {
		return map.size();
	}

	@Override
	public Collection values() {
		return map.values();
	}
}
