package com.ssj.service.kmt.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.ssj.service.kmt.dto.exercise.ExerciseQuestionAnswerSpace;
import org.apache.commons.collections.CollectionUtils;

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

public class ExerciseQuestionDto implements Serializable {

    private static final long serialVersionUID = 1L;

    private String questionId;
    private String bigQuestionIndex = "";
    private String smallQuestionIndex;
    private String optionAnswer;
    private String bookCourseId = "";
    private String answerJson;
    private Integer dealStatus;
    private List<ExerciseQuestionAnswerDto> answers = new ArrayList<>();
    private List<List<ExerciseQuestionAnswerSpace>> json = new ArrayList<List<ExerciseQuestionAnswerSpace>>();
    private List<ExerciseQuestionAnswerSpace> answerJsonV1 = new ArrayList<>();

    public Integer getDealStatus() {
		return dealStatus;
	}
	public void setDealStatus(Integer dealStatus) {
		this.dealStatus = dealStatus;
	}
	public String getQuestionId() {
        return questionId;
    }
    public void setQuestionId(String questionId) {
        this.questionId = questionId;
    }
    @JSONField(serialize=false)
    public String getBigQuestionIndex() {
        return bigQuestionIndex;
    }

    public void setBigQuestionIndex(String bigQuestionIndex) {
        this.bigQuestionIndex = bigQuestionIndex;
    }

    public String getSmallQuestionIndex() {
        return smallQuestionIndex;
    }

    public void setSmallQuestionIndex(String smallQuestionIndex) {
        //如果小题号为0或者(0)则设置为空字符串
        if("0".equals(smallQuestionIndex) || "(0)".equals(smallQuestionIndex)) {
            this.smallQuestionIndex = "";
        }else {
            this.smallQuestionIndex = smallQuestionIndex;
        }
    }

    @JSONField(serialize=false)
    public String getBookCourseId() {
        return bookCourseId;
    }

    public void setBookCourseId(String bookCourseId) {
        this.bookCourseId = bookCourseId;
    }

    @JSONField(serialize=false)
    public String getOptionAnswer() {
        return optionAnswer;
    }

    public void setOptionAnswer(String optionAnswer) {
        this.optionAnswer = optionAnswer;
//        if (StringUtil.isNotBlank(optionAnswer)){
//            String[] answer = optionAnswer.split("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|#&");
//            this.addAnswers((Arrays.asList(answer)));
//        }
    }

    public List<ExerciseQuestionAnswerDto> getAnswers() {
        return answers;
    }
    public void addAnswers (List<String> answers) {
        if (answers != null){
            for (String an : answers){
                ExerciseQuestionAnswerDto base = new ExerciseQuestionAnswerDto(an);
                this.answers.add(base);
            }
        }
    }

    @JSONField(serialize=false)
    public String getAnswerJson() {
        return this.answerJson;
    }

    public void setAnswerJson(String answerJson) {
        this.answerJson = answerJson;
        List<ExerciseQuestionAnswerSpace> questionAnswers = JSON.parseArray(answerJson, ExerciseQuestionAnswerSpace.class);
        int index = 1;
        if (questionAnswers != null && questionAnswers.size() > 0) {
            for (ExerciseQuestionAnswerSpace questionAnswer : questionAnswers) {
                questionAnswer.setIndex(index);
                index++;
            }
            this.answerJsonV1 = questionAnswers;
            this.json.add(questionAnswers);
        }
    }

    /**
     * 设置空的选择类型
     * @param index
     * @param type
     */
    public void setAnswerSelectedType (int index, int type){
        if (this.json != null && this.json.size() >0){
            for (List<ExerciseQuestionAnswerSpace> spaces : this.json){
                for (ExerciseQuestionAnswerSpace space : spaces){
                    if (index == space.getIndex()){
                        space.setSelectedType(type);
                    }
                }
            }
        }

        //answerjsonv1
        if (CollectionUtils.isNotEmpty(this.answerJsonV1)){
            for (ExerciseQuestionAnswerSpace space : this.answerJsonV1){
                if (index == space.getIndex()){
                    space.setSelectedType(type);
                }
            }
        }
    }
    /**
     * 设置所有的空都为选择类型
     * @param type
     */
    public void setAllAnswerSelectedType (int type){
        if (this.json != null && this.json.size() >0){
            for (List<ExerciseQuestionAnswerSpace> spaces : this.json){
                for (ExerciseQuestionAnswerSpace space : spaces){
                    space.setSelectedType(type);
                }
            }
        }
        //answerjsonv1
        if (CollectionUtils.isNotEmpty(this.answerJsonV1)){
            for (ExerciseQuestionAnswerSpace space : this.answerJsonV1){
                space.setSelectedType(type);
            }
        }
    }

    public List<List<ExerciseQuestionAnswerSpace>> getJson() {
        return json;
    }

    public void setJson(List<List<ExerciseQuestionAnswerSpace>> json) {
        this.json = json;
    }

    public List<ExerciseQuestionAnswerSpace> getAnswerJsonV1() {
        return answerJsonV1;
    }

    public void setAnswerJsonV1(List<ExerciseQuestionAnswerSpace> answerJsonV1) {
        this.answerJsonV1 = answerJsonV1;
    }
}
