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

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

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

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.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 "gx";
    }

    @AuthValidate("adminUser")
    @RequestMapping("/exams")
    @ResponseBody
    public JSONArray getExamInfos(HttpServletRequest request, ExamSearchQuery query) {
        User user = RequestUtils.getApiUser(request);
        JSONArray array = new JSONArray();
        List<Exam> list = new LinkedList<Exam>();
        if (user.getSchoolId() != null) {
            // query.setCreatorId(user.getId());
            query.setSchoolId(user.getSchoolId());
            // query.addStatus(ExamStatus.START);
            // query.setPageNumber(1);
            // query.setPageSize(20);
            if (query.getPageSize() < 20) {
                query.setPageSize(20);
            }
            query.setOrderByIdDesc(true);
            query = examService.findByQuery(query);
            if (query.getCurrentCount() > 0) {
                list = query.getResult();
            }
        }

        for (Exam exam : list) {
            JSONObject obj = new JSONObject();
            obj.accumulate("id", exam.getId());
            obj.accumulate("name", exam.getName());
            String examTime = DateUtils.formatDateTime(exam.getExamTime());
            obj.accumulate("examTime", examTime);
            array.add(obj);
        }
        return array;
    }

    @RequestMapping("/campus/{schoolId}")
    @ResponseBody
    public JSONArray getCampus(@PathVariable Integer schoolId) {
        List<Campus> list = campusService.findBySchoolId(schoolId);
        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("/subjects/{examId}")
    @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")
    @RequestMapping(value = "/subject/update", method = RequestMethod.POST)
    @ResponseBody
    public JSONObject updateSubject(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ExamSubjectVO subject) {
        User user = RequestUtils.getApiUser(request);
        JSONObject result = new JSONObject();
        Exam exam = examService.findById(subject.getExamId());
        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());
            } else {
                result.accumulate("code", "");
            }
        } else {
            throw ApiException.EXAM_NOT_ACCESSIBLED;
        }
        return result;
    }
}
