package com.ssj.service.kmt.dto;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.annotation.JSONField;
import com.ssj.bean.kmt.kmtsys.domain.KmtSysUser;
import com.ssj.bean.weixin.libmy.domain.TbLibManager;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.conch.schoolmaster.util.Constant;
import com.ssj.service.kmt.dto.exercise.ExerciseQuestionAnswerSpace;
import com.ssj.service.kmt.dto.exercise.QuestionSubjectAttribute;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

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

public class ExerciseQuetionDto implements Serializable {
    private static final long serialVersionUID = 1L;

    private String questionId;

    private String bookName;
    private String unit;
    private String period;
    private String courseName;

    private List<KnowHowDetailsDTO> knowledges = new ArrayList<>();

    private String question;

    private String originAnswer;

    private String answer;

    private String dealUser;

    private String addUser;

    private String auditingUser;

    private String analysisDealUser;

    private String analysisAuditingUser;

    private String createTime;

    private String reason;

    private String questionText;

    private List<ExerciseQuestionAnswerSpace> answerJson;

    private String smallQuestionIndex;

    private int questionStatus;

    private String courseId;

    private String bigQuestionIndex;

    private String questionAnalysis;

    private List<ExerciseQuestionAnswerSpace> analysisJson;

    private List<KnowledgeBaseDto> questionTypes;

    private List<KnowledgeBaseDto> difficultyLevels;

    private List<KnowledgeBaseDto> realExamTypes;

    private List<KnowledgeBaseDto> trainingSkills;

    private String parentId;

    private String grade;

    private String subject;

    private int semester;

    private int versionType;

    private String versionName;

    private int moduleType;

    private  int num;

    private String questionImage;

    @JSONField(serialize = false)
    private String handleUserId;

    @JSONField(serialize = false)
    private String auditingUserId;

    @JSONField(serialize = false)
    private String analysisUserId;

    @JSONField(serialize = false)
    private String analysisAuditerId;

    @JSONField(serialize = false)
    private String fromUserId;

    @JSONField(serialize = false)
    private String toUserId;

    private String fromUser;

    private String toUser;

    @JSONField(serialize = false)
    private String updateType;

    private int proofreadStatus;

    @JSONField(serialize = false)
    private Integer beginYear;

    @JSONField(serialize = false)
    private Integer endYear;

    private String yearName;

    private String questionProvince;

    private String questionCity;

    private String questionCountry;

    @JSONField(serialize = false)
    private String questionPath;

    private String errorReasonImage;


    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getQuestionId() {
        return questionId;
    }

