<template>
  <div class="platine pointer-events-none" :class="{ 'loading': platineStore.isLoadingTrack, 'mounted': isMounted }"
    ref="platine">
    <div class="disc pointer-events-auto fixed" ref="discRef" :style="'background-image: url(/card-dock.svg)'"
      id="disc">
      <div
        class="bobine bg-slate-900 bg-opacity-50 absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2 rounded-full"
        :style="{ height: platineStore.progressPercentage + '%', width: platineStore.progressPercentage + '%' }"></div>
      <img class="cover" :src="platineStore.currentTrack?.coverId" :alt="platineStore.currentTrack?.title">

      <div class="disc-label rounded-full bg-cover bg-center">
        <img src="/favicon.svg" class="size-1/3">
        <div v-if="platineStore.isLoadingTrack" class="loading-indicator">
          <div class="spinner"></div>
        </div>
      </div>
      <div v-if="!platineStore.isLoadingTrack" class="absolute top-1/2 right-8 size-1/12 rounded-full bg-esyellow">
      </div>
    </div>
    <div class="w-full h-1/5 text-base">
      {{ platineStore.currentTrack?.title }}
      <br>
      {{ platineStore.currentTrack?.artist?.name }}
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, watch } from 'vue'
import { usePlatineStore } from '~/store/platine'
import type { Track } from '~~/types/types'

const props = defineProps<{ track?: Track }>()
const platineStore = usePlatineStore()
const discRef = ref<HTMLElement>()
const platine = ref<HTMLElement>()
const isMounted = ref(false)

// Initialisation du lecteur
onMounted(() => {
  isMounted.value = true
  if (discRef.value) {
    platineStore.initPlatine(discRef.value)
  }
})

// Nettoyage
onUnmounted(() => {
  isMounted.value = false
  platineStore.cleanup()
})

// Surveillance des changements de piste
watch(() => props.track, (newTrack) => {
  if (newTrack) {
    platineStore.loadTrack(newTrack)
  }
})

</script>

<style lang="scss">
.platine {
  overflow: hidden;
  position: relative;
  width: 100%;
  height: 100%;
  padding: 20px;

  .card {
    position: absolute !important;
    z-index: 99;
    top: -20%;
    left: 50%;
    bottom: 0;
    transform: translate(-50%, 50%);
  }
}

.disc {
  position: relative;
  background-color: white;
  aspect-ratio: 1;
  width: 100%;
  overflow: hidden;
  border-radius: 50%;
  cursor: grab;
  background-position: center;
  background-size: cover;
  box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);

  .dragoOver & {
    background-color: #4CAF50;
  }
}

.disc.is-scratching {
  cursor: grabbing;
}

.disc-label {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
  background-size: cover;
  width: 45%;
  aspect-ratio: 1/1;
  // background: no-repeat url(/favicon.svg) center center;
  background-size: 30%;
  border-radius: 50%;
  cursor: pointer !important;
}

.disc-middle {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  background: rgb(26, 26, 26);
  border-radius: 50%;
}

.button {
  border-radius: 0;
  border: none;
  background: rgb(69, 69, 69);
  font-size: 0.75rem;
  padding: 0.4rem;
  color: #fff;
  line-height: 1.3;
  cursor: pointer;
  will-change: box-shadow;
  transition:
    box-shadow 0.2s ease-out,
    transform 0.05s ease-in;
}

.power.is-active {
  transform: translate(1px, 2px);
  color: red;
}

.button[disabled] {
  opacity: 0.5;
}

.loading-indicator {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 50%;
  z-index: 10;
}

.cover {
  position: absolute;
  border-radius: 100%;
  object-fit: cover;
  width: 100%;
  height: 100%;
  transition: opacity 3s ease;

  .loading & {
    opacity: 0;
    transition: opacity 0.3s ease;
  }
}

.spinner {
  width: 40px;
  height: 40px;
  border: 4px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.bobine {
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-size: cover;
    opacity: 0.7;
  }
}
</style>
