package cn.com.qmth.stmms.admin.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import cn.com.qmth.stmms.admin.utils.SessionExamUtils;
import cn.com.qmth.stmms.biz.basic.service.PrivilegeService;
import cn.com.qmth.stmms.biz.basic.service.RolePrivilegeService;
import cn.com.qmth.stmms.biz.exam.service.SubjectUserService;
import cn.com.qmth.stmms.biz.user.service.UserService;
import cn.com.qmth.stmms.common.domain.WebUser;
import cn.com.qmth.stmms.common.enums.Role;
import cn.com.qmth.stmms.common.interceptor.SessionInterceptor;
import cn.com.qmth.stmms.common.utils.RequestUtils;

/**
 * Admin模块操作拦截器
 *
 * @author LS
 */
public class AdminInterceptor extends SessionInterceptor {

    @Autowired
    private UserService userService;

    @Autowired
    private SubjectUserService subjectUserService;
    
    @Autowired
    private RolePrivilegeService rolePrivilegeService;
    
    @Autowired
    private PrivilegeService privilegeService;
    
    
    

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        return validateAdminUser(buildWebUser(request), request, response);
    }

    private boolean validateAdminUser(WebUser wu, HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        if (wu != null && (wu.getRole() == Role.SYS_ADMIN)) {
            return true;
        }
        if (wu != null
                && (wu.getRole() == Role.SCHOOL_ADMIN || wu.getRole() == Role.SUBJECT_HEADER
                        || wu.getRole() == Role.SCHOOL_VIEWER || wu.getRole() == Role.INSPECTOR || wu.getRole() == Role.COLLEGE_ADMIN)) {
            Integer examId = SessionExamUtils.getExamId(request);
            String uri = request.getRequestURI();
            if (examId > 0 || uri.startsWith("/admin/exam/select") || uri.startsWith("/admin/home")
                    || uri.startsWith("/admin/exam/create") || uri.startsWith("/admin/exam/save")
                    || uri.startsWith("/admin/reset")) {
                return true;
            }
            return redirect(request, response, "/admin/exam/select");
        }
        return sessionExpire(request, response, "/login");
    }

    private WebUser buildWebUser(HttpServletRequest request) {
        WebUser wu = WebUser.buildFromSession(RequestUtils.getSession(request), userService);
        if (wu != null) {
            if (wu.getRole() == Role.SUBJECT_HEADER || wu.getRole() == Role.INSPECTOR
                    || wu.getRole() == Role.COLLEGE_ADMIN || wu.getRole() == Role.SCHOOL_VIEWER) {
                wu.setSubjectCodeSet(subjectUserService.findSubjectCode(wu.getId()));
            }
            RequestUtils.setWebUser(request, wu);
            RequestUtils.setRolePrivilegeCodes(request, rolePrivilegeService.getRoleCodes(wu.getUser().getSchoolId(), wu.getRole()));
            RequestUtils.setPrivileges(request, privilegeService.getMenuPrivileges());
            return wu;
        }
        return null;
    }
}
