import consts from '@/common/mx-const'
import config from '@/common/mx-config'
import { scoreExamineeQuestion, teacherScoreExamineeQuestions } from '@/api/webApi/studentEvaluating'
import { mapGetters } from 'vuex'

export default {
  computed: {
    ...mapGetters(['isFrontTeacher'])
  },
  methods: {
    // local data
    eventProcessPaper(paper, questionContainer) {
      // 预处理试卷，目的在于对原始数据做检查或微调，加强代码容错性
      const autoScore = this.eventNeedAutoScoreObjectives(paper)
      const autoScoreQuestions = []
      paper?.questions?.forEach((q, idx) => {
        // question local data
        this.eventEnsureLocalAnswerData(q)

        // cache question options
        questionContainer[idx] = {
          question: q,
          allowAnswer: paper.allowAnswer,
          allowScore: paper.allowScore,
          examineeType: this.examineeType,
          paperOptions: this.options
        }

        // 客观题自动打分！
        if (q._isObjective && autoScore && q.score == null) {
          q.score = this.eventIsAnswerCorrect(q) ? q['scoreTotal'] : 0
          autoScoreQuestions.push({
            score: q.score,
            questionId: q.questionId,
            examineeType: this.examineeType
          })
        }
      })

      paper._userSumScore = this.eventUserSumScore(paper)
      paper._paperSumScore = this.eventPaperSumScore(paper)
      return autoScoreQuestions
    },
    eventEnsureLocalAnswerData(question) {
      question._boardMode = question.attachments?.length
        ? consts.enum.paper.boardMode.image
        : consts.enum.paper.boardMode.text
      // local full cache _parseMode
      if (!question._parseMode) question._parseMode = consts.enum.paper.parseMode.parse
      question._duration = question.duration || 0
      question._answer = question.answer || ''
      question._attachments = question.attachments || []
      this.$set(question, 'score', question.score)
      this.$set(question, '_score', question.score) // to satisfy null
      question._isObjective = this.eventIsObjective(question)
      question._isFinished = this.eventIsQuestionFinished(question)
    },
    eventNeedAutoScoreObjectives(paper) {
      return paper.allowScore && paper.state == 5
    },
    eventIsObjective(question) {
      const objectiveIds = config.objectiveQuestionTypeIds
      return objectiveIds.some(t => t == question.typeId) // 没用include，不确认number/string类型
    },
    eventIsAnswerCorrect(question) {
      return question.answers?.length && question.answer == question.answers[0].toString()
    },
    eventIsQuestionFinished(question) {
      return !!question.answer || question.attachments?.length
    },
    eventIsQuestionLocalFinished(question) {
      return (question._boardMode == consts.enum.paper.boardMode.text && question._answer) ||
        (question._boardMode == consts.enum.paper.boardMode.image && question._attachments.length)
    },
    eventUnfinishedQuestions(paper) {
      return paper.questions.filter(q => !this.eventIsQuestionFinished(q))
    },
    eventUnScoredQuestions(paper) {
      return paper.questions.filter(q => q.score == null)
    },
    eventUserSumScore(paper) {
      return paper.questions.sum(q => q.score || 0)
    },
    eventPaperSumScore(paper) {
      return paper.questions.sum(q => q['scoreTotal'] || 0)
    },
    // event
    eventBuildCommitScoreData(questionOptions) {
      return {
        questionId: questionOptions.question.questionId,
        examineeType: questionOptions.examineeType,
        score: questionOptions.question._score
      }
    },
    eventBuildCommitAnswerData(question) {
      const commit = {
        questionId: question.questionId,
        duration: question._duration,
        answer: question._answer,
        attachments: question._attachments
      }
      if (question._boardMode == consts.enum.paper.boardMode.image) commit.answer = ''
      if (question._boardMode == consts.enum.paper.boardMode.text) commit.attachments = []
      return commit
    },
    eventHandleScoreSubmitted(commit, question, paper) {
      question.score = commit.score
      if (paper) paper._userSumScore = this.eventUserSumScore(paper)
    },
    eventHandleAnswerSubmitted(commit, question) {
      question.duration = commit.duration
      question.answer = commit.answer
      question.attachments = commit.attachments
      this.eventEnsureLocalAnswerData(question)
    },
    eventScoreQuestion(options) {
      // 单题打分
      const paperOptions = options.paperOptions
      const question = options.question
      const paper = paperOptions.paper
      const commit = this.eventBuildCommitScoreData(options)
      const singleCommit = {
        examineeId: paperOptions.extData.examineeId || paper.examineeId,
        examineeType: options.examineeType,
        questions: [commit]
      }
      const scoreApi = this.isFrontTeacher ? teacherScoreExamineeQuestions : scoreExamineeQuestion
      scoreApi(singleCommit)
        .then(res => {
          // assign after succeed
          this.eventHandleScoreSubmitted(commit, question, paper)
          // go next
          if (paper && paperOptions.currentTab < paper.questions.length - 1) {
            paperOptions.currentTab += 1
          }
        })
    }
  }
}
