package cn.com.qmth.stmms.common.domain;

import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
import cn.com.qmth.stmms.biz.exam.model.Marker;
import cn.com.qmth.stmms.biz.school.model.School;
import cn.com.qmth.stmms.biz.user.model.User;
import cn.com.qmth.stmms.common.enums.Auth;
import cn.com.qmth.stmms.common.enums.Role;
import cn.com.qmth.stmms.common.enums.UserType;
import cn.com.qmth.stmms.common.session.model.StmmsSession;

/**
 * 云阅卷前台已登录用户抽象描述类
 * 
 * @author luoshi
 * 
 */
public class WebUser {

    private Integer id;

    private UserType type;

    private Object userData;

    private String name;

    private Set<Role> roles;

    private String roleDisplays;

    private Map<String, Role> roleMap;

    private Map<String, Auth> authMap;

    private String schoolName;

    private Set<School> schools;

    public WebUser(Integer id, UserType type) {
        this.id = id;
        this.type = type;

        if (id == null || type == null) {
            throw new IllegalArgumentException("webUser init error!");
        }
    }

    public void writeToSession(StmmsSession session) {
        session.setParameter("_web_user_id_", String.valueOf(id));
        session.setParameter("_web_user_type_", String.valueOf(type.getValue()));
    }

    public static WebUser buildFromSession(StmmsSession session) {
        WebUser wu = null;
        String userId = StringUtils.trimToNull(session.getParameter("_web_user_id_"));
        String userType = StringUtils.trimToNull(session.getParameter("_web_user_type_"));
        if (StringUtils.isNumeric(userId) && StringUtils.isNumeric(userType)) {
            try {
                wu = new WebUser(Integer.valueOf(userId), UserType.findByValue(Integer.valueOf(userType)));
            } catch (Exception e) {
            }
        }
        return wu;
    }

    public boolean isType(UserType userType) {
        return type != null ? type == userType : false;
    }

    public boolean isAdmin() {
        return isType(UserType.ADMIN);
    }

    public boolean isViewer() {
        return isType(UserType.VIEWER);
    }

    public void setDataObject(Object userData) {
        this.userData = userData;
    }

    public User getUser() {
        return userData != null && userData instanceof User ? (User) userData : null;
    }

    public Marker getMarker() {
        return userData != null && userData instanceof Marker ? (Marker) userData : null;
    }

    public ExamStudent getExamStudent() {
        return userData != null && userData instanceof ExamStudent ? (ExamStudent) userData : null;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

    public String getRoleDisplays() {
        return roleDisplays;
    }

    public void setRoleDisplays(String roleDisplays) {
        this.roleDisplays = roleDisplays;
    }

    public Map<String, Role> getRoleMap() {
        return roleMap;
    }

    public void setRoleMap(Map<String, Role> roleMap) {
        this.roleMap = roleMap;
    }

    public Map<String, Auth> getAuthMap() {
        return authMap;
    }

    public void setAuthMap(Map<String, Auth> authMap) {
        this.authMap = authMap;
    }

    public Integer getId() {
        return id;
    }

    public UserType getType() {
        return type;
    }

    public String getSchoolName() {
        return schoolName;
    }

    public void setSchoolName(String schoolName) {
        this.schoolName = schoolName;
    }

    public Set<School> getSchools() {
        return schools;
    }

    public void setSchools(Set<School> schools) {
        this.schools = schools;
    }

}
