<template>
  <Transition name="slide-up">
    <div 
      v-if="currentSong" 
      class="mini-player"
      :data-playing="isPlaying"
      @click="openFullPlayer"
      @touchstart="handleTouchStart"
      @touchmove="handleTouchMove"
      @touchend="handleTouchEnd"
    >
      <div class="mini-player-content">
        <!-- 专辑封面 -->
        <div class="cover-container">
          <img 
            :src="coverUrl" 
            :alt="currentSong.name"
            class="cover-image"
            @error="handleImageError"
          />
        </div>
        
        <!-- 歌曲信息 -->
        <div class="song-info">
          <div class="song-name">{{ currentSong.name }}</div>
          <div class="song-artist">{{ formatArtist(currentSong.artist) }}</div>
        </div>
        
        <!-- 播放控制 -->
        <div class="play-controls">
          <!-- PC端增加上一首按钮 -->
          <button 
            class="control-btn prev-btn hidden-mobile"
            @click.stop="$emit('playPrevious')"
          >
            <i class="fas fa-step-backward"></i>
          </button>
          
          <button 
            class="control-btn play-btn"
            @click.stop="togglePlay"
            :disabled="!audioSrc"
          >
            <i :class="isPlaying ? 'fas fa-pause' : 'fas fa-play'"></i>
          </button>
          
          <!-- PC端增加下一首按钮 -->
          <button 
            class="control-btn next-btn hidden-mobile"
            @click.stop="$emit('playNext')"
          >
            <i class="fas fa-step-forward"></i>
          </button>
        </div>
        
        <!-- PC端音量控制 -->
        <div class="volume-control hidden-mobile">
          <button class="volume-btn" @click.stop="toggleMute">
            <i :class="volumeIcon"></i>
          </button>
          <input 
            type="range" 
            min="0" 
            max="100" 
            :value="volume"
            @input="handleVolumeChange"
            @click.stop
            class="volume-slider"
          />
        </div>
        
        <!-- 进度条 -->
        <div class="progress-background">
          <div 
            class="progress-fill"
            :style="{ width: `${progress}%` }"
          ></div>
        </div>
      </div>
    </div>
  </Transition>
</template>

<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import { usePlayerStore } from '@/stores/player'
import { musicApi, utils } from '@/services/musicApi'

const emit = defineEmits(['openFullPlayer', 'togglePlay', 'playNext', 'playPrevious'])

const router = useRouter()

const playerStore = usePlayerStore()
const coverUrl = ref('')

// 音量控制
const isMuted = ref(false)
const lastVolume = ref(80)

// 触摸处理
const touchStartY = ref(0)
const touchStartX = ref(0)
const isSwiping = ref(false)

// 计算属性
const currentSong = computed(() => playerStore.currentSong)
const isPlaying = computed(() => playerStore.isPlaying)
const progress = computed(() => playerStore.progress)
const audioSrc = computed(() => playerStore.audioSrc)
const volume = computed(() => playerStore.volume || 80)

// 音量图标
const volumeIcon = computed(() => {
  if (isMuted.value || volume.value === 0) {
    return 'fas fa-volume-mute'
  } else if (volume.value < 30) {
    return 'fas fa-volume-off'
  } else if (volume.value < 70) {
    return 'fas fa-volume-down'
  } else {
    return 'fas fa-volume-up'
  }
})

// 格式化歌手名
const formatArtist = (artist) => {
  return utils.formatArtist(artist)
}

// 播放控制
const togglePlay = () => {
  // 通过事件通知父组件执行实际的播放控制逻辑
  // 这样确保mini播放器和大播放器使用相同的播放控制逻辑
  emit('togglePlay')
}

// 打开全屏播放器
const openFullPlayer = () => {
  // 直接使用路由跳转，不再依赖emit
  if (currentSong.value) {
    router.push('/player')
  }
}

// 音量控制方法
const toggleMute = () => {
  if (isMuted.value) {
    // 取消静音
    playerStore.setVolume(lastVolume.value)
    isMuted.value = false
  } else {
    // 静音
    lastVolume.value = volume.value
    playerStore.setVolume(0)
    isMuted.value = true
  }
}

