<template>
  <transition-group name="track-score" tag="div">
    <template v-for="track in trackList">
      <div
        v-if="store.shouldShowTrack && (doubleTrack || !track.isByMultMark)"
        :key="`key-${track.mainNumber}-${track.subNumber}-${track.offsetY}-${track.offsetX}`"
        class="score-container no-event"
        :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
      v-if="tag.tagType === 'TEXT'"
      class="score-container"
      :style="computeTopAndLeft(tag)"
      @keypress.stop
      @mousemove.stop
      @mousedown.stop
      @mouseup.stop
    >
      <a-textarea
        v-model:value="tag.tagName"
        class="tag-textarea tw-m-auto"
        :autosize="{ minRows: 2, maxRows: 6 }"
        :maxlength="32"
      />
    </div>
    <div
      v-else
      :class="['score-container', 'no-event']"
      :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, computed } from "vue";
import { store } from "@/store/store";
import { message } from "ant-design-vue";
// import { CheckOutlined } from "@ant-design/icons-vue";
import { useRoute } from "vue-router";
const route = useRoute();

const doubleTrack = computed(() => {
  return !!store.setting?.doubleTrack;
});
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 {
    color: route.path === "/arbitrate" ? "green" : track.color || "red",
    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 hasMember = (track: any) => {
  return (
    // (store.getMarkStatus === "正评" || store.getMarkStatus === "试评") &&
    store.focusTracks.find((item: any) => {
      return (
        item.mainNumber == track.mainNumber && item.subNumber == track.subNumber
      );
    })
  );
};
const focusedTrack = (track: Track) => {
  return store.focusTracks.includes(track) || hasMember(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) {
      let allHeaderTracks = store.currentTask.questionList
        .map((item: any) => item.headerTrack || [])
        .flat();
      console.log("allHeaderTracks", allHeaderTracks);
      let find = allHeaderTracks.find(
        (item: any) =>
          item.mainNumber == topTrack.mainNumber &&
          item.subNumber == topTrack.subNumber
      );
      document
        .querySelector(
          `#a-${topTrack.mainNumber}-${topTrack.subNumber}-${
            find?.offsetY || topTrack.offsetY
          }-${find?.offsetX || topTrack.offsetX}`
        )
        ?.scrollIntoView({ behavior: "smooth" });
    }
  },
  {
    deep: true,
  }
);
</script>

<style scoped>
.score-container {
  position: absolute;
  z-index: 9;
  display: flex;
  align-items: center;
  justify-content: center;
  place-content: center;
  /* color: red; */

  /* to center score */
  width: 200px;
  height: 200px;
  margin-top: -100px;
  margin-left: -100px;
}
.score-container.no-event {
  /* to click through div */
  pointer-events: none;
}
.score-animation {
  animation: 0.5s ease-in-out 0s infinite alternate change_size;
}
.score-container .tag-textarea {
  background-color: transparent;
  color: red;
  font-size: inherit;
  resize: none;
  line-height: 1.1;
  border-color: transparent;
  outline: none;
}
.score-container .tag-textarea:hover,
.score-container .tag-textarea:focus {
  background-color: rgba(255, 255, 255, 0.8);
  border-color: #f53f3f;
}

@keyframes change_size {
  from {
    transform: scale(1, 1);
  }
  to {
    transform: scale(2, 2);
  }
}
.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>
