<template>
  <div class="dialog__inner">
    <header class="flex">
      <div class="options-selector">
        <button
          v-for="(track, index) in tracks"
          class="btn btn--image"
          :aria-pressed="activeTrack === track ? true : false"
          :aria-controls="track.slug"
          :style="`--btn-image: url(${track.files[7].url});`"
          @click="
            activeTrack = track;
            currentX = 0;
            currentY = 0;
          "
        >
          <span>{{ track.title }}</span>
        </button>
      </div>
      <button class="btn | ml-auto" disabled>
        <span>Comparer les pistes</span>
      </button>
    </header>

    <div class="track">
      <figure>
        <div @mousedown="enableDragToRotate" class="drag-zone"></div>
        <img :src="currentFile.url" alt="" width="500" height="500" />
      </figure>
    </div>
  </div>
</template>
<script setup>
import throttle from "lodash/throttle";
import { ref, computed, watch } from "vue";
import { storeToRefs } from "pinia";
import { usePageStore } from "../../../stores/page";
import { useVirtualSampleStore } from "../../../stores/virtualSample";

const { page } = storeToRefs(usePageStore());
const virtualSampleStore = useVirtualSampleStore();

const tracks = computed(
  () => page.value.steps[page.value.steps.length - 1].files.dynamic
);

const activeTrack = ref(tracks.value[0]);

const yMax = computed(() => {
  return parseInt(
    activeTrack.value.files[activeTrack.value.files.length - 1].name.charAt(0)
  );
});
const xMax = computed(() => {
  return parseInt(
    activeTrack.value.files[activeTrack.value.files.length - 1].name
      .split("_")[1]
      .split(".")[0]
  );
});

const currentX = ref(0);
const currentY = ref(0);
const currentFileIndex = computed(() => currentY.value + "_" + currentX.value);
const currentFile = computed(() =>
  activeTrack.value.files.find((file) =>
    file.name.includes(currentFileIndex.value)
  )
);
watch(currentFile, () => (virtualSampleStore.currentFile = currentFile.value), {
  immediate: true,
});

function rotateX(direction) {
  if (direction == "left") {
    currentX.value = currentX.value === 0 ? xMax.value : currentX.value - 1;
  }
  if (direction == "right") {
    currentX.value = currentX.value === xMax.value ? 0 : currentX.value + 1;
  }
}

const isDragToRotateEnabled = ref(false);
window.addEventListener("mouseup", disableDragToRotate);
function enableDragToRotate() {
  isDragToRotateEnabled.value = true;
}
function disableDragToRotate() {
  isDragToRotateEnabled.value = false;
}

let previousMouseXPos;
let previousMouseYPos;
const DRAG_STEP = 20;

function dragToRotate(event) {
  handleHorizontalRotation(event);
  handleVerticalRotation(event);
}

function handleHorizontalRotation(event) {
  if (previousMouseXPos === undefined) {
    previousMouseXPos = event.clientX;
    return;
  }

  if (event.clientX > previousMouseXPos + DRAG_STEP) {
    rotateX("left");
    previousMouseXPos = event.clientX;
  } else if (event.clientX < previousMouseXPos - DRAG_STEP) {
    rotateX("right");
    previousMouseXPos = event.clientX;
  }
}

function handleVerticalRotation(event) {
  if (previousMouseYPos === undefined) {
    previousMouseYPos = event.clientY;
    return;
  }

  if (event.clientY > previousMouseYPos + DRAG_STEP) {
    if (currentY.value < yMax.value) {
      currentY.value++;
      previousMouseYPos = event.clientY;
    }
  } else if (event.clientY < previousMouseYPos - DRAG_STEP) {
    if (currentY.value > 0) {
      currentY.value--;
      previousMouseYPos = event.clientY;
    }
  }
}

const throttledDragToRotate = throttle(dragToRotate, 50);

watch(isDragToRotateEnabled, (newValue) => {
  if (newValue) {
    window.addEventListener("mousemove", throttledDragToRotate);
  } else {
    window.removeEventListener("mousemove", throttledDragToRotate);
    previousMouseXPos = undefined;
    previousMouseYPos = undefined;
  }
});
</script>

<style>
.track figure {
  position: relative;
}

.track .drag-zone {
  position: absolute;
  inset: 0;
  z-index: 2;
}
</style>
