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

import java.util.List;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

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.exam.model.Exam;
import cn.com.qmth.stmms.biz.exam.model.ExamPackage;
import cn.com.qmth.stmms.biz.exam.service.ExamPackageService;
import cn.com.qmth.stmms.biz.exam.service.ExamService;
import cn.com.qmth.stmms.biz.exam.service.ExamStudentService;
import cn.com.qmth.stmms.biz.user.model.User;
import cn.com.qmth.stmms.common.utils.RequestUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller("examPackageApiController")
@RequestMapping("/api")
public class ExamPackageController extends BaseApiController {

    protected static Logger logger = LoggerFactory.getLogger(ExamPackageController.class);

    @Autowired
    private ExamStudentService examStudentService;

    @Autowired
    private ExamService examService;

    @Autowired
    private ExamPackageService packageService;

    @AuthValidate("adminUser")
    @RequestMapping(value = "/package/{examId}", method = RequestMethod.GET)
    @ResponseBody
    public JSONArray getPackageCode(HttpServletRequest request, HttpServletResponse response,
            @PathVariable Integer examId) {
        User user = RequestUtils.getApiUser(request);
        JSONArray array = new JSONArray();
        Exam exam = examService.findById(examId);
        if (exam != null && exam.getSchoolId().equals(user.getSchoolId())) {
            List<String> list = examStudentService.findDistinctPackageCode(examId);
            if (list != null) {
                for (String code : list) {
                    array.add(code);
                }
            }
        } else {
            throw ApiException.EXAM_NOT_ACCESSIBLED;
        }
        return array;
    }

    @AuthValidate("adminUser")
    @RequestMapping(value = "/package/count/{examId}", method = RequestMethod.GET)
    @ResponseBody
    public JSONArray getPackageCount(HttpServletRequest request, HttpServletResponse response,
            @PathVariable Integer examId, @RequestParam(required = false) Boolean upload) {
        User user = RequestUtils.getApiUser(request);
        JSONArray array = new JSONArray();
        Exam exam = examService.findById(examId);
        if (exam != null && exam.getSchoolId().equals(user.getSchoolId())) {
            List<ExamPackage> list = upload != null ? packageService.list(examId, upload) : packageService.list(examId);
            if (list != null) {
                for (ExamPackage ep : list) {
                    JSONObject obj = new JSONObject();
                    obj.accumulate("code", ep.getCode());
                    obj.accumulate("picCount", ep.getPicCount());
                    array.add(obj);
                }
            }
        } else {
            throw ApiException.EXAM_NOT_ACCESSIBLED;
        }
        return array;
    }

    @AuthValidate("adminUser")
    @RequestMapping(value = "/package/{examId}", method = RequestMethod.POST)
    @ResponseBody
    public int updatePackage(HttpServletRequest request, HttpServletResponse response, @PathVariable Integer examId,
            @RequestBody ExamPackage examPackage) {
        User user = RequestUtils.getApiUser(request);
        Exam exam = examService.findById(examId);
        if (exam != null && exam.getSchoolId().equals(user.getSchoolId())) {
            ExamPackage obj = packageService.find(examId, examPackage.getCode());
            if (obj != null && examPackage.getPicCount() != null) {
                obj.setPicCount(examPackage.getPicCount());
                obj = packageService.save(obj);
                return obj.getPicCount();
            }
        } else {
            throw ApiException.EXAM_NOT_ACCESSIBLED;
        }
        return -1;
    }

}
