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

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

import org.apache.commons.lang.StringUtils;

import cn.com.qmth.stmms.biz.exam.model.ExamQuestion;

public class SubjectQuestionDTO {

    private int examId;

    private String subjectCode;

    private List<ExamQuestion> list;

    private double totalScore;

    private boolean objective;

    public SubjectQuestionDTO(int examId, String subjectCode, boolean objective) {
        this.examId = examId;
        this.subjectCode = subjectCode;
        this.list = new LinkedList<ExamQuestion>();
        this.totalScore = 0d;
        this.objective = objective;
    }

    public void addQuestion(ExamQuestion question) {
        if (question != null && question.getSubjectCode().equals(subjectCode)) {
            question.setExamId(examId);
            list.add(question);

            totalScore += question.getTotalScore();
        }
    }

    public List<ExamQuestion> getQuestionList() {
        return list;
    }

    public String getSubjectCode() {
        return subjectCode;
    }

    public double getTotalScore() {
        return totalScore;
    }

    public boolean validate(List<String> error) {
        if (list.size() > 0) {
            Set<String> numberSet = new HashSet<String>();
            Map<Integer, String> titleMap = new HashMap<Integer, String>();
            for (ExamQuestion question : list) {
                if (StringUtils.isBlank(question.getMainTitle())) {
                    error.add("[" + subjectCode + "] 有大题名称为空的记录");
                    return false;
                }
                if (question.getMainTitle().length() > 30) {
                    error.add("[" + subjectCode + "] 有大题名称长度超过30个字的记录");
                    return false;
                }
                if (question.getMainNumber() == null || question.getSubNumber() == null) {
                    error.add("[" + subjectCode + "] 有题号为空的记录");
                    return false;
                }
                if (question.getTotalScore() == null) {
                    error.add("[" + subjectCode + "] 有满分为空的记录");
                    return false;
                }
                if (objective && StringUtils.isBlank(question.getAnswer())) {
                    error.add("[" + subjectCode + "] 有答案为空的记录");
                    return false;
                }

                String title = titleMap.get(question.getMainNumber());
                if (title != null && !title.equals(question.getMainTitle())) {
                    error.add("[" + subjectCode + "] 大题号" + question.getMainNumber() + "有名称不一致的记录");
                    return false;
                } else {
                    titleMap.put(question.getMainNumber(), question.getMainTitle());
                }

                String number = question.getMainNumber() + "-" + question.getSubNumber();
                if (numberSet.contains(number)) {
                    error.add("[" + subjectCode + "] 题号" + number + "有重复记录");
                    return false;
                } else {
                    numberSet.add(number);
                }

            }

            numberSet.clear();
            titleMap.clear();
            return true;
        } else {
            return false;
        }
    }
}
