<!-- eslint-disable vue/no-v-html -->
<template>
  <div class="fill-preview">
    <div class="stem">
      <span class="question-number" :style="{ fontSize: data.property.stem_question_number_font_size }">
        {{ questionNumberEndIsBracket(data.property.question_number) }}
      </span>
      <span v-html="sanitizeHTML(data.stem)"></span>
    </div>

    <div
      v-if="isEnable(data.property.is_enable_description)"
      class="description rich-text"
      v-html="sanitizeHTML(data.description)"
    ></div>

    <div v-if="isEnable(data.property.is_enable_word_select_fill)" class="word-fill">
      <span v-for="(text, i) in wordFillList" :key="i" class="word-fill-item" @click="selectedDescription(text)">
        {{ text }}
      </span>
    </div>

    <AudioPlay
      v-if="isEnable(data.property.is_enable_listening) && data.file_id_list.length > 0"
      :file-id="data.file_id_list[0]"
      :show-slider="true"
    />

    <div class="fill-wrapper">
      <p v-for="(item, i) in modelEssay" :key="i">
        <template v-for="(li, j) in item">
          <span v-if="li.type === 'text'" :key="j" v-html="sanitizeHTML(li.content)"></span>
          <template v-if="li.type === 'input'">
            <el-input
              v-if="li.type === 'input'"
              :key="j"
              v-model="li.content"
              :disabled="disabled"
              :class="[...computedAnswerClass(li.mark)]"
              :style="[{ width: Math.max(80, li.content.length * 21.3) + 'px' }]"
              @input="handleInput(i, j)"
              @focus="handleInputFocus(i, j)"
              @blur="handleTone(li.content, i, j)"
            />
            <span v-show="computedAnswerText(li.mark).length > 0" :key="`answer-${j}`" class="right-answer">
              {{ computedAnswerText(li.mark) }}
            </span>
          </template>
        </template>
      </p>
    </div>

    <div v-if="!isEnable(data.property.is_enable_word_select_fill) && isShowRightAnswer" class="reference-box">
      <h5 class="reference-title">参考答案</h5>
      <div class="reference-answer rich-text" v-html="sanitizeHTML(data.reference_answer)"></div>
    </div>

    <div v-if="isEnable(data.property.is_enable_analysis) && isShowRightAnswer" class="analysis">
      <span class="analysis-title">解析</span>
      <div class="analysis-content" v-html="sanitizeHTML(data.analysis)"></div>
    </div>
  </div>
</template>

<script>
import PreviewMixin from './components/PreviewMixin';

import { addTone, handleToneValue } from '@/views/exercise_questions/data/common';

