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

import cn.com.qmth.stmms.biz.exam.model.Marker;
import cn.com.qmth.stmms.biz.user.model.User;
import cn.com.qmth.stmms.biz.user.service.UserService;
import cn.com.qmth.stmms.common.enums.Role;
import cn.com.qmth.stmms.common.session.model.StmmsSession;
import cn.com.qmth.stmms.common.utils.AccessControlUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.HashSet;
import java.util.Set;

/**
 * web端访问用户抽象描述类
 *
 * @author luoshi
 */
public class WebUser {

    private Integer id;

    private String name;

    private Role role;

    private Object userData;

    private String token;

    private String logoutUrl;

    private Integer markerId;

    private Marker marker;

    private Set<String> subjectCodeSet;

    private WebUser(Integer id, String name, Role role, String token, Object userData, String logoutUrl,
            Integer markerId) {
        this.id = id;
        this.name = name;
        this.role = role;
        this.token = token;
        this.userData = userData;
        this.logoutUrl = logoutUrl;
        this.markerId = markerId;
        this.subjectCodeSet = new HashSet<>();
    }

    public WebUser(User user) {
        this(user.getId(), user.getName(), user.getRole(), user.getAccessToken(), user, null, null);
    }

    public static WebUser buildFromSession(StmmsSession session, UserService userService) {
        Integer userId = session.getWebUserId();
        Role role = session.getWebUserRole();
        if (userId != null && role != null) {
            User user = userService.findById(userId);
            if (user != null && user.getRole().equals(role) && user.getAccessTokenRefreshTime() != null
                    && StringUtils.equals(session.getWebUserToken(), user.getAccessToken())
                    && !AccessControlUtils.expired(user.getAccessTokenRefreshTime())) {
                return new WebUser(user.getId(), user.getName(), user.getRole(), user.getAccessToken(), user,
                        session.getWebUserLogoutUrl(), session.getWebMarkerId());
            }
        }
        return null;
    }

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

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Role getRole() {
        return role;
    }

    public String getToken() {
        return token;
    }

    public void setLogoutUrl(String logoutUrl) {
        this.logoutUrl = logoutUrl;
    }

    public String getLogoutUrl() {
        return logoutUrl;
    }

    public boolean isSubjectHeader() {
        return (role == Role.SUBJECT_HEADER) || (role == Role.COLLEGE_ADMIN);
    }

    public boolean isSchoolAdmin() {
        return role == Role.SCHOOL_ADMIN;
    }

    public boolean isSchoolViewer() {
        return role == Role.SCHOOL_VIEWER;
    }

    public boolean isMarker() {
        return role == Role.MARKER;
    }

    public boolean isInspector() {
        return role == Role.INSPECTOR;
    }

    public void setMarkerId(Integer markerId) {
        this.markerId = markerId;
    }

    public Integer getMarkerId() {
        return markerId;
    }

    public Marker getMarker() {
        return marker;
    }

    public void setMarker(Marker marker) {
        this.marker = marker;
    }

    public Set<String> getSubjectCodeSet() {
        return subjectCodeSet;
    }

    public void setSubjectCodeSet(Set<String> subjectCodeSet) {
        this.subjectCodeSet = subjectCodeSet;
    }

}
