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

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.com.qmth.stmms.admin.utils.SessionExamUtils;
import cn.com.qmth.stmms.biz.common.BaseQuery;
import cn.com.qmth.stmms.biz.exam.model.Exam;
import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
import cn.com.qmth.stmms.biz.exam.service.ExamService;
import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
import cn.com.qmth.stmms.biz.exam.service.query.ExamSubjectSearchQuery;
import cn.com.qmth.stmms.biz.mark.service.MarkLibraryService;
import cn.com.qmth.stmms.common.controller.BaseController;
import cn.com.qmth.stmms.common.domain.WebUser;
import cn.com.qmth.stmms.common.enums.ExamStatus;

public class BaseExamController extends BaseController {

    public static final int EXAM_SELECT_COUNT = 5;

    protected static Logger log = LoggerFactory.getLogger(BaseExamController.class);

    @Autowired
    private MarkLibraryService libraryService;

    @Autowired
    private ExamSubjectService subjectService;

    @Autowired
    private ExamService examService;

    protected int getSessionExamId(HttpServletRequest request) {
        return SessionExamUtils.getExamId(request);
    }

    protected boolean isFinish(int examId) {
        Exam exam = examService.findById(examId);
        return exam != null ? exam.getStatus() == ExamStatus.FINISH : false;
    }

    /**
     * 添加Flash消息
     * 
     * @param messages
     *            消息
     */
    protected void addMessage(RedirectAttributes redirectAttributes, String... messages) {
        StringBuilder sb = new StringBuilder();
        for (String message : messages) {
            sb.append(message).append(messages.length > 1 ? "<br/>" : "");
        }
        redirectAttributes.addFlashAttribute("message", sb.toString());
    }

    protected List<ExamSubject> getExamSubject(int examId, WebUser wu) {
        List<ExamSubject> list = new LinkedList<ExamSubject>();
        if (wu.isSchoolAdmin() || wu.isSchoolViewer()) {
            list = subjectService.list(examId);
        } else if (wu.isSubjectHeader()) {
            ExamSubject subject = subjectService.find(examId, wu.getUser().getSubjectCode());
            if (subject != null) {
                list.add(subject);
            }
        }
        return list;
    }

    protected List<ExamSubject> getUploadExamSubject(int examId, WebUser wu) {
        List<ExamSubject> list = new LinkedList<ExamSubject>();
        if (wu.isSchoolAdmin()) {
            list = subjectService.list(examId, 0);
        } else if (wu.isSubjectHeader()) {
            ExamSubject subject = subjectService.find(examId, wu.getUser().getSubjectCode());
            if (subject != null && subject.getUploadCount() > 0) {
                list.add(subject);
            }
        }
        return list;
    }

    protected List<ExamSubject> getTagSubject(int examId, WebUser wu) {
        List<ExamSubject> list = new LinkedList<ExamSubject>();
        List<String> codes = libraryService.findTagSubjectCode(examId);
        for (String code : codes) {
            ExamSubject subject = subjectService.find(examId, code);
            if (subject != null) {
                if (wu.isSubjectHeader() && subject.getCode().equals(wu.getUser().getSubjectCode())) {
                    list.add(subject);
                } else if (wu.isSchoolAdmin()) {
                    list.add(subject);
                }
            }
        }
        return list;
    }

    @SuppressWarnings("rawtypes")
    protected BaseQuery subjectFilter(BaseQuery query, WebUser wu) {
        if (wu.isSubjectHeader()) {
            if (query instanceof ExamSubjectSearchQuery) {
                ((ExamSubjectSearchQuery) query).setCode(wu.getUser().getSubjectCode());
            } else {
                try {
                    BeanUtils.setProperty(query, "subjectCode", wu.getUser().getSubjectCode());
                } catch (Exception e) {

                }
            }
        }
        return query;
    }

    protected boolean subjectCheck(String subjectCode, WebUser wu) {
        if (wu.isSubjectHeader()) {
            return subjectCode.equals(wu.getUser().getSubjectCode());
        } else {
            return true;
        }
    }

}