export default {
  name: 'ListenFillPreview',
  mixins: [PreviewMixin],
  data() {
    return {
      inputFocus: false,
      modelEssay: [],
      history: [], // modelEssay 历史记录
      focusPostion: {
        i: -1,
        j: -1,
      },
    };
  },
  computed: {
    wordFillList() {
      return this.data.word_select_fill.split(/\s+/).filter((item) => item);
    },
  },
  watch: {
    'data.model_essay': {
      handler(val) {
        if (!val) return;
        this.modelEssay = JSON.parse(JSON.stringify(val));
      },
      deep: true,
      immediate: true,
    },
    modelEssay: {
      handler(val) {
        if (!val) return;
        this.history = JSON.parse(JSON.stringify(val));
        this.answer.answer_list = val
          .map((item) => {
            return item
              .map(({ type, content, mark }) => {
                if (type === 'input') {
                  return {
                    value: content,
                    mark,
                  };
                }
              })
              .filter((item) => item);
          })
          .flat();
      },
      deep: true,
      immediate: true,
    },
    isJudgingRightWrong(val) {
      if (!val) return;
      this.answer.answer_list.forEach(({ mark, value }) => {
        this.modelEssay.forEach((item) => {
          item.forEach((li) => {
            if (li.mark === mark) {
              li.content = value;
            }
          });
        });
      });
    },
  },
  created() {
    document.addEventListener('click', this.handleBlur);
    document.addEventListener('keydown', this.handleBlurTab);
  },
  beforeDestroy() {
    document.removeEventListener('click', this.handleBlur);
    document.removeEventListener('keydown', this.handleBlurTab);
  },
  methods: {
    handleBlur(e) {
      if (e.target.tagName === 'INPUT') return;
      this.inputFocus = false;
    },
    handleBlurTab(e) {
      if (e.keyCode !== 9 || e.key !== 'Tab') return;
      this.inputFocus = false;
    },
    handleInputFocus(i, j) {
      this.inputFocus = true;
      this.focusPostion = {
        i,
        j,
      };
    },
    /**
     * 处理输入框输入
     * @param {number} i
     * @param {number} j
     */
    handleInput(i, j) {
      // 在开启选词填空的情况下，输入框输入内容时，做撤销处理
      if (!this.isEnable(this.data.property.is_enable_word_select_fill)) return;
      this.modelEssay[i][j].content = this.history[i][j].content;
    },
    selectedDescription(text) {
      if (!this.inputFocus) return;
      const { i, j } = this.focusPostion;
      this.modelEssay[i][j].content = text;
      this.inputFocus = false;
    },
    handleTone(value, i, j) {
      if (!/^[a-zA-Z0-9\s]+$/.test(value)) return;
      this.modelEssay[i][j].content = value
        .trim()
        .split(/\s+/)
        .map((item) => {
          return handleToneValue(item);
        })
        .map((item) =>
          item.map(({ number, con }) => (number && con ? addTone(Number(number), con) : number || con || '')),
        )
        .filter((item) => item.length > 0)
        .join(' ');
    },
    /**
     * 计算答题对错选项字体颜色
     * @param {string} mark 选项标识
     */
    computedAnswerClass(mark) {
      if (!this.isJudgingRightWrong && !this.isShowRightAnswer) {
        return '';
      }
      if (!this.isEnable(this.data.property.is_enable_word_select_fill)) return '';
      let selectOption = this.answer.answer_list.find((item) => item.mark === mark); // 是否已选中的选项
      let answerOption = this.data.answer.answer_list.find((item) => item.mark === mark); // 是否正确的选项
      if (!selectOption) return '';
      let selectValue = selectOption.value;
      let answerValue = answerOption.value;
      let type = answerOption.type;
      let classList = [];
      let isRight =
        (type === 'only_one' && selectValue === answerValue) ||
        (type === 'any_one' && answerValue.split('/').includes(selectValue));

      if (this.isJudgingRightWrong) {
        isRight ? classList.push('right') : classList.push('wrong');
      }

      if (this.isShowRightAnswer && !isRight) {
        classList.push('show-right-answer');
      }
      return classList;
    },
    computedAnswerText(mark) {
      if (!this.isShowRightAnswer) return '';
      if (!this.isEnable(this.data.property.is_enable_word_select_fill)) return '';
      let selectOption = this.answer.answer_list.find((item) => item.mark === mark); // 是否已选中的选项
      let answerOption = this.data.answer.answer_list.find((item) => item.mark === mark); // 是否正确的选项
      if (!selectOption) return '';
      let selectValue = selectOption.value;
      let answerValue = answerOption.value;
      let type = answerOption.type;
      let isRight =
        (type === 'only_one' && selectValue === answerValue) ||
        (type === 'any_one' && answerValue.split('/').includes(selectValue));
      if (isRight) return '';
      return `（${answerValue}）`;
    },
  },
};
</script>

<style lang="scss" scoped>
@use '@/styles/mixin.scss' as *;

.fill-preview {
  @include preview;

  .word-fill {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 8px;

    &-item {
      padding: 4px 8px;
      cursor: pointer;
      background-color: #e9edf7;
      border-radius: 4px;

      &:hover,
      &:active {
        color: $light-main-color;
      }
    }
  }

  .fill-wrapper {
    font-size: 16pt;

    .el-input {
      display: inline-flex;
      align-items: center;
      width: 120px;
      margin: 0 2px;

      &.right {
        :deep input.el-input__inner {
          color: $right-color;
        }
      }

      &.wrong {
        :deep input.el-input__inner {
          color: $error-color;
        }
      }

      & + .right-answer {
        position: relative;
        left: -4px;
        display: inline-block;
        height: 32px;
        line-height: 28px;
        vertical-align: bottom;
        border-bottom: 1px solid $font-color;
      }

      :deep input.el-input__inner {
        padding: 0;
        font-size: 16pt;
        color: $font-color;
        text-align: center;
        background-color: #fff;
        border-width: 0;
        border-bottom: 1px solid $font-color;
        border-radius: 0;
      }
    }
  }
}
</style>
