<template>
  <!-- Spacer to prevent content from being hidden behind the player -->
  <div v-if="audioPlayer.currentName.value" class="h-14"></div>

  <Teleport to="body">
    <Transition name="slide-up">
      <div
        v-if="audioPlayer.currentName.value"
        class="fixed bottom-0 left-0 right-0 z-50 audio-player-bar"
      >
        <!-- Progress bar (clickable) -->
        <div
          class="h-1 bg-white/10 cursor-pointer"
          @click="onProgressClick"
        >
          <div
            class="h-full bg-orange-500 transition-all duration-200"
            :style="{ width: audioPlayer.progress.value + '%' }"
          />
        </div>

        <div class="flex items-center gap-3 px-4 py-2.5">
          <!-- Play/Pause -->
          <button
            class="flex-shrink-0 w-9 h-9 rounded-full bg-white/10 hover:bg-white/20 flex items-center justify-center transition-colors"
            @click="togglePlay"
          >
            <svg v-if="!audioPlayer.playing.value" class="w-5 h-5 text-white ml-0.5" fill="currentColor" viewBox="0 0 24 24">
              <path d="M8 5v14l11-7L8 5z" />
            </svg>
            <svg v-else class="w-5 h-5 text-white" fill="currentColor" viewBox="0 0 24 24">
              <path d="M6 4h4v16H6V4zm8 0h4v16h-4V4z" />
            </svg>
          </button>

          <!-- Track info -->
          <div class="flex-1 min-w-0">
            <p v-if="audioPlayer.error.value" class="text-sm text-red-400 truncate">{{ audioPlayer.error.value }}</p>
            <p v-else class="text-sm font-medium text-white/90 truncate">{{ audioPlayer.currentName.value }}</p>
            <p class="text-xs text-white/40">{{ formatTime(audioPlayer.currentTime.value) }} / {{ formatTime(audioPlayer.duration.value) }}</p>
          </div>

          <!-- Close -->
          <button
            class="flex-shrink-0 w-8 h-8 rounded-full hover:bg-white/10 flex items-center justify-center transition-colors"
            @click="audioPlayer.stop()"
          >
            <svg class="w-4 h-4 text-white/60" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
            </svg>
          </button>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup lang="ts">
import { useAudioPlayer } from '@/composables/useAudioPlayer'

const audioPlayer = useAudioPlayer()

function togglePlay() {
  if (audioPlayer.playing.value) {
    audioPlayer.pause()
  } else if (audioPlayer.currentSrc.value) {
    audioPlayer.play(audioPlayer.currentSrc.value, audioPlayer.currentName.value)
  }
}

function onProgressClick(e: MouseEvent) {
  const el = e.currentTarget as HTMLElement
  const rect = el.getBoundingClientRect()
  const ratio = (e.clientX - rect.left) / rect.width
  const time = ratio * audioPlayer.duration.value
  audioPlayer.seek(time)
}

function formatTime(seconds: number): string {
  if (!seconds || !isFinite(seconds)) return '0:00'
  const m = Math.floor(seconds / 60)
  const s = Math.floor(seconds % 60)
  return `${m}:${s.toString().padStart(2, '0')}`
}
</script>

<style scoped>
.audio-player-bar {
  background: rgba(15, 15, 15, 0.55);
  backdrop-filter: blur(24px) saturate(1.4);
  -webkit-backdrop-filter: blur(24px) saturate(1.4);
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 -4px 30px rgba(0, 0, 0, 0.3);
}

.slide-up-enter-active,
.slide-up-leave-active {
  transition: transform 0.3s ease, opacity 0.3s ease;
}

.slide-up-enter-from,
.slide-up-leave-to {
  transform: translateY(100%);
  opacity: 0;
}
</style>
