<template>
  <div v-if="store.currentTask" class="mark-board-track-container">
    <div>
      <h1 class="tw-text-3xl tw-text-center" @click="toggleKeyMouse">
        键盘给分
      </h1>
    </div>
    <div>
      <h1 class="tw-text-3xl tw-text-center">
        总分：{{ store.currentMarkResult?.markerScore || 0 }}
      </h1>
    </div>

    <div v-if="store.currentTask && store.currentTask.questionList">
      <template
        v-for="(question, index) in store.currentTask?.questionList"
        :key="index"
      >
        <div
          @click="chooseQuestion(question)"
          class="question tw-rounded tw-p-1 tw-mb-2"
          :class="isCurrentQuestion(question) && 'current-question'"
        >
          <div class="tw-flex tw-justify-between">
            <div>
              <div>
                {{ question.title }} {{ question.mainNumber }}-{{
                  question.subNumber
                }}
              </div>
              <div class="tw-text-center tw-text-3xl">
                {{ question.score }}
              </div>
            </div>
            <div>
              <div class="tw-text-center">
                间隔{{ question.intervalScore }}分
              </div>
              <div class="tw-text-3xl">
                {{ question.minScore }} ~ {{ question.maxScore }}
              </div>
            </div>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<script lang="ts">
import { Question } from "@/types";
import { isNumber } from "lodash";
import { computed, defineComponent, onMounted, onUnmounted, watch } from "vue";
import { store } from "./store";
import { keyMouse } from "./use/keyboardAndMouse";
import { autoChooseFirstQuestion } from "./use/autoChooseFirstQuestion";

export default defineComponent({
  name: "MarkBoardKeyBoard",
  emits: ["submit"],
  setup(props, { emit }) {
    const { toggleKeyMouse } = keyMouse();
    const { chooseQuestion } = autoChooseFirstQuestion();

    const questionScoreSteps = computed(() => {
      const question = store.currentQuestion;
      if (!question) return [];

      const steps = [];
      for (
        let i = 0;
        i <= question.maxScore - (question.score || 0);
        i += question.intervalScore
      ) {
        steps.push(i);
      }
      if (
        (question.maxScore - (question.score || 0)) % question.intervalScore !==
        0
      ) {
        steps.push(question.maxScore - (question.score || 0));
      }

      return steps;
    });

    function isCurrentQuestion(question: Question) {
      return (
        store.currentQuestion?.mainNumber === question.mainNumber &&
        store.currentQuestion?.subNumber === question.subNumber
      );
    }
    // 当题目改变时，重置当前分数
    watch(
      () => store.currentQuestion,
      () => {
        store.currentScore = undefined;
      }
    );

    let keyPressTimestamp = 0;
    let keys: string[] = [];
    function numberKeyListener(event: KeyboardEvent) {
      // console.log(event);
      if (!store.currentQuestion || !store.currentTask) return;

      // 处理Enter跳下一题或submit
      if (event.key === "Enter") {
        if (!isNumber(store.currentQuestion.score)) {
          // 当前题赋分不通过，Enter无效
          return;
        }
        const idx = store.currentTask?.questionList.findIndex(
          (q) =>
            q.mainNumber === store.currentQuestion?.mainNumber &&
            q.subNumber === store.currentQuestion.subNumber
        );
        if (idx + 1 === store.currentTask?.questionList.length) {
          submit();
        } else {
          chooseQuestion(store.currentTask.questionList[idx + 1]);
        }
        keys = [];
        return;
      }
      // TODO: 确认数字按键的间隔
      if (event.timeStamp - keyPressTimestamp > 1.5 * 1000) {
        keys = [];
      }
      keyPressTimestamp = event.timeStamp;
      keys.push(event.key);
      if (isNaN(parseFloat(keys.join("")))) {
        keys = [];
      }
      if (event.key === "Escape") {
        keys = [];
      }
      const score = parseFloat(keys.join(""));
      if (isNumber(score) && questionScoreSteps.value.includes(score)) {
        store.currentQuestion.score = score;
      }
    }
    onMounted(() => {
      document.addEventListener("keydown", numberKeyListener);
    });
    onUnmounted(() => {
      document.removeEventListener("keydown", numberKeyListener);
    });

    function submit() {
      const errors: any = [];
      store.currentTask?.questionList.forEach((question, index) => {
        if (!isNumber(question.score)) {
          errors.push({ question, index, error: "没有赋分不能提交" });
        }
      });
      if (errors.length === 0) {
        emit("submit");
      } else {
        console.log(errors);
      }
    }

    return {
      store,
      toggleKeyMouse,
      isCurrentQuestion,
      chooseQuestion,
      questionScoreSteps,
      submit,
    };
  },
});
</script>

<style scoped>
.mark-board-track-container {
  max-width: 250px;
  min-width: 250px;
  border-left: 1px solid grey;
  padding-left: 6px;
  padding-right: 6px;
}
.question {
  min-width: 80px;
  border: 1px solid grey;
}
.current-question {
  border: 1px solid yellowgreen;
  background-color: lightblue;
}
.single-score {
  width: 30px;
  height: 30px;
  display: grid;
  place-content: center;

  border: 1px solid black;
  border-radius: 5px;
}
.current-score {
  border: 1px solid yellowgreen;
  background-color: lightblue;
}
</style>
