<template>
  <div v-if="currentTrack" class="music-controls glass" :class="{ collapsed: isCollapsed }">
    <!-- Collapsed State -->
    <div v-if="isCollapsed" class="collapsed-content">
      <div class="collapsed-info">
        <div class="collapsed-track-name">{{ currentTrack.name }}</div>
        <div class="collapsed-meta">
          <span class="status-indicator" :class="{ playing: isPlaying }"></span>
          <span class="status-text">{{ isPlaying ? 'Playing' : 'Paused' }}</span>
          <span class="time-separator">•</span>
          <span class="current-time">{{ formatTime(currentTime) }}</span>
        </div>
      </div>
      <IconButton
        :icon="ChevronUp"
        title="Expand controls"
        size="small"
        @click="toggleCollapse"
        class="expand-btn"
      />
    </div>
    
    <!-- Expanded State -->
    <template v-else>
      <!-- Track Info -->
      <div class="track-info">
        <div class="track-name">{{ currentTrack.name }}</div>
        <div class="track-status">
          <span class="status-indicator" :class="{ playing: isPlaying }"></span>
          <span class="status-text">{{ isPlaying ? 'Playing' : 'Paused' }}</span>
        </div>
      </div>
      
      <div class="controls-layout">
        <!-- Navigation Controls -->
        <div class="nav-controls">
          <IconButton
            :icon="SkipBack"
            title="Previous"
            size="small"
            @click="$emit('previous')"
            class="nav-btn"
          />
          
          <IconButton
            :icon="isPlaying ? Pause : Play"
            :title="isPlaying ? 'Pause' : 'Play'"
            size="large"
            @click="$emit('toggle-play')"
            class="play-btn"
            :class="{ playing: isPlaying }"
          />
          
          <IconButton
            :icon="SkipForward"
            title="Next"
            size="small"
            @click="$emit('next')"
            class="nav-btn"
          />
        </div>
      
      <!-- Progress Section -->
      <div class="progress-section">
        <div 
          class="progress-bar" 
          @click="handleSeek"
          ref="progressBarRef"
          :title="`Seek to ${Math.floor(progressPercent)}%`"
        >
          <div class="progress-track">
            <div 
              class="progress-fill" 
              :style="{ width: progressPercent + '%' }"
            ></div>
            <div 
              class="progress-thumb" 
              :style="{ left: progressPercent + '%' }"
              :class="{ visible: isDragging || isHovered }"
            ></div>
          </div>
        </div>
        
        <div class="time-display">
          <span class="current-time">{{ formatTime(currentTime) }}</span>
          <span class="separator">/</span>
          <span class="total-time">{{ formatTime(duration) }}</span>
        </div>
      </div>
      
      <!-- Volume Control -->
      <div class="volume-section">
        <div class="volume-control">
          <IconButton
            :icon="volumeIcon"
            title="Volume"
            size="small"
            @click="toggleMute"
            class="volume-btn"
          />
          
          <div 
            class="volume-slider" 
            @click="handleVolumeChange"
            ref="volumeSliderRef"
            :title="`Volume: ${Math.round(volume * 100)}%`"
          >
            <div class="volume-track">
              <div 
                class="volume-fill" 
                :style="{ width: (volume * 100) + '%' }"
              ></div>
              <div 
                class="volume-thumb"
                :style="{ left: (volume * 100) + '%' }"
                :class="{ visible: isVolumeHovered }"
              ></div>
            </div>
          </div>
        </div>
      </div>
      </div>
      
      <!-- Collapse Button (positioned absolute top-right) -->
      <IconButton
        :icon="ChevronDown"
        title="Collapse controls"
        size="small"
        @click="toggleCollapse"
        class="collapse-btn-compact"
      />
    </template>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { ChevronUp, ChevronDown, SkipBack, SkipForward, Play, Pause, Volume2, Volume1, VolumeX } from 'lucide-vue-next'
import IconButton from './IconButton.client.vue'

const props = defineProps({
  currentTrack: {
    type: Object,
    default: null
  },
  isPlaying: {
    type: Boolean,
    default: false
  },
  currentTime: {
    type: Number,
    default: 0
  },
  duration: {
    type: Number,
    default: 0
  },
  volume: {
    type: Number,
    default: 0.7
  }
})

