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

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

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

import cn.com.qmth.stmms.biz.exam.model.CheckStudent;
import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
import cn.com.qmth.stmms.biz.exam.query.CheckStudentSearchQuery;
import cn.com.qmth.stmms.biz.exam.service.CheckStudentService;
import cn.com.qmth.stmms.biz.exam.service.ExamStudentService;
import cn.com.qmth.stmms.common.enums.CheckType;

@Controller
@RequestMapping("/admin/exam/check/student")
public class CheckStudentController extends BaseExamController {

    @Autowired
    private ExamStudentService studentService;

    @Autowired
    private CheckStudentService checkStudentService;

    @Value("${sheet.image.server}")
    private String sheetServer;

    @RequestMapping
    public String index(Model model, HttpServletRequest request, CheckStudentSearchQuery query) {
        int examId = getSessionExamId(request);
        if (examId > 0) {
            List<ExamStudent> studentList = new ArrayList<ExamStudent>();
            query.setExamId(examId);
            query.setChecked(false);
            if (query.getType() == null) {
                query.setType(CheckType.SCAN_ABSENT);
            }
            query = checkStudentService.findByQuery(query);
            for (CheckStudent c : query.getResult()) {
                studentList.add(studentService.findById(c.getStudentId()));
            }
            model.addAttribute("typeList", CheckType.values());
            model.addAttribute("studentList", studentList);
            model.addAttribute("query", query);
            return "modules/exam/checkStudent";
        } else {
            return "redirect:/admin/exam/list";
        }
    }

    @RequestMapping("/start")
    @ResponseBody
    public ModelAndView start(HttpServletRequest request, @RequestParam CheckType type) {
        int examId = getSessionExamId(request);
        if (examId > 0) {
            ModelAndView view = new ModelAndView("modules/exam/checkStudentEdit");
            List<Integer> ids = new ArrayList<Integer>();
            List<CheckStudent> list = checkStudentService.findByExamIdAndIsCheckedAndType(examId, false, type);
            for (CheckStudent student : list) {
                ids.add(student.getStudentId());
            }
            view.addObject("sheetServer", sheetServer);
            view.addObject("ids", StringUtils.join(ids, ","));
            return view;
        } else {
            return new ModelAndView("redirect:/admin/exam/list");
        }
    }

    @RequestMapping("/save")
    @ResponseBody
    public Object save(HttpServletRequest request, @RequestParam Integer studentId, @RequestParam String answers,
            @RequestParam(required = false) Boolean absent) {
        ExamStudent student = studentService.findById(studentId);
        CheckStudent cs = checkStudentService.findByStudentId(studentId);
        if (student != null && cs != null) {
            if (absent != null) {
                student.setAbsent(absent);
            }
            student.setAnswers(answers);
            if (saveUploadStudent(student)) {
                cs.setChecked(true);
                cs.setUpdateTime(new Date());
                checkStudentService.save(cs);
            }
            return true;
        } else {
            return false;
        }
    }

}