    public void setQuestionId(String questionId) {
        this.questionId = questionId;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public String getPeriod() {
        return period;
    }

    public void setPeriod(String period) {
        this.period = period;
    }

    public List<KnowHowDetailsDTO> getKnowledges() {
        return knowledges;
    }

    public void setKnowledges(List<KnowHowDetailsDTO> knowledges) {
        this.knowledges = knowledges;
    }

    public String getQuestion() {
        return question;
    }

    public void setQuestion(String question) {
        this.question = question;
    }

    public String getAnswer() {
        return answer;
    }

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

    public String getDealUser() {
        return dealUser;
    }

    public void setDealUser(String dealUser) {
        this.dealUser = dealUser;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }

    public String getAuditingUser() {
        return auditingUser;
    }

    public void setAuditingUser(String auditingUser) {
        this.auditingUser = auditingUser;
    }

    public String getAnalysisDealUser() {
        return analysisDealUser;
    }

    public void setAnalysisDealUser(String analysisDealUser) {
        this.analysisDealUser = analysisDealUser;
    }

    public String getAnalysisAuditingUser() {
        return analysisAuditingUser;
    }

    public void setAnalysisAuditingUser(String analysisAuditingUser) {
        this.analysisAuditingUser = analysisAuditingUser;
    }

    public String getQuestionText() {
        return questionText;
    }

    public void setQuestionText(String questionText) {
        this.questionText = questionText;
    }

    public List<ExerciseQuestionAnswerSpace> getAnswerJson() {
        return answerJson;
    }

    public void setAnswerJson(String answerJson) {
        if (StringUtil.isNotBlank(answerJson)){
            try {
                this.answerJson = JSON.parseArray(answerJson, ExerciseQuestionAnswerSpace.class);
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public String getSmallQuestionIndex() {
        return smallQuestionIndex;
    }

    public void setSmallQuestionIndex(String smallQuestionIndex) {
        this.smallQuestionIndex = smallQuestionIndex;
    }

    public int getQuestionStatus() {
        return questionStatus;
    }

    public void setQuestionStatus(int questionStatus) {
        if (questionStatus == 0 || questionStatus == 1){
            this.questionStatus = 1;
        } else if (questionStatus == 3 || questionStatus == 7){
            this.questionStatus = 3;
        } else if (questionStatus == 5 || questionStatus == 8) {
            this.questionStatus = 8;
        } else if (questionStatus == 9 || questionStatus == 10) {
            this.questionStatus = 10;
        } else {
            this.questionStatus = questionStatus;
        }
    }

    @JSONField(serialize=false)
    public String getCourseId() {
        return courseId;
    }

    public void setCourseId(String courseId) {
        this.courseId = courseId;
    }

    @JSONField(serialize=false)
    public String getBigQuestionIndex() {
        return bigQuestionIndex;
    }

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

    public String getQuestionAnalysis() {
        return questionAnalysis;
    }

    public void setQuestionAnalysis(String questionAnalysis) {
        this.questionAnalysis = questionAnalysis;
    }

    public List<ExerciseQuestionAnswerSpace> getAnalysisJson() {
        return analysisJson;
    }

    public void setAnalysisJson(String analysisJson) {
        if (StringUtil.isNotBlank(analysisJson)){
            try {
                this.analysisJson = JSONArray.parseArray(analysisJson, ExerciseQuestionAnswerSpace.class);
            } catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    public List<KnowledgeBaseDto> getQuestionTypes() {
        return questionTypes;
    }

    public void setQuestionTypes(int questionTypes) {
        this.questionTypes = new ArrayList<>();
        if (questionTypes > 0 && Constant.QUESTION_TYPES != null){
            for (QuestionSubjectAttribute baseDto : Constant.QUESTION_TYPES){
                if (this.getCourseName().equals(baseDto.getSubject())){
                    for (KnowledgeBaseDto knowledgeBaseDto : baseDto.getList()){
                        if (knowledgeBaseDto.getType() == questionTypes){
                            this.questionTypes.add(knowledgeBaseDto);
                        }
                    }
                }
            }
        }
    }

    public List<KnowledgeBaseDto> getDifficultyLevels() {
        return difficultyLevels;
    }

    public void setDifficultyLevels(int difficultyLevels) {
        if (difficultyLevels > 0 && Constant.DIFFICULTY_LEVELS != null){
            for (KnowledgeBaseDto knowledgeBaseDto : Constant.DIFFICULTY_LEVELS){
                if (difficultyLevels == knowledgeBaseDto.getType()){
                    this.difficultyLevels = new ArrayList<>();
                    this.difficultyLevels.add(knowledgeBaseDto);
                }
            }
        }
    }

    public List<KnowledgeBaseDto> getRealExamTypes() {
        return realExamTypes;
    }

    public void setRealExamTypes(String realExamTypes) {
        if (StringUtil.isNotBlank(realExamTypes) && Constant.REAL_EXAM_TYPE != null){
            this.realExamTypes = new ArrayList<>();
            for (KnowledgeBaseDto knowledgeBaseDto : Constant.REAL_EXAM_TYPE){
                for (String type : realExamTypes.split(",")){
                    if (knowledgeBaseDto.getType() == Integer.parseInt(type)){
                        this.realExamTypes.add(knowledgeBaseDto);
                    }
                }
            }
        }
    }

    public List<KnowledgeBaseDto> getTrainingSkills() {
        return trainingSkills;
    }

    public void setTrainingSkills(String trainingSkills) {
        if (StringUtil.isNotBlank(trainingSkills) && Constant.TRAINING_SKILL != null){
            this.trainingSkills = new ArrayList<>();
            for (QuestionSubjectAttribute baseDto : Constant.TRAINING_SKILL){
                if (this.getCourseName().equals(baseDto.getSubject())){
                    for (KnowledgeBaseDto knowledgeBaseDto : baseDto.getList()){
                        for (String name : trainingSkills.split(",")){
                            if (knowledgeBaseDto.getName().equals(name)){
                                this.trainingSkills.add(knowledgeBaseDto);
                            }
                        }
                    }
                }
            }
        }
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
        this.subject = courseName;
    }

    @JSONField(serialize=false)
    public String getParentId() {
        return parentId;
    }

    public void setParentId(String parentId) {
        this.parentId = parentId;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public int getSemester() {
        return semester;
    }

    public void setSemester(int semester) {
        this.semester = semester;
    }

    public int getVersionType() {
        return versionType;
    }

    public void setVersionType(int versionType) {
        this.versionType = versionType;
    }

    public String getVersionName() {
        return versionName;
    }

    public void setVersionName(String versionName) {
        this.versionName = versionName;
    }

    public int getModuleType() {
        return moduleType;
    }

    public void setModuleType(int moduleType) {
        this.moduleType = moduleType;
    }

    public String getQuestionImage() {
        return questionImage;
    }

    public void setQuestionImage(String questionImage) {
        this.questionImage = questionImage;
    }

    public String getHandleUserId() {
        return handleUserId;
    }

    public void setHandleUserId(String handleUserId) {
        this.handleUserId = handleUserId;
    }

    public String getAuditingUserId() {
        return auditingUserId;
    }

    public void setAuditingUserId(String auditingUserId) {
        this.auditingUserId = auditingUserId;
    }

    public String getAnalysisUserId() {
        return analysisUserId;
    }

    public void setAnalysisUserId(String analysisUserId) {
        this.analysisUserId = analysisUserId;
    }

    public String getAnalysisAuditerId() {
        return analysisAuditerId;
    }

    public void setAnalysisAuditerId(String analysisAuditerId) {
        this.analysisAuditerId = analysisAuditerId;
    }

    public void appendUserNameByIds (List<TbLibManager> users){
        if (CollectionUtils.isNotEmpty(users)){
            for (TbLibManager manager : users){
                if (manager.getUserId().equals(this.getHandleUserId()) && StringUtils.isNotBlank(manager.getName())){
                    this.setDealUser(manager.getName());
                }
                if (manager.getUserId().equals(this.getAuditingUserId()) && StringUtils.isNotBlank(manager.getName())){
                    this.setAuditingUser(manager.getName());
                }
                if (manager.getUserId().equals(this.getAnalysisUserId()) && StringUtils.isNotBlank(manager.getName())){
                    this.setAnalysisDealUser(manager.getName());
                }
                if (manager.getUserId().equals(this.getAnalysisAuditerId()) && StringUtils.isNotBlank(manager.getName())){
                    this.setAnalysisAuditingUser(manager.getName());
                }
                if (manager.getUserId().equals(this.getFromUserId()) && StringUtils.isNotBlank(manager.getName())){
                    this.setFromUser(manager.getName());
                }
                if (manager.getUserId().equals(this.getToUserId()) && StringUtils.isNotBlank(manager.getName())){
                    this.setToUser(manager.getName());
                }
            }
        }
    }


    public void appendKmtSysUserNameByIds (List<KmtSysUser> users){
        if (CollectionUtils.isNotEmpty(users)){
            for (KmtSysUser kmtSysUser : users){
                if (kmtSysUser.getId().equals(this.getHandleUserId())){
                    this.setDealUser(kmtSysUser.getName());
                }
                if (kmtSysUser.getId().equals(this.getAuditingUserId())){
                    this.setAuditingUser(kmtSysUser.getName());
                }
                if (kmtSysUser.getId().equals(this.getAnalysisUserId())){
                    this.setAnalysisDealUser(kmtSysUser.getName());
                }
                if (kmtSysUser.getId().equals(this.getAnalysisAuditerId())){
                    this.setAnalysisAuditingUser(kmtSysUser.getName());
                }
                if (kmtSysUser.getId().equals(this.getFromUserId())){
                    this.setFromUser(kmtSysUser.getName());
                }
                if (kmtSysUser.getId().equals(this.getToUserId())){
                    this.setToUser(kmtSysUser.getName());
                }
            }
        }
    }


    public String getFromUserId() {
        return fromUserId;
    }

    public void setFromUserId(String fromUserId) {
        this.fromUserId = fromUserId;
    }

    public String getToUserId() {
        return toUserId;
    }

    public void setToUserId(String toUserId) {
        if ("1".equals(toUserId)){
            this.setToUser("AI");
        }
        this.toUserId = toUserId;
    }

    public String getFromUser() {
        if (fromUser == null){
            fromUser = "";
        }
        return fromUser;
    }

    public void setFromUser(String fromUser) {
        this.fromUser = fromUser;
    }

    public String getToUser() {
        if (toUser == null){
            toUser = "";
        }
        return toUser;
    }

    public void setToUser(String toUser) {
        this.toUser = toUser;
    }

    public String getAddUser() {
        return addUser;
    }

    public void setAddUser(String addUser) {
        this.addUser = addUser;
    }

    public String getUpdateType() {
        return updateType;
    }

    public void setUpdateType(String updateType) {
        this.updateType = updateType;
    }

    public int getProofreadStatus() {
        return proofreadStatus;
    }

    public void setProofreadStatus(int proofreadStatus) {
        this.proofreadStatus = proofreadStatus;
    }


    public Integer getBeginYear() {
        return beginYear;
    }

    public void setBeginYear(Integer beginYear) {
        this.beginYear = beginYear;
    }

    public Integer getEndYear() {
        return endYear;
    }

    public void setEndYear(Integer endYear) {
        this.endYear = endYear;
    }

    public String getYearName() {
        if (beginYear != null && endYear != null && beginYear.intValue() > 0 && endYear.intValue() > 0){
            return beginYear.toString() + "-" + endYear.toString();
        } else {
            return null;
        }
    }

    public void setYearName(String yearName) {
        this.yearName = yearName;
    }

    public String getQuestionProvince() {
        return questionProvince;
    }

    public void setQuestionProvince(String questionProvince) {
        this.questionProvince = questionProvince;
    }

    public String getQuestionCity() {
        return questionCity;
    }

    public void setQuestionCity(String questionCity) {
        this.questionCity = questionCity;
    }

    public String getQuestionCountry() {
        return questionCountry;
    }

    public void setQuestionCountry(String questionCountry) {
        this.questionCountry = questionCountry;
    }

    public String getQuestionPath() {
        return questionPath;
    }

    public void setQuestionPath(String questionPath) {
        this.questionPath = questionPath;
    }

    public String getErrorReasonImage() {
        return errorReasonImage;
    }

    public void setErrorReasonImage(String errorReasonImage) {
        this.errorReasonImage = errorReasonImage;
    }

    public String getOriginAnswer() {
        return originAnswer;
    }

    public void setOriginAnswer(String originAnswer) {
        this.originAnswer = originAnswer;
    }
}
