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

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.qmth.stmms.biz.exam.model.ExamQuestion;
import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
import cn.com.qmth.stmms.biz.exam.service.ExamQuestionService;
import cn.com.qmth.stmms.biz.exam.service.ExamSubjectService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller("examQuestionApiController")
@RequestMapping("/api")
public class ExamQuestionController extends BaseApiController {

    @Autowired
    private ExamQuestionService questionService;

    @Autowired
    private ExamSubjectService subjectService;

    @RequestMapping(value = "/objective/{examId}", method = RequestMethod.GET)
    @ResponseBody
    public JSONArray getObjectiveQuestion(HttpServletRequest request, @PathVariable Integer examId) {
        JSONArray result = new JSONArray();
        List<ExamSubject> subjectList = subjectService.list(examId);
        if (subjectList != null) {
            for (ExamSubject subject : subjectList) {
                List<ExamQuestion> list = distinct(
                        questionService.findByExamAndSubjectAndObjective(examId, subject.getCode(), true));
                if (list.isEmpty()) {
                    continue;
                }

                JSONArray array = new JSONArray();
                int index = 0;
                JSONObject current = null;
                for (ExamQuestion question : list) {
                    index++;
                    if (question.getTotalScore() > 0) {
                        if (current == null) {
                            current = new JSONObject();
                            current.accumulate("start", index);
                        }
                    } else {
                        if (current != null) {
                            current.accumulate("end", index - 1);
                            array.add(current);
                            current = null;
                        }
                    }
                }
                if (current != null) {
                    current.accumulate("end", index);
                    array.add(current);
                }

                JSONObject obj = new JSONObject();
                obj.accumulate("subjectCode", subject.getCode());
                obj.accumulate("index", array);
                result.add(obj);
            }
        }
        return result;
    }

    /**
     * 针对客观题存在多种paperType的情况，只过滤出其中的一种paperType，获取一份完整的小题集合
     * 
     * @param list
     * @return
     */
    private List<ExamQuestion> distinct(List<ExamQuestion> origin) {
        List<ExamQuestion> list = new LinkedList<>();
        if (origin != null) {
            Set<String> numbers = new HashSet<>();
            for (ExamQuestion question : origin) {
                String number = question.getQuestionNumber();
                if (numbers.contains(number)) {
                    continue;
                } else {
                    list.add(question);
                    numbers.add(number);
                }
            }
        }
        return list;
    }
}
