package cn.com.qmth.stmms.biz.report.utils.module;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import cn.com.qmth.stmms.biz.exam.model.ExamQuestion;
import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
import cn.com.qmth.stmms.biz.report.model.ReportSubjectQuestion;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectQuestionService;
import cn.com.qmth.stmms.biz.report.utils.Module;
import cn.com.qmth.stmms.biz.report.utils.ReportContext;
import cn.com.qmth.stmms.biz.report.utils.unit.BaseCalculatorUnit;
import cn.com.qmth.stmms.biz.utils.ScoreItem;
import cn.com.qmth.stmms.biz.utils.SpringContextHolder;

public class SubjectQuestionModule implements Module {

    private Map<String, BaseCalculatorUnit> units;

    private ReportContext context;

    public SubjectQuestionModule(ReportContext context) {
        this.units = new HashMap<String, BaseCalculatorUnit>();
        this.context = context;
    }

    public void process(ExamStudent student) {
        if (student.isUpload() && !student.isAbsent() && !student.isBreach() && student.getSubject() != null) {
            double totalScore = student.getTotalScore();
            // 遍历客观题
            process(student.getScoreList(true), student.getObjectiveQuestionList(), totalScore);
            // 遍历主观题
            process(student.getScoreList(false), student.getSubjectiveQuestionList(), totalScore);
        }
    }

    private void process(List<ScoreItem> scoreList, List<ExamQuestion> questionList, double totalScore) {
        if (scoreList == null || questionList == null) {
            return;
        }
        int scoreCount = scoreList.size();
        for (int i = 0; i < questionList.size(); i++) {
            ExamQuestion question = questionList.get(i);
            if (question.getTotalScore() == null || question.getTotalScore() == 0) {
                continue;
            }
            ScoreItem item = scoreCount > i ? scoreList.get(i) : null;
            double score = item != null ? item.getScore() : 0;
            findUnit(getKey(question)).process(score, question.getTotalScore(), totalScore);
        }
    }

    public String getKey(ExamQuestion question) {
        return question.getSubjectCode() + "\t" + question.isObjective() + "\t"
                + StringUtils.trimToEmpty(question.getPaperType()) + "\t" + question.getMainNumber() + "\t"
                + question.getSubNumber();
    }

    public void save() {
        ReportSubjectQuestionService service = SpringContextHolder.getBean(ReportSubjectQuestionService.class);
        for (String key : this.units.keySet()) {
            String s[] = key.split("\t");
            if (s.length > 4) {
                String subjectCode = s[0];
                Boolean objective = Boolean.parseBoolean(s[1]);
                String paperType = StringUtils.trimToNull(s[2]);
                Integer mainNumber = Integer.parseInt(s[3]);
                Integer subNumber = Integer.parseInt(s[4]);
                BaseCalculatorUnit unit = units.get(key);
                ReportSubjectQuestion reportSubjectQuestion = new ReportSubjectQuestion();
                reportSubjectQuestion.setExamId(this.context.getExamId());
                reportSubjectQuestion.setSubjectCode(subjectCode);
                reportSubjectQuestion.setSubjectName(context.getSubject(subjectCode).getName());
                reportSubjectQuestion.setObjective(objective);
                reportSubjectQuestion.setQuestionName(context.getExamQuestion(key).getMainTitle());
                reportSubjectQuestion.setMainNumber(mainNumber);
                reportSubjectQuestion.setSubNumber(subNumber);
                reportSubjectQuestion.setTotalScore(unit.fullScore);
                reportSubjectQuestion.setAvgScore(unit.avgScore);
                reportSubjectQuestion.setScoreRate(unit.difficulty);
                reportSubjectQuestion.setStdev(unit.stdev);
                reportSubjectQuestion.setFullScoreRate(unit.fullScoreRate);
                reportSubjectQuestion.setPaperType(paperType);
                service.save(reportSubjectQuestion);
                
            }
        }
    }

    private BaseCalculatorUnit findUnit(String key) {
        BaseCalculatorUnit unit = units.get(key);
        if (unit == null) {
            unit = new BaseCalculatorUnit();
            units.put(key, unit);
        }
        return unit;
    }

}
