/*
 * *************************************************
 * 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.platform.*;

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

/**
 * 试题
 */
public class QuestionDto implements Serializable {
    private static final long serialVersionUID = 2180336388838138300L;
    private String id;
    private Integer number;//题序号
    private Double score;//满分
    private Integer structType;//题类型
    private Boolean objective;//是否主观题
    private String body;//题干
    private String answer;//正确答案
    private List<QuestionOptionDto> options;//题选项列表
    private List<QuestionDto> subQuestions;//套题下的子题列表

    public QuestionDto(QuestionVo question) {
        this.id = question.getId();
        this.number = question.getNumber();
        this.score = question.getScore() != null ? question.getScore() : 1d;
        this.structType = question.getStructType();
        this.objective = question.getObjective();

        //转换题干内容
        StringBuilder bodyStr = new StringBuilder();
        BodyVo body = question.getBody();
        if (body != null && body.hasSection()) {
            for (SectionVo section : body.getSections()) {
                if (section.hasBlock()) {
                    for (BlockVo block : section.getBlocks()) {
                        if (Constants.TEXT.equals(block.getType())) {
                            bodyStr.append(block.getValue());
                        } else if (Constants.IMAGE.equals(block.getType())) {
                            ParamVo param = block.getParam();
                            if (param != null) {
                                bodyStr.append(String.format(Constants.TAG_IMG, param.getStyle(), block.getValue()));
                            } else {
                                bodyStr.append(String.format(Constants.TAG_IMG, "", block.getValue()));
                            }
                        }
                    }
                }
            }
        }
        this.body = bodyStr.toString();

        //转换选项内容
        if (question.hasOption()) {
            for (OptionVo option : question.getOptions()) {
                StringBuilder optionBodyStr = new StringBuilder();
                BodyVo optionBody = option.getBody();
                if (optionBody != null && optionBody.hasSection()) {
                    for (SectionVo section : optionBody.getSections()) {
                        if (section.hasBlock()) {
                            for (BlockVo block : section.getBlocks()) {
                                if (Constants.TEXT.equals(block.getType())) {
                                    optionBodyStr.append(block.getValue());
                                } else if (Constants.IMAGE.equals(block.getType())) {
                                    ParamVo param = block.getParam();
                                    if (param != null) {
                                        optionBodyStr.append(String.format(Constants.TAG_IMG, param.getStyle(), block.getValue()));
                                    } else {
                                        optionBodyStr.append(String.format(Constants.TAG_IMG, "", block.getValue()));
                                    }
                                }
                            }
                        }
                    }
                }
                QuestionOptionDto newOption = new QuestionOptionDto();
                newOption.setNumber(option.getNumber());
                newOption.setBody(optionBodyStr.toString());
                this.addOption(newOption);
            }
        }
    }

    public void addOption(QuestionOptionDto option) {
        if (options == null) {
            options = new ArrayList<>();
        }
        options.add(option);
    }

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

    public QuestionDto() {

    }

    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 Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    public Integer getStructType() {
        return structType;
    }

    public void setStructType(Integer structType) {
        this.structType = structType;
    }

    public Boolean getObjective() {
        return objective;
    }

    public void setObjective(Boolean objective) {
        this.objective = objective;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    public String getAnswer() {
        return answer;
    }

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

    public List<QuestionOptionDto> getOptions() {
        if (options == null) {
            return new ArrayList<>();
        }
        return options;
    }

    public void setOptions(List<QuestionOptionDto> options) {
        this.options = options;
    }

    public List<QuestionDto> getSubQuestions() {
        if (subQuestions == null) {
            return new ArrayList<>();
        }
        return subQuestions;
    }

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

}