package com.ruoyi.learn.domain;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.enums.QuestionType;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Arrays;
import java.util.List;

@Data
@ApiModel("试卷")
public class PaperVO {
    private Long id;
    private Long subjectId;
    private String paperName;
    private Long year;
    private String paperType;
    private String number;
    private Long score;
    private String directKey;

    @ApiModelProperty("试题")
    List<QuestionSeq> questions;

    @Data
    @ApiModel("试题")
    public static class QuestionSeq {
        private Long id;
        private Long knowledgeId;
        private String title;
        private String answer1;
        @ApiModelProperty("非标准格式答案或含部分过程说明的答案")
        private String answer2;
        private String parse;
        @ApiModelProperty("序号")
        Integer seq;
        @ApiModelProperty("考题类型 question_type")
        Integer typeId;
        @ApiModelProperty("考题类型名称")
        String type;
        @ApiModelProperty("考题类型名称")
        String qtpye;
        @ApiModelProperty("分数无用")
        Integer score;
        @ApiModelProperty("原题型")
        String typeTitle;
        @ApiModelProperty("总分数")
        Integer totalScore;
        @ApiModelProperty("选项数组")
        List<String> options;

        List<QuestionSeq> subQuestions;
    }


    @Data
    @ApiModel("试题")
    public static class QuestionAnswer {
        private Long answerId;
        @ApiModelProperty("题ID")
        private Long id;
        private Long knowledgeId;
        private String title;
        private String answer1;
        @ApiModelProperty("非标准格式答案或含部分过程说明的答案")
        private String answer2;
        private String parse;
        @ApiModelProperty("序号")
        Integer seq;
        @ApiModelProperty("考题类型 question_type")
        Integer typeId;

        @ApiModelProperty("本卷中标记")
        Boolean isMark;
        @ApiModelProperty("是否收藏")
        Boolean isFavorite;
        @ApiModelProperty("不会做,转State")
        Boolean isNotKnow;
        @ApiModelProperty("学生回答")
        List<String> answers;

        @ApiModelProperty("答题正确状态 0 默认 1正确 2错误 3不会 4 未答")
        Integer state;
        @ApiModelProperty("试题标准分")
        Integer totalScore;
        @ApiModelProperty("学生得分")
        Integer score;
        @ApiModelProperty("做题时长")
        private Long duration;

        List<QuestionAnswer> subQuestions;

        public Integer calcState(QuestionAnswer std, boolean isDone) {
            if(!isDone) {
                return 0;
            }
            if(null != isNotKnow && isNotKnow) {
                return 3;
            } else {
                QuestionType qt = QuestionType.of(this.typeId);
                if(QuestionType.Single.equals(qt) || QuestionType.Judgment.equals(qt)) {
                    if(CollectionUtils.isEmpty(answers)) {
                        return 4;
                    }
                    return answers.get(0).equals(std.getAnswer1()) ? 1 : 2;
                } else if(QuestionType.Multiple.equals(qt)) {
                    if(CollectionUtils.isEmpty(answers)) {
                        return 4;
                    }
                    List<String> stdAnswers = StringUtils.charStr2List(std.answer1); // 多选的 answer1 无逗号分隔
                    return stdAnswers.size() == answers.size() && CollectionUtils.intersection(stdAnswers, answers).size() == answers.size() ? 1 : 2;
                } else {
                    if(CollectionUtils.isEmpty(answers)) {
                        return 4;
                    }
                    return answers.get(0).equals("A") ? 1 : 2;
                }
            }
        }
    }
}
