package com.cirle.scientific.questionnaire.valueobject;

import com.cirle.scientific.questionnaire.entity.QuestionEntity;
import com.cirle.scientific.questionnaire.entity.QuestionGroupEntity;
import com.cirle.scientific.questionnaire.entity.QuestionOptionEntity;
import com.cirle.scientific.questionnaire.entity.QuestionnaireEntity;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

/**
 * 获取所有问卷值对象
 */
@Data
@ApiModel("问卷查询结果对象")
public class GetQuestionnaireVO {

    @ApiModelProperty("问卷ID")
    private String id;

    @ApiModelProperty("问卷名")
    private String name;

    @ApiModelProperty("问卷备注")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String remark;

    @ApiModelProperty("问卷创建时间")
    private LocalDateTime createTime;

    @ApiModelProperty("问卷最近一次修改时间")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private LocalDateTime lastModifiedTime;

    @ApiModelProperty("问卷下的所有问题组")
    private List<QuestionGroup> questionGroups;

    public void parse(QuestionnaireEntity questionnaireEntity) {
        setId(questionnaireEntity.getId());
        setName(questionnaireEntity.getName());
        setRemark(questionnaireEntity.getRemark());
        setCreateTime(questionnaireEntity.getCreateTime());
        setLastModifiedTime(questionnaireEntity.getLastModifiedTime());
        Set<QuestionGroupEntity> questionGroupEntities = questionnaireEntity.getQuestionGroupEntities();
        if (questionGroupEntities == null) {
            return;
        }
        if (questionGroups == null) {
            questionGroups = new ArrayList<>();
        }
        for (QuestionGroupEntity questionGroupEntity : questionGroupEntities) {
            QuestionGroup questionGroup = new QuestionGroup();
            questionGroup.parse(questionGroupEntity);
            questionGroups.add(questionGroup);
        }
        questionGroups.sort(Comparator.comparing(QuestionGroup::getNo));
    }

    @Data
    public static class QuestionGroup {
        @ApiModelProperty("问题组ID")
        private String id;

        @ApiModelProperty("问题组序号")
        private Integer no;

        @ApiModelProperty("问题组名")
        private String name;

        @ApiModelProperty("问题组下的所有问题")
        private List<Question> questions;

        private void parse(QuestionGroupEntity questionGroupEntity) {
            setId(questionGroupEntity.getId());
            setNo(questionGroupEntity.getNo());
            setName(questionGroupEntity.getName());
            Set<QuestionEntity> questionEntities = questionGroupEntity.getQuestionEntities();
            if (questionEntities == null) {
                return;
            }
            if (questions == null) {
                questions = new ArrayList<>();
            }
            for (QuestionEntity questionEntity : questionEntities) {
                Question question = new Question();
                question.parse(questionEntity);
                questions.add(question);
            }
            questions.sort(Comparator.comparing(Question::getLabel));
        }
    }

    @Data
    public static class Question {
        @ApiModelProperty("问题ID")
        private String id;

        @ApiModelProperty("问题标签（通常为题号）")
        private String label;

        @ApiModelProperty("问题文本（即题干）")
        private String text;

        @ApiModelProperty("问题类型")
        private String type;

        @ApiModelProperty("必填标志（当该值为True时，必须提供该问题的答案）")
        private boolean required;

        @ApiModelProperty("问题下的所有选项（仅问题类型为判断题、单选题、多选题时该字段才存在）")
        @JsonInclude(JsonInclude.Include.NON_NULL)
        private List<QuestionOption> questionOptions;

        private void parse(QuestionEntity questionEntity) {
            setId(questionEntity.getId());
            setLabel(questionEntity.getLabel());
            setText(questionEntity.getText());
            setType(questionEntity.getType().name());
            setRequired(questionEntity.getRequired());
            Set<QuestionOptionEntity> questionOptionEntities = questionEntity.getQuestionOptionEntities();
            if (questionOptionEntities == null) {
                return;
            }
            if (questionOptions == null) {
                questionOptions = new ArrayList<>();
            }
            for (QuestionOptionEntity questionOptionEntity : questionOptionEntities) {
                QuestionOption questionOption = new QuestionOption();
                questionOption.parse(questionOptionEntity);
                questionOptions.add(questionOption);
            }
            questionOptions.sort(Comparator.comparing(QuestionOption::getLabel));
        }
    }

    @Data
    public static class QuestionOption {
        @ApiModelProperty("选项ID")
        private String id;

        @ApiModelProperty("选项标签（通常为A、B、C、D等）")
        private String label;

        @ApiModelProperty("选项文本")
        private String text;

        private void parse(QuestionOptionEntity entity) {
            setId(entity.getId());
            setLabel(entity.getLabel());
            setText(entity.getText());
        }
    }

}
