package com.ningdatech.carapi.archives.assembler;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import com.ningdatech.carapi.archives.entity.PaperQuestion;
import com.ningdatech.carapi.archives.model.JudgmentalQuestionTextContent;
import com.ningdatech.carapi.archives.model.MultipleChoiceQuestionTextContent;
import com.ningdatech.carapi.archives.model.SingleChoiceQuestionTextContent;
import com.ningdatech.carapi.archives.model.vo.ResQuestionDetailVO;
import com.ningdatech.carapi.driver.constant.QuestionTypeEnum;

/**
 * @author liuxinxin
 * @date 2022/10/22 下午3:36
 */

public class QuestionAssembler {

    public static ResQuestionDetailVO buildResQuestionDetailVO(PaperQuestion paperQuestion) {
        ResQuestionDetailVO resQuestionDetailVO = new ResQuestionDetailVO();
        QuestionTypeEnum questionTypeEnum = QuestionTypeEnum.valueOf(paperQuestion.getQuestionType());
        String questionTextContent = paperQuestion.getQuestionTextContent();
        switch (questionTypeEnum) {
            case MULTIPLE_CHOICE:
                MultipleChoiceQuestionTextContent multipleChoiceQuestionTextContent
                        = JSONObject.parseObject(questionTextContent, MultipleChoiceQuestionTextContent.class);
                resQuestionDetailVO.setChoiceQuestionCorrectAnswer(multipleChoiceQuestionTextContent.getAnswer());
                resQuestionDetailVO.setOptions(multipleChoiceQuestionTextContent.getOptions());
                break;
            case SINGLE_CHOICE:
                SingleChoiceQuestionTextContent singleChoiceQuestionTextContent
                        = JSONObject.parseObject(questionTextContent, SingleChoiceQuestionTextContent.class);
                resQuestionDetailVO.setChoiceQuestionCorrectAnswer(CollUtil.toList(singleChoiceQuestionTextContent.getAnswer()));
                resQuestionDetailVO.setOptions(singleChoiceQuestionTextContent.getOptions());
                break;
            case JUDGMENTAL:
                JudgmentalQuestionTextContent judgmentalQuestionTextContent
                        = JSONObject.parseObject(questionTextContent, JudgmentalQuestionTextContent.class);
                resQuestionDetailVO.setJudgmentalQuestionCorrectAnswer(judgmentalQuestionTextContent.getAnswer());
                break;
            default:
        }
        resQuestionDetailVO.setQuestionType(QuestionTypeEnum.valueOf(paperQuestion.getQuestionType()));
        resQuestionDetailVO.setStem(paperQuestion.getStem());
        resQuestionDetailVO.setId(paperQuestion.getId());
        return resQuestionDetailVO;
    }
}
