<template>
  <transition-group name="track-score" tag="div">
    <template v-for="track in trackList">
      <div
        v-if="store.shouldShowTrack"
        :key="`key-${track.mainNumber}-${track.subNumber}-${track.offsetY}-${track.offsetX}`"
        class="score-container"
        :class="[focusedTrack(track) && 'score-animation']"
        :style="computeTopAndLeft(track)"
      >
        <span
          :id="`a-${track.mainNumber}-${track.subNumber}-${track.offsetY}-${track.offsetX}`"
          class="tw-m-auto"
        >
          {{ track.unanswered ? "空" : track.score }}
        </span>
      </div>
    </template>
  </transition-group>
  <template v-for="(tag, index) in specialTagList" :key="index">
    <div class="score-container" :style="computeTopAndLeft(tag)">
      <span class="tw-m-auto">
        {{ tag.tagName }}
      </span>
    </div>
  </template>
</template>

<script setup lang="ts">
import type { SpecialTag, Track } from "@/types";
import { toRefs, watch, nextTick } from "vue";
import { store } from "@/store/store";
import { message } from "ant-design-vue";

const props = defineProps<{
  trackList: Array<Track>;
  specialTagList: Array<SpecialTag>;
  sliceImageWidth: number;
  sliceImageHeight: number;
  dx: number;
  dy: number;
}>();
const { trackList } = toRefs(props);

const computeTopAndLeft = (track: Track | SpecialTag) => {
  const topInsideSlice = track.offsetY - props.dy;
  const leftInsideSlice = track.offsetX - props.dx;
  const topInsideSliceRatio = topInsideSlice / props.sliceImageHeight;
  const leftInsideSliceRatio = leftInsideSlice / props.sliceImageWidth;
  if (
    topInsideSliceRatio < 0 ||
    topInsideSliceRatio > 1 ||
    leftInsideSliceRatio < 0 ||
    leftInsideSliceRatio > 1
  ) {
    /** 解决message提示死循环的问题 */
    void nextTick(() => {
      void message.error("轨迹坐标有误，可能是图片被修改过，请联系管理员！");
    });
  }

  return {
    top: topInsideSliceRatio * 100 + "%",
    left: leftInsideSliceRatio * 100 + "%",
    "font-size":
      (store.setting.uiSetting["score.fontSize.scale"] || 1) *
        store.setting.uiSetting["answer.paper.scale"] *
        2.2 +
      "em",
  };
};

const focusedTrack = (track: Track) => {
  return store.focusTracks.includes(track);
};

watch(
  () => store.focusTracks,
  () => {
    if (store.focusTracks.length === 0) return;
    const minImageIndex = Math.min(
      ...store.focusTracks.map((t) => t.offsetIndex)
    );
    const minImageOffsetY = Math.min(
      ...store.focusTracks
        .filter((t) => t.offsetIndex === minImageIndex)
        .map((t) => t.offsetY)
    );
    const topTrack = store.focusTracks.find(
      (t) => t.offsetIndex === minImageIndex && t.offsetY === minImageOffsetY
    );
    if (topTrack) {
      document
        .querySelector(
          `#a-${topTrack.mainNumber}-${topTrack.subNumber}-${topTrack.offsetY}-${topTrack.offsetX}`
        )
        ?.scrollIntoView({ behavior: "smooth" });
    }
  },
  {
    deep: true
  }
);
</script>

<style scoped>
.score-container {
  position: absolute;
  display: flex;
  place-content: center;
  color: red;

  /* to center score */
  width: 200px;
  height: 200px;
  margin-top: -100px;
  margin-left: -100px;

  /* to click through div */
  pointer-events: none;
}
.score-animation {
  animation: 2s ease-in-out 0s infinite alternate change_size;
}

@keyframes change_size {
  from {
    font-size: 2em;
    margin-top: -100px;
    margin-left: -100px;
  }
  to {
    font-size: 4em;
    margin-top: -80px;
    margin-left: -80px;
  }
}
.track-score-enter-active {
  transition: opacity 0.3s ease;
}
.track-score-leave-active {
  transition: opacity 0.6s ease;
}

.track-score-enter-from,
.track-score-leave-to {
  opacity: 0;
}
</style>
