package cn.com.qmth.examcloud.core.questions.service.bean;

import cn.com.qmth.examcloud.core.questions.base.question.enums.QuesStructType;
import cn.com.qmth.examcloud.core.questions.dao.entity.QuesOption;
import cn.com.qmth.examcloud.core.questions.dao.entity.Question;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class QuestionAnswerConvert {

    /**
     * 设置试题答案
     */
    public static List<String> parseQuestionAnswerList(Question question) {
        List<String> values = new ArrayList<>();

        if (question.getQuestionType() == QuesStructType.BOOL_ANSWER_QUESTION) {
            //判断题答案
            if (question.getQuesAnswer().equals("正确")) {
                values.add("true");
            } else if (question.getQuesAnswer().equals("错误")) {
                values.add("false");
            }
            return values;
        }

        /*if (question.getQuestionType() == QuesStructType.BOOL_ANSWER_QUESTION) {
            //填空题答案
            String[] rightAnswers = question.getQuesAnswer().split("##");
            for (String value : rightAnswers) {
                values.add(value);
            }
            return values;
        }*/

        values.add(question.getQuesAnswer());
        return values;
    }

    public static String[] parseQuestionAnswers(Question question) {
        List<String> rightAnswers = parseQuestionAnswerList(question);

        if (rightAnswers.size() == 0) {
            return new String[0];
        }

        return rightAnswers.toArray(new String[rightAnswers.size()]);
    }

    /**
     * 设置单选题和多选题答案
     */
    public static List<String> parseQuestionOptionAnswerList(List<QuesOption> options) {
        List<String> values = new ArrayList<>();

        if (CollectionUtils.isNotEmpty(options)) {
            for (int i = 0; i < options.size(); i++) {
                QuesOption option = options.get(i);
                if (option.getIsCorrect() == 1) {
                    values.add(String.valueOf(i));
                }
            }
        }

        return values;
    }

    public static String[] parseQuestionOptionAnswers(List<QuesOption> options) {
        List<String> rightAnswers = parseQuestionOptionAnswerList(options);

        if (rightAnswers.size() == 0) {
            return new String[0];
        }

        return rightAnswers.toArray(new String[rightAnswers.size()]);
    }

}