package cn.com.qmth.stmms.api.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.qmth.stmms.admin.vo.ExamSubjectVO;
import cn.com.qmth.stmms.biz.api.auth.annotation.AuthValidate;
import cn.com.qmth.stmms.biz.api.auth.exception.ApiException;
import cn.com.qmth.stmms.biz.campus.model.Campus;
import cn.com.qmth.stmms.biz.campus.service.CampusService;
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.query.ExamSearchQuery;
import cn.com.qmth.stmms.biz.exam.service.ExamService;
import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
import cn.com.qmth.stmms.biz.user.model.User;
import cn.com.qmth.stmms.common.enums.ExamStatus;
import cn.com.qmth.stmms.common.utils.DateUtils;
import cn.com.qmth.stmms.common.utils.RequestUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller("examInfoApiController")
@RequestMapping("/api")
public class ExamInfoController extends BaseApiController {

    @Autowired
    private ExamService examService;

    @Autowired
    private ExamSubjectService subjectService;

    @Autowired
    private CampusService campusService;

    @RequestMapping("/version")
    @ResponseBody
    public String version(HttpServletRequest request) {
        return "ft";
    }

    @AuthValidate({ "adminUser", "scanner" })
    @RequestMapping("/exams")
    @ResponseBody
    public JSONArray getExamInfos(HttpServletRequest request, ExamSearchQuery query) {
        User user = RequestUtils.getApiUser(request);
        JSONArray array = new JSONArray();
        if (user.getSchoolId() != null) {
            query.setSchoolId(user.getSchoolId());
            query.addStatus(ExamStatus.START);
            if (query.getPageSize() < 1) {
                query.setPageSize(20);
            }
            query = examService.findByQuery(query);
            for (Exam exam : query.getResult()) {
                JSONObject obj = new JSONObject();
                obj.accumulate("id", exam.getId());
                obj.accumulate("name", exam.getName());
                obj.accumulate("examTime", DateUtils.formatDate(exam.getExamTime()));
                array.add(obj);
            }
        }
        return array;
    }

    @AuthValidate({ "adminUser", "scanner" })
    @RequestMapping(value = "/campus", method = RequestMethod.GET)
    @ResponseBody
    public JSONArray getCampus(HttpServletRequest request) {
        User user = RequestUtils.getApiUser(request);
        List<Campus> list = campusService.findBySchoolId(user.getSchoolId());
        JSONArray array = new JSONArray();
        for (Campus c : list) {
            JSONObject obj = new JSONObject();
            obj.accumulate("code", c.getId().toString());
            obj.accumulate("name", c.getName());
            array.add(obj);
        }
        return array;
    }

    @RequestMapping(value = "/subjects/{examId}", method = RequestMethod.GET)
    @ResponseBody
    public JSONArray getSubjects(@PathVariable Integer examId) {
        List<ExamSubject> sList = subjectService.list(examId);
        JSONArray array = new JSONArray();
        for (ExamSubject subject : sList) {
            JSONObject obj = new JSONObject();
            obj.accumulate("code", subject.getCode());
            obj.accumulate("name", subject.getName());
            array.add(obj);
        }
        return array;
    }

    @AuthValidate({ "adminUser", "scanner" })
    @RequestMapping(value = "/subject/update", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject updateSubject(HttpServletRequest request, @RequestBody ExamSubjectVO subject) {
        User user = RequestUtils.getApiUser(request);
        Exam exam = examService.findById(subject.getExamId());
        JSONObject result = new JSONObject();
        if (exam != null && exam.getSchoolId().equals(user.getSchoolId())) {
            ExamSubject es = subjectService.find(subject.getExamId(), subject.getCode());
            if (es != null) {
                if (subject.getHasAnswer() != null) {
                    es.setHasAnswer(subject.getHasAnswer().booleanValue());
                }
                if (subject.getHasPaper() != null) {
                    es.setHasPaper(subject.getHasPaper().booleanValue());
                }
                subjectService.save(es);
                result.accumulate("code", subject.getCode());
                return result;
            } else {
                result.accumulate("code", "");
            }
        } else {
            throw ApiException.EXAM_NOT_ACCESSIBLED;
        }
        return result;
    }
}