const handleVolumeChange = (event) => {
  const newVolume = parseInt(event.target.value)
  playerStore.setVolume(newVolume)
  isMuted.value = newVolume === 0
}

// 加载专辑封面
const loadCover = async () => {
  if (!currentSong.value) {
    coverUrl.value = getDefaultCover()
    return
  }
  
  try {
    const coverUrlResult = await playerStore.getAlbumCover(currentSong.value, 300)
    if (coverUrlResult) {
      coverUrl.value = coverUrlResult
    } else {
      coverUrl.value = getDefaultCover()
    }
  } catch (error) {
    console.error('加载封面失败:', error)
    coverUrl.value = getDefaultCover()
  }
}

// 默认封面
const getDefaultCover = () => {
  return 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNjQiIGhlaWdodD0iNjQiIHZpZXdCb3g9IjAgMCA2NCA2NCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHJlY3Qgd2lkdGg9IjY0IiBoZWlnaHQ9IjY0IiBmaWxsPSJyZ2JhKDI1NSwyNTUsMjU1LDAuMSkiIHJ4PSI4Ii8+CjxwYXRoIGQ9Ik0zMiAyMEw0MCAzMkgzNlY0NEgyOFYzMkgyNEwzMiAyMFoiIGZpbGw9InJnYmEoMjU1LDI1NSwyNTUsMC4zKSIvPgo8L3N2Zz4K'
}

// 图片加载错误处理
const handleImageError = () => {
  coverUrl.value = getDefaultCover()
}

// 触摸事件处理
const handleTouchStart = (e) => {
  touchStartY.value = e.touches[0].clientY
  touchStartX.value = e.touches[0].clientX
  isSwiping.value = false
}

const handleTouchMove = (e) => {
  const deltaY = e.touches[0].clientY - touchStartY.value
  const deltaX = e.touches[0].clientX - touchStartX.value
  
  // 判断是否为上滑手势
  if (Math.abs(deltaY) > Math.abs(deltaX) && deltaY < -30) {
    isSwiping.value = true
    e.preventDefault()
  }
  
  // 判断是否为左右滑动切歌
  if (Math.abs(deltaX) > Math.abs(deltaY) && Math.abs(deltaX) > 50) {
    isSwiping.value = true
    if (deltaX > 0) {
      // 右滑：上一首
      emit('playPrevious')
    } else {
      // 左滑：下一首
      emit('playNext')
    }
  }
}

const handleTouchEnd = (e) => {
  const deltaY = touchStartY.value - e.changedTouches[0].clientY
  
  if (deltaY > 50 && !isSwiping.value) {
    // 上滑打开全屏播放器
    openFullPlayer()
  }
  
  touchStartY.value = 0
  touchStartX.value = 0
  isSwiping.value = false
}

// 监听当前歌曲变化
watch(() => currentSong.value, (newSong) => {
  if (newSong) {
    loadCover()
  } else {
    coverUrl.value = getDefaultCover()
  }
}, { immediate: true })

onMounted(() => {
  if (currentSong.value) {
    loadCover()
  }
})
</script>

<style scoped>
.mini-player {
  position: fixed;
  bottom: var(--tabbar-height);
  left: 0;
  right: 0;
  height: var(--mini-player-height);
  background: var(--bg-card);
  backdrop-filter: blur(20px);
  border-top: 1px solid var(--border-strong);
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
  z-index: 999;
  cursor: pointer;
  transition: var(--transition-fast);
}

/* iOS PWA 模式适配 */
@supports (-webkit-touch-callout: none) {
  @media all and (display-mode: standalone) {
    .mini-player {
      bottom: calc(var(--tabbar-height) + 20px);
    }
  }
}

.mini-player:hover {
  background: rgba(255, 255, 255, 0.08);
  box-shadow: 0 -2px 15px rgba(0, 0, 0, 0.1);
}

.mini-player-content {
  display: flex;
  align-items: center;
  height: 100%;
  padding: 0 16px;
  position: relative;
}

.cover-container {
  width: 48px;
  height: 48px;
  margin-right: 12px;
  flex-shrink: 0;
}

