package cn.com.qmth.stmms.common.session.model;

import cn.com.qmth.stmms.common.domain.WebUser;
import cn.com.qmth.stmms.common.enums.Role;
import org.apache.commons.lang.StringUtils;

import javax.persistence.Transient;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class StmmsSession implements Serializable {

    private static final long serialVersionUID = -1004172926154352747L;

    private static final String WEB_USER_ID_KEY = "_web_user_id_";

    private static final String WEB_MARKER_ID_KEY = "_web_marker_id_";

    private static final String WEB_USER_ROLE_KEY = "_web_user_role_";

    private static final String WEB_USER_TOKEN_KEY = "_web_user_token_";

    private static final String WEB_USER_LOGOUT_URL = "_web_user_logout_url";

    private String id;

    private Map<String, String> map;

    @Transient
    private boolean invalid;

    public StmmsSession() {
        this.map = new HashMap<String, String>();
    }

    public String getParameter(String key) {
        return map.get(key);
    }

    public void setParameter(String key, String value) {
        map.put(key, value);
    }

    public Set<Entry<String, String>> getParameters() {
        return map.entrySet();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void clear() {
        this.id = null;
        this.map.clear();
    }

    public boolean isInvalid() {
        return invalid;
    }

    public void setInvalid(boolean invalid) {
        this.invalid = invalid;
        if (isInvalid()) {
            clear();
        }
    }

    public Integer getWebUserId() {
        String value = getParameter(WEB_USER_ID_KEY);
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {
            return null;
        }
    }

    public Integer getWebMarkerId() {
        String value = getParameter(WEB_MARKER_ID_KEY);
        try {
            return Integer.parseInt(value);
        } catch (Exception e) {
            return null;
        }
    }

    public Role getWebUserRole() {
        String value = getParameter(WEB_USER_ROLE_KEY);
        try {
            return Role.findByValue(Integer.parseInt(value));
        } catch (Exception e) {
            return null;
        }
    }

    public String getWebUserToken() {
        return getParameter(WEB_USER_TOKEN_KEY);
    }

    public String getWebUserLogoutUrl() {
        return getParameter(WEB_USER_LOGOUT_URL);
    }

    public void saveWebUser(WebUser webUser) {
        setParameter(WEB_USER_ID_KEY, String.valueOf(webUser.getId()));
        setParameter(WEB_MARKER_ID_KEY, webUser.getMarkerId() != null ? String.valueOf(webUser.getMarkerId()) : null);
        setParameter(WEB_USER_ROLE_KEY, String.valueOf(webUser.getRole().getValue()));
        setParameter(WEB_USER_TOKEN_KEY, StringUtils.trimToEmpty(webUser.getToken()));
        setParameter(WEB_USER_LOGOUT_URL, StringUtils.trimToEmpty(webUser.getLogoutUrl()));
    }

}
