/*
 * *************************************************
 * Copyright (c) 2018 QMTH. All Rights Reserved.
 * Created by Deason on 2018-07-12 15:31:10.
 * *************************************************
 */

package cn.com.qmth.examcloud.core.questions.base.converter.model.dps;

import cn.com.qmth.examcloud.core.questions.base.converter.model.Constants;
import cn.com.qmth.examcloud.core.questions.base.converter.model.QuesStructType;
import cn.com.qmth.examcloud.core.questions.base.converter.model.platform.AnswerVo;
import cn.com.qmth.examcloud.core.questions.base.converter.model.platform.BlockVo;
import cn.com.qmth.examcloud.core.questions.base.converter.model.platform.QuestionVo;
import cn.com.qmth.examcloud.core.questions.base.converter.model.platform.SectionVo;
import cn.com.qmth.examcloud.core.questions.base.converter.utils.NumUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 试题
 */
public class SimpleQuestionDto implements Serializable {
    private static final long serialVersionUID = 2180336388838138300L;
    private String id;
    private Integer number;//题号
    private String answer;//正确答案
    private List<SimpleQuestionDto> subQuestions;//套题下的子题列表

    public SimpleQuestionDto(QuestionVo question) {
        this.id = question.getId();
        QuesStructType type = QuesStructType.getTypeById(question.getStructType());
        if (type == null) {
            throw new IllegalArgumentException("Question struct type is wrong");
        }
        //转换答案内容
        StringBuilder answerStr = new StringBuilder();
        AnswerVo answer = question.getAnswer();
        if (answer != null && answer.hasSection()) {
            for (SectionVo section : answer.getSections()) {
                if (section.hasBlock()) {
                    for (BlockVo block : section.getBlocks()) {
                        if (Constants.TEXT.equals(block.getType())) {
                            String value = block.getValue();
                            if (type.isObjective()) {
                                if (QuesStructType.SINGLE_ANSWER_QUESTION.getId() == type.getId()) {
                                    value = NumUtil.toNumber(value);
                                } else if (QuesStructType.MULTIPLE_ANSWER_QUESTION.getId() == type.getId()) {
                                    value = NumUtil.toNumber(value);
                                } else if (QuesStructType.BOOL_ANSWER_QUESTION.getId() == type.getId()) {
                                    if (Constants.CORRECT.equals(value)) {
                                        value = Constants.CORRECT_NUM;
                                    } else {
                                        value = Constants.WRONG_NUM;
                                    }
                                }
                            }
                            answerStr.append(value);
                        }
                    }
                }
            }
        }
        this.answer = answerStr.toString();
    }

    public void addSubQuestion(SimpleQuestionDto question) {
        if (subQuestions == null) {
            subQuestions = new ArrayList<>();
        }
        subQuestions.add(question);
    }

    public SimpleQuestionDto() {

    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public List<SimpleQuestionDto> getSubQuestions() {
        return subQuestions;
    }

    public void setSubQuestions(List<SimpleQuestionDto> subQuestions) {
        this.subQuestions = subQuestions;
    }

}