<template>
  <div
    v-if="store.currentTask"
    :style="{ display: store.MarkBoardTrackCollapse ? 'none' : 'block' }"
    style="
      max-width: 250px;
      min-width: 250px;
      border: 1px solid grey;
      padding-left: 6px;
      padding-right: 6px;
    "
  >
    <div>
      <h1 class="tw-text-3xl tw-text-center">
        总分：{{ store.currentMarkResult?.markerScore || 0 }}
      </h1>
    </div>
    <div>
      <div class="tw-text-2xl tw-text-center" @click="submit">提交</div>
    </div>

    <div
      v-if="store.currentTask && store.currentTask.questionList"
      class="tw-flex tw-gap-1 tw-flex-wrap tw-justify-between"
    >
      <template
        v-for="(question, index) in store.currentTask?.questionList"
        :key="index"
      >
        <div
          @click="chooseQuestion(question)"
          class="question tw-rounded tw-p-1"
          style="min-width: 100px"
          :class="isCurrentQuestion(question) && 'current-question'"
        >
          <div>
            {{ question.title }} {{ question.mainNumber }}-{{
              question.subNumber
            }}
          </div>
          <div class="tw-text-center">
            {{ question.score || 0 }}
          </div>
        </div>
      </template>
    </div>

    <div class="tw-flex tw-gap-1 tw-flex-wrap tw-mt-5">
      <div
        v-for="(s, i) in questionScoreSteps"
        :key="i"
        @click="chooseScore(s)"
        class="single-score"
        :class="isCurrentScore(s) && 'current-score'"
      >
        {{ s }}
      </div>
    </div>
  </div>
</template>

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

export default defineComponent({
  name: "MarkBoardTrack",
  emits: ["submit"],
  setup(props, { emit }) {
    const markResult = findCurrentTaskMarkResult();

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

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

      return steps;
    });

    function isCurrentQuestion(question: Question) {
      return (
        store.currentQuestion?.mainNumber === question.mainNumber &&
        store.currentQuestion?.subNumber === question.subNumber
      );
    }
    watch(
      () => store.currentTask,
      () => {
        store.currentQuestion = undefined;
        store.currentScore = undefined;
      }
    );
    watch(
      () => store.currentQuestion,
      () => {
        store.currentScore = undefined;
      }
    );
    function chooseQuestion(question: Question) {
      store.currentQuestion = question;
    }
    function isCurrentScore(score: number) {
      return store.currentScore === score;
    }
    function chooseScore(score: number) {
      store.currentScore = score;
    }

    let keyPressTimestamp = 0;
    let keys: string[] = [];
    function numberKeyListener(event: KeyboardEvent) {
      // console.log(event);
      if (!store.currentQuestion) return;
      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)) {
        chooseScore(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,
      markResult,
      isCurrentQuestion,
      chooseQuestion,
      isCurrentScore,
      chooseScore,
      questionScoreSteps,
      submit,
    };
  },
});
</script>

<style scoped>
.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>