.cover-image {
  width: 100%;
  height: 100%;
  border-radius: 8px;
  object-fit: cover;
  transition: var(--transition-fast);
}

.song-info {
  flex: 1;
  min-width: 0;
  margin-right: 12px;
}

.song-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.song-artist {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.control-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--bg-overlay);
  border: 1px solid var(--border-light);
  color: var(--text-primary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: var(--transition-fast);
  position: relative;
  backdrop-filter: blur(10px);
}

/* PC端播放控制按钮调整 */
@media (min-width: 1024px) {
  .mini-player-content {
    padding: 0 20px;
  }
  
  .play-controls {
    gap: 4px;
  }
  
  .control-btn.prev-btn,
  .control-btn.next-btn {
    width: 32px;
    height: 32px;
    font-size: 12px;
    background: var(--overlay-lighter);
  }
  
  .control-btn.play-btn {
    width: 40px;
    height: 40px;
    font-size: 16px;
  }
}

/* PC端音量控制 */
.volume-control {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.volume-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--overlay-lighter);
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  transition: var(--transition-fast);
}

.volume-btn:hover {
  background: var(--overlay-light);
  color: var(--text-primary);
}

.volume-slider {
  width: 80px;
  height: 4px;
  background: var(--border-light);
  border-radius: 2px;
  outline: none;
  appearance: none;
  cursor: pointer;
  transition: var(--transition-fast);
}

.volume-slider::-webkit-slider-thumb {
  appearance: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--primary-color);
  cursor: pointer;
  border: 2px solid white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: var(--transition-fast);
}

.volume-slider::-webkit-slider-thumb:hover {
  transform: scale(1.2);
  box-shadow: 0 0 8px var(--glow-color);
}

.volume-slider::-moz-range-thumb {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--primary-color);
  cursor: pointer;
  border: 2px solid white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
  transition: var(--transition-fast);
}

/* PC端迷你播放器整体布局调整 */
@media (min-width: 1024px) {
  .mini-player {
    display: none; /* PC端隐藏迷你播放器，使用侧边栏播放器 */
  }
}

/* 播放状态时的视觉反馈 - 立即显示 */
.mini-player .control-btn {
  transition: all var(--transition-fast);
}

.mini-player[data-playing="true"] .control-btn {
  background: var(--primary-color);
  color: white;
  border-color: var(--primary-color);
  box-shadow: 0 0 20px var(--glow-color);
  animation: playing-pulse 2s ease-in-out infinite;
}

/* 非播放状态也给予适当的视觉提示 */
.mini-player[data-playing="false"] .control-btn {
  background: var(--bg-overlay);
  border: 1px solid var(--border-light);
  color: var(--primary-color);
  box-shadow: 0 2px 8px var(--shadow-color);
}

.mini-player[data-playing="false"] .control-btn:hover {
  background: var(--primary-color);
  color: white;
  transform: scale(1.05);
  box-shadow: 0 0 15px var(--glow-color);
}

@keyframes playing-pulse {
  0%, 100% {
    box-shadow: 0 0 20px var(--glow-color);
  }
  50% {
    box-shadow: 0 0 30px var(--glow-color), 0 0 40px var(--glow-color);
  }
}

.mini-player[data-playing="true"] .control-btn:hover:not(:disabled) {
  background: var(--primary-color-hover);
  transform: scale(1.1);
  box-shadow: 0 0 25px var(--glow-color), 0 0 35px var(--glow-color);
}

.control-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.progress-background {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 0 0 0 0;
}

.progress-fill {
  height: 100%;
  background: var(--primary-color);
  transition: width 0.1s ease;
  border-radius: 0 0 0 0;
}

/* 动画 */
.slide-up-enter-active,
.slide-up-leave-active {
  transition: transform 0.3s ease;
}

.slide-up-enter-from,
.slide-up-leave-to {
  transform: translateY(100%);
}

/* 响应式 */
@media (max-width: 375px) {
  .mini-player-content {
    padding: 0 12px;
  }
  
  .cover-container {
    width: 44px;
    height: 44px;
    margin-right: 10px;
  }
  
  .control-btn {
    width: 36px;
    height: 36px;
    font-size: 14px;
  }
}
</style>