const emit = defineEmits(['previous', 'next', 'toggle-play', 'seek', 'volume-change'])

const progressBarRef = ref(null)
const volumeSliderRef = ref(null)
const isDragging = ref(false)
const isHovered = ref(false)
const isVolumeHovered = ref(false)
const previousVolume = ref(0.7)
const isCollapsed = ref(false)

const progressPercent = computed(() => {
  if (props.duration === 0) return 0
  return (props.currentTime / props.duration) * 100
})

const volumeIcon = computed(() => {
  if (props.volume === 0) return VolumeX
  if (props.volume < 0.7) return Volume1
  return Volume2
})

const formatTime = (seconds) => {
  if (!seconds || isNaN(seconds)) return '0:00'
  const mins = Math.floor(seconds / 60)
  const secs = Math.floor(seconds % 60)
  return `${mins}:${secs.toString().padStart(2, '0')}`
}

const handleSeek = (event) => {
  if (!progressBarRef.value) return
  
  const rect = progressBarRef.value.getBoundingClientRect()
  const percent = (event.clientX - rect.left) / rect.width
  const newTime = percent * props.duration
  
  emit('seek', newTime)
}

const handleVolumeChange = (event) => {
  if (!volumeSliderRef.value) return
  
  const rect = volumeSliderRef.value.getBoundingClientRect()
  const percent = Math.max(0, Math.min(1, (event.clientX - rect.left) / rect.width))
  
  emit('volume-change', percent)
}

const toggleMute = () => {
  if (props.volume > 0) {
    previousVolume.value = props.volume
    emit('volume-change', 0)
  } else {
    emit('volume-change', previousVolume.value)
  }
}

const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value
}

// Event listeners for hover states
onMounted(() => {
  if (progressBarRef.value) {
    progressBarRef.value.addEventListener('mouseenter', () => { isHovered.value = true })
    progressBarRef.value.addEventListener('mouseleave', () => { isHovered.value = false })
  }
  
  if (volumeSliderRef.value) {
    volumeSliderRef.value.addEventListener('mouseenter', () => { isVolumeHovered.value = true })
    volumeSliderRef.value.addEventListener('mouseleave', () => { isVolumeHovered.value = false })
  }
})
</script>

<style scoped>
.music-controls {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  backdrop-filter: blur(40px);
  -webkit-backdrop-filter: blur(40px);
  border-radius: 20px;
  padding: 20px;
  box-shadow: var(--shadow-glass);
  z-index: 1000;
  min-width: 600px;
  max-width: 800px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.music-controls.collapsed {
  min-width: 350px;
  max-width: 450px;
  padding: 12px 16px;
  border-radius: 16px;
  bottom: -10px;
}

/* Collapsed State Styles */
.collapsed-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.collapsed-info {
  flex: 1;
  min-width: 0;
}

.collapsed-track-name {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--accent-primary);
  background: linear-gradient(45deg, var(--accent-primary), var(--accent-secondary));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  animation: glow 3s ease-in-out infinite alternate;
}

.collapsed-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.time-separator {
  opacity: 0.5;
}

.expand-btn {
  flex-shrink: 0;
}

.music-controls {
  position: relative;
}

.collapse-btn-compact {
  position: absolute;
  top: 8px;
  right: 8px;
  opacity: 0.6;
  transition: all 0.3s ease;
  width: 24px !important;
  height: 24px !important;
  z-index: 10;
}

.collapse-btn-compact:hover {
  opacity: 1;
  transform: scale(1.1);
}

.track-info {
  text-align: center;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.track-name {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--accent-primary);
  background: linear-gradient(45deg, var(--accent-primary), var(--accent-secondary));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  animation: glow 3s ease-in-out infinite alternate;
}

.track-status {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

.status-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--text-secondary);
  transition: all 0.3s ease;
}

.status-indicator.playing {
  background: var(--accent-primary);
  animation: pulse-dot 1.5s ease-in-out infinite;
}

.status-text {
  font-weight: 500;
}

.controls-layout {
  display: flex;
  align-items: center;
  gap: 24px;
}

