package cn.com.qmth.stmms.analysis.school;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.qmth.stmms.biz.exam.query.ExamSearchQuery;
import cn.com.qmth.stmms.biz.exam.service.ExamService;
import cn.com.qmth.stmms.biz.school.model.School;
import cn.com.qmth.stmms.biz.school.service.SchoolService;
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.ExamStatus;
import cn.com.qmth.stmms.common.utils.RequestUtils;

@Controller
@RequestMapping("/analysis/school")
public class SchoolAnalysisController {

    @Autowired
    private ExamService examService;

    @Autowired
    private UserService userService;

    @Autowired
    private SchoolService schoolService;

    @RequestMapping("/totalreport")
    public ModelAndView totalReport(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/totalreport");
        buildOptionValues(request, view);
        User user = RequestUtils.getWebUser(request).getUser();
        School department = schoolService.findById(user.getSchoolId());

        if (StringUtils.isNotBlank(department.getCity())) {
            view.addObject("cityId", department.getCity());
        } else if (StringUtils.isNotBlank(department.getProvince())) {
            view.addObject("provinceId", department.getCity());
        }
        return view;
    }

    @RequestMapping("/onlinedetail")
    public ModelAndView onlineDetail(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/onlinedetail");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/excellent")
    public ModelAndView classAnalysis(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/excellent");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/first")
    public ModelAndView first(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/first");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/frequencyanalysis")
    public ModelAndView frequencyanalysis(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/frequencyanalysis");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/knowledgedifficult")
    public ModelAndView knowledgeDifficult(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/knowledgedifficult");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/knowledgescored")
    public ModelAndView knowledgeScored(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/knowledgescored");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/objectivescored")
    public ModelAndView objectiveScored(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/objectivescored");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/online")
    public ModelAndView onLine(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/online");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/paperdifficult")
    public ModelAndView paperDifficult(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/paperdifficult");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/points")
    public ModelAndView points(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/points");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/qualityanalysis")
    public ModelAndView qualityAnalysis(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/qualityanalysis");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/questiontypedifficult")
    public ModelAndView questionTypeDifficult(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/questiontypedifficult");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/questiontypescored")
    public ModelAndView questionTypeScored(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/questiontypescored");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/totalanalysis")
    public ModelAndView totalAnalysis(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/totalanalysis");
        // 初始菜单选中状态
        request.getSession().setAttribute("menuNo", 13);
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/reportall")
    public ModelAndView reportAll(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/reportall");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/avgcompare")
    public ModelAndView avgCompare(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/avgcompare");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/subjectrange")
    public ModelAndView subjectRange(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/subjectrange");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/totalrange")
    public ModelAndView totalRange(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/totalrange");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/subjectanalysis")
    public ModelAndView subjectAnalysis(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/subjectanalysis");
        buildOptionValues(request, view);
        return view;
    }

    @RequestMapping("/subjectitemanalysis")
    public ModelAndView subjectItemAnalysis(HttpServletRequest request) {
        ModelAndView view = new ModelAndView("/analysis/school/subjectitemanalysis");
        buildOptionValues(request, view);
        return view;
    }

    private void buildOptionValues(HttpServletRequest request, ModelAndView view) {
        User analysiser = RequestUtils.getWebUser(request).getUser();
        User admin = userService.findSchoolAdmin(analysiser.getSchoolId());

        ExamSearchQuery query = new ExamSearchQuery();
        query.addStatus(ExamStatus.START);
        query.addStatus(ExamStatus.ENDED);
        query.setCreatorId(admin.getId());
        ExamSearchQuery result = examService.findByQuery(query);
        view.addObject("examList", result.getResult());
        view.addObject("web_request_url", request.getRequestURI());
    }
}
