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

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class ScoreCalculateUtil {

    protected static Logger log = LoggerFactory.getLogger(ScoreCalculateUtil.class);

    private ExamStudent student;

    private ScoreCalculateUtil(ExamStudent student) {
        this.student = student;
    }

    public ScoreInfo calculate(List<ExamQuestion> oList, List<ExamQuestion> sList) {
        // log.debug("start calculate, examId=" + student.getExamId() +
        // ", examNumber=" + student.getExamNumber());

        ScoreInfo info = new ScoreInfo();
        if (oList != null && oList.size() > 0) {
            calculateObjective(oList, info);
        }
        if (sList != null && sList.size() > 0) {
            calculateSubjective(sList, info);
        }
        return info;
    }

    private void calculateObjective(List<ExamQuestion> questionList, ScoreInfo scoreInfo) {
        // log.debug("start calculate objective score");
        List<String> answerList = student.getAnswerList();
        // Map<String, Integer> config = exam.getObjectiveBlockMap();

        // log.debug("student answers: " + student.getAnswers());
        // log.debug("exam objective config: " + exam.getObjectiveBlock());

        int index = -1;
        int lastMainNumber = 0;
        for (ExamQuestion question : questionList) {
            // log.debug("find objective block for " + block.getTitle() +
            // ", question start=" + start);
            if (question.getMainNumber() != lastMainNumber) {
                scoreInfo.incrMainNumber(question.getMainTitle());
            }

            index++;
            String answer = "#";
            double score = 0d;
            try {
                answer = answerList.get(index);
            } catch (Exception e) {
                continue;
            }
            boolean correct = true;
            for (int i = 0; i < answer.length(); i++) {
                if (!question.getAnswer().contains(String.valueOf(answer.charAt(i)))) {
                    correct = false;
                    break;
                }
            }
            if (correct) {
                score = answer.length() < question.getAnswer().length() ? 0 : question.getTotalScore();
            }

            // log.debug("question result: " + question.getNumber() + ", " +
            // answer + ", " + score);
            scoreInfo.addScore(null, question.getSubNumber(), score, answer, true);
        }
    }

    private void calculateSubjective(List<ExamQuestion> sList, ScoreInfo scoreInfo) {

    }

    public static ScoreCalculateUtil instance(ExamStudent student) {
        return new ScoreCalculateUtil(student);
    }

    public static void main(String[] args) {
        ExamStudent student = new ExamStudent();
        student.setAnswers("A,B,AB,C,#,#,#,#");
        ScoreCalculateUtil util = ScoreCalculateUtil.instance(student);

        List<ExamQuestion> oList = new LinkedList<ExamQuestion>();
        ExamQuestion q = new ExamQuestion();
        q.setMainNumber(1);
        q.setMainTitle("单选");
        q.setSubNumber(1);
        q.setAnswer("B");
        q.setTotalScore(2d);
        oList.add(q);

        q = new ExamQuestion();
        q.setMainNumber(1);
        q.setMainTitle("单选");
        q.setSubNumber(2);
        q.setAnswer("B");
        q.setTotalScore(2d);
        oList.add(q);

        q = new ExamQuestion();
        q.setMainNumber(2);
        q.setMainTitle("多选");
        q.setSubNumber(1);
        q.setAnswer("AC");
        q.setTotalScore(3d);
        oList.add(q);

        q = new ExamQuestion();
        q.setMainNumber(2);
        q.setMainTitle("多选");
        q.setSubNumber(2);
        q.setAnswer("C");
        q.setTotalScore(3d);
        oList.add(q);

        ScoreInfo info = util.calculate(oList, null);
        System.out.println(info.getObjectiveScore());
        for (ScoreItem item : info.getScoreList()) {
            System.out.println(item.getTitle() + ": " + item.toString());
        }
    }
}