.nav-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.play-btn {
  position: relative;
  overflow: hidden;
}

.play-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(45deg, var(--accent-primary), var(--accent-secondary));
  opacity: 0;
  transition: opacity 0.3s ease;
  border-radius: 50%;
}

.play-btn.playing::before {
  opacity: 0.2;
}

.play-btn:hover::before {
  opacity: 0.3;
}

.progress-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}

.progress-bar {
  cursor: pointer;
  padding: 8px 0;
}

.progress-track {
  height: 6px;
  background: var(--bg-glass);
  border-radius: 3px;
  position: relative;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent-primary), var(--accent-secondary));
  border-radius: 3px;
  transition: width 0.1s ease;
  position: relative;
}

.progress-fill::after {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  width: 100px;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3));
  animation: shimmer 2s infinite;
}

.progress-thumb {
  position: absolute;
  top: 50%;
  width: 12px;
  height: 12px;
  background: white;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: opacity 0.2s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.progress-thumb.visible {
  opacity: 1;
}

.time-display {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 4px;
  font-size: 0.85rem;
  color: var(--text-secondary);
  min-width: 100px;
}

.separator {
  opacity: 0.5;
}

.volume-section {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.volume-control {
  display: flex;
  align-items: center;
  gap: 8px;
}

.volume-slider {
  width: 80px;
  cursor: pointer;
  padding: 8px 0;
}

.volume-track {
  height: 4px;
  background: var(--bg-glass);
  border-radius: 2px;
  position: relative;
}

.volume-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent-primary), var(--accent-secondary));
  border-radius: 2px;
  transition: width 0.2s ease;
}

.volume-thumb {
  position: absolute;
  top: 50%;
  width: 10px;
  height: 10px;
  background: white;
  border-radius: 50%;
  transform: translate(-50%, -50%);
  opacity: 0;
  transition: opacity 0.2s ease;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

.volume-thumb.visible {
  opacity: 1;
}

/* Animations */
@keyframes shimmer {
  0% { transform: translateX(-100px); }
  100% { transform: translateX(100px); }
}

@keyframes glow {
  0% { filter: brightness(1); }
  100% { filter: brightness(1.2); }
}

@keyframes pulse-dot {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.2); opacity: 0.8; }
}

/* Responsive design */
@media (max-width: 768px) {
  .music-controls {
    position: fixed;
    bottom: calc(20px + env(safe-area-inset-bottom));
    left: 20px;
    right: 20px;
    width: auto;
    min-width: auto;
    max-width: none;
    transform: none;
    padding: 12px 16px;
    border-radius: 16px;
    gap: 10px;
  }
  
  .music-controls.collapsed {
    padding: 10px 12px;
    border-radius: 12px;
  }
  
  .collapsed-track-name {
    font-size: 0.9rem;
  }
  
  .collapsed-meta {
    font-size: 0.75rem;
  }
  
  .track-name {
    font-size: 1rem;
  }
  
  .track-status {
    font-size: 0.8rem;
  }
  
  .controls-layout {
    gap: 12px;
  }
  
  .nav-controls {
    gap: 8px;
  }
  
  .progress-section {
    gap: 6px;
  }
  
  .progress-track {
    height: 4px;
  }
  
  .time-display {
    font-size: 0.75rem;
    min-width: 80px;
  }
  
  .volume-slider {
    width: 60px;
  }
  
  .volume-track {
    height: 3px;
  }
  
  .volume-control {
    gap: 6px;
  }
}

/* Very small screens */
@media (max-width: 480px) {
  .track-info {
    padding-bottom: 6px;
  }
  
  .track-name {
    font-size: 0.95rem;
  }
  
  .controls-layout {
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
  }
  
  .progress-section {
    order: -1;
    width: 100%;
  }
  
  .nav-controls {
    flex: 1;
    justify-content: center;
  }
  
  .volume-section {
    flex: 1;
    justify-content: center;
  }
  
  .collapsed-content {
    gap: 8px;
  }
  
  .collapsed-track-name {
    font-size: 0.85rem;
  }
  
  .collapsed-meta {
    font-size: 0.7rem;
    gap: 4px;
  }
}
</style>