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

import cn.com.qmth.examcloud.core.questions.base.question.QuesOptionDto;
import cn.com.qmth.examcloud.core.questions.base.question.SubQuestionDto;
import cn.com.qmth.examcloud.core.questions.dao.entity.QuesOption;
import cn.com.qmth.examcloud.core.questions.dao.entity.Question;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by songyue on 18/1/5.
 */
@Component
public class SubQuestionDtoAssembler {

    public SubQuestionDto toDto(Question question) {
        SubQuestionDto subQuestionDto = new SubQuestionDto();
        subQuestionDto.setQuestionType(question.getQuestionType());
        subQuestionDto.setNumber(question.getNumber());
        subQuestionDto.setScore(question.getScore());
        subQuestionDto.setQuesAnswer(question.getQuesAnswer());
        subQuestionDto.setQuesBody(question.getQuesBody());
        subQuestionDto.setQuesParams(question.getQuesParams());
        subQuestionDto.setQuesOptions(toOptionDtoList(question.getQuesOptions()));
        return subQuestionDto;
    }

    public QuesOptionDto toOptionDto(QuesOption quesOption) {
        QuesOptionDto quesOptionDto = new QuesOptionDto();
        quesOptionDto.setNumber(quesOption.getNumber());
        quesOptionDto.setOptionBody(quesOption.getOptionBody());
        return quesOptionDto;
    }

    public List<SubQuestionDto> toDtoList(List<Question> questionList) {
        if (questionList == null || questionList.size() == 0) {
            return new ArrayList<SubQuestionDto>();
        }
        return questionList.stream()
                .map(question -> toDto(question))
                .collect(Collectors.toList());
    }

    public List<QuesOptionDto> toOptionDtoList(List<QuesOption> quesOptionList) {
        if (quesOptionList == null || quesOptionList.size() == 0) {
            return new ArrayList<QuesOptionDto>();
        }
        return quesOptionList.stream()
                .map(quesOption -> toOptionDto(quesOption))
                .collect(Collectors.toList());
    }
}
