<template>
  <div class="music-list">
    <div class="list-header">
      <div class="header-item index">#</div>
      <div class="header-item title">歌曲</div>
      <div class="header-item singer">歌手</div>
      <div class="header-item album">专辑</div>
      <div class="header-item duration">时长</div>
      <div class="header-item actions">操作</div>
    </div>

    <el-empty v-if="!loading && (!list || list.length === 0)" description="暂无音乐" />

    <el-skeleton v-if="loading" :rows="5" animated />

    <template v-else>
      <div
        v-for="(item, index) in list"
        :key="item.id"
        class="list-item"
        :class="{ 'active': isPlaying(item.id) }"
        @dblclick="playMusic(item)"
      >
        <div class="item-index">
          <span v-if="!isPlaying(item.id)">{{ index + 1 }}</span>
          <el-icon v-else class="playing-icon"><VideoPlay /></el-icon>
        </div>

        <div class="item-title">
          <div class="music-cover">
            <img :src="item.cover || '/default-cover.jpg'" alt="封面">
          </div>
          <div class="music-info">
            <div class="music-name">{{ item.name }}</div>
            <div class="music-type" v-if="item.typeName">{{ item.typeName }}</div>
          </div>
        </div>

        <div class="item-singer">
          <template v-if="item.singerNames && item.singerNames.length">
            <span v-for="(singer, idx) in item.singerNames" :key="idx">
              {{ singer }}{{ idx < item.singerNames.length - 1 ? '、' : '' }}
            </span>
          </template>
          <span v-else>{{ item.singer || '未知歌手' }}</span>
        </div>

        <div class="item-album">{{ item.album || '-' }}</div>

        <div class="item-duration">{{ formatDuration(item.duration) }}</div>

        <div class="item-actions">
          <el-button
            type="primary"
            circle
            size="small"
            @click="playMusic(item)"
            :title="isPlaying(item.id) ? '暂停' : '播放'"
          >
            <el-icon>
              <component :is="isPlaying(item.id) ? 'VideoPause' : 'VideoPlay'" />
            </el-icon>
          </el-button>

          <el-button
            :type="item.collected ? 'danger' : 'default'"
            circle
            size="small"
            @click="toggleCollect(item)"
            :title="item.collected ? '取消收藏' : '收藏'"
          >
            <el-icon><Star /></el-icon>
          </el-button>

          <el-button
            :type="item.liked ? 'success' : 'default'"
            circle
            size="small"
            @click="toggleLike(item)"
            :title="item.liked ? '取消点赞' : '点赞'"
          >
            <el-icon><Pointer /></el-icon>
          </el-button>

          <el-button
            v-if="!detailInMore"
            circle
            size="small"
            @click="goToMusicDetail(item.id)"
            title="查看详情"
          >
            <el-icon><InfoFilled /></el-icon>
          </el-button>

          <el-dropdown trigger="click" @command="handleCommand($event, item)">
            <el-button circle size="small">
              <el-icon><More /></el-icon>
            </el-button>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="addToPlaylist">添加到播放列表</el-dropdown-item>
                <el-dropdown-item command="playNext">下一首播放</el-dropdown-item>
                <el-dropdown-item v-if="userStore.isLoggedIn" command="showAddToPlaylistDialog" divided>添加到我的歌单</el-dropdown-item>
                <el-dropdown-item command="downloadMusic" :divided="!userStore.isLoggedIn">下载音乐</el-dropdown-item>
                <el-dropdown-item command="downloadLyric">下载歌词</el-dropdown-item>
                <el-dropdown-item v-if="detailInMore" command="goToMusicDetail">音乐详情</el-dropdown-item>
                <el-dropdown-item command="share" v-if="false">分享</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </template>

    <div class="pagination-container" v-if="showPagination && total > 0">
      <el-pagination
        v-model:current-page="localCurrentPage"
        v-model:page-size="localPageSize"
        :page-sizes="pageSizes"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 添加到歌单弹窗 -->
    <el-dialog v-model="isAddToPlaylistDialogVisible" title="添加到歌单" width="300px">
      <div v-loading="loadingUserPlaylists" class="dialog-playlist-container">
        <ul v-if="userPlaylists.length > 0" class="dialog-playlist-list">
          <li
            v-for="playlist in userPlaylists"
            :key="playlist.id"
            class="dialog-playlist-item"
            @click="handleAddToPlaylist(currentMusicToAdd, playlist.id)"
          >
            {{ playlist.name }}
          </li>
        </ul>
        <el-empty v-else-if="!loadingUserPlaylists" description="您还没有创建过歌单" />
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useRouter } from 'vue-router'
import { VideoPlay, VideoPause, Star, More, InfoFilled, Pointer } from '@element-plus/icons-vue'
import { usePlayerStore } from '../stores/player'
import { useUserStore } from '../stores/user'
import { toggleCollectMusic, toggleLikeMusic } from '../api/music'
import { getUserPlaylist, addMusicToPlaylist } from '../api/playlist'
import { ElMessage, ElMessageBox } from 'element-plus'

const props = defineProps({
  list: {
    type: Array,
    default: () => []
  },
  loading: {
    type: Boolean,
    default: false
  },
  showPagination: {
    type: Boolean,
    default: true
  },
  total: {
    type: Number,
    default: 0
  },
  currentPage: {
    type: Number,
    default: 1
  },
  pageSize: {
    type: Number,
    default: 10
  },
  detailInMore: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:currentPage', 'update:pageSize', 'page-change', 'collect-change', 'like-change'])

const router = useRouter()

// 使用本地状态来代替直接绑定到props
const localCurrentPage = ref(props.currentPage)
const localPageSize = ref(props.pageSize)
const pageSizes = [10, 20, 50, 100] // 添加分页大小选项

// 监听props变化，更新本地状态
watch(() => props.currentPage, (newVal) => {
  localCurrentPage.value = newVal
})

watch(() => props.pageSize, (newVal) => {
  localPageSize.value = newVal
})

const playerStore = usePlayerStore()
const userStore = useUserStore()

const userPlaylists = ref([])
const loadingUserPlaylists = ref(false)
const isAddToPlaylistDialogVisible = ref(false)
const currentMusicToAdd = ref(null)

// 获取用户创建的歌单
const fetchUserPlaylists = async () => {
  if (!userStore.isLoggedIn) {
    userPlaylists.value = []
    return
  }
  loadingUserPlaylists.value = true
  try {
    // 获取用户所有歌单，这里设置一个比较大的size
    const res = await getUserPlaylist(1, 200)
    if (res.code === 200) {
      userPlaylists.value = res.data.records || res.data.list || []
    } else {
      ElMessage.error(res.message || '获取您的歌单失败')
    }
  } catch (error) {
    console.error('获取用户歌单失败:', error)
    ElMessage.error('获取您的歌单失败')
  } finally {
    loadingUserPlaylists.value = false
  }
}

// 将音乐添加到歌单
const handleAddToPlaylist = async (music, playlistId) => {
  if (!music || !playlistId) return
  try {
    const res = await addMusicToPlaylist(playlistId, music.id)
    if (res.code === 200 && res.data) {
      ElMessage.success(`已将《${music.name}》添加到歌单`)
      isAddToPlaylistDialogVisible.value = false
    } else {
      // 这种情况可能不会发生，因为拦截器会处理非200代码
      ElMessage.error(res.message || '添加失败')
    }
  } catch (error) {
    // 错误已由axios拦截器处理，这里只记录日志
    console.error('添加到歌单失败:', error.message)
    // 由于拦截器已经显示了ElMessage，这里不再重复显示
  }
}

// 跳转到音乐详情页
const goToMusicDetail = (id) => {
  router.push({ name: 'music-detail', params: { id } })
}

// 检查音乐是否正在播放
const isPlaying = (id) => {
  return playerStore.getCurrentMusic?.id === id && playerStore.getIsPlaying
}

// 播放音乐
const playMusic = (music) => {
  if (isPlaying(music.id)) {
    playerStore.togglePlay()
  } else {
    playerStore.setCurrentMusic(music)
  }
}

// 切换收藏状态
const toggleCollect = async (music) => {
  try {
    const res = await toggleCollectMusic(music.id)
    if (res.code === 200) {
      music.collected = !music.collected
      ElMessage.success(music.collected ? '收藏成功' : '已取消收藏')
      emit('collect-change', music)
    } else {
      ElMessage.error(res.message || '操作失败')
    }
  } catch (error) {
    console.error('收藏操作失败:', error)
    ElMessage.error('操作失败，请稍后重试')
  }
}

// 切换点赞状态
const toggleLike = async (music) => {
  try {
    const res = await toggleLikeMusic(music.id)
    if (res.code === 200) {
      music.liked = !music.liked
      ElMessage.success(music.liked ? '点赞成功' : '已取消点赞')
      emit('like-change', music)
    } else {
      ElMessage.error(res.message || '操作失败')
    }
  } catch (error) {
    console.error('点赞操作失败:', error)
    ElMessage.error('操作失败，请稍后重试')
  }
}

// 处理下拉菜单命令
const handleCommand = (command, music) => {
  switch (command) {
    case 'addToPlaylist':
      playerStore.addToPlaylist(music)
      ElMessage.success('已添加到播放列表')
      break
    case 'playNext':
      // 添加到当前播放音乐的后面
      const currentIndex = playerStore.getCurrentIndex
      if (currentIndex !== -1) {
        const playlist = [...playerStore.getPlaylist]
        // 先移除如果已存在的相同音乐
        const existingIndex = playlist.findIndex(item => item.id === music.id)
        if (existingIndex !== -1) {
          playlist.splice(existingIndex, 1)
        }
        // 插入到当前播放音乐的后面
        playlist.splice(currentIndex + 1, 0, music)
        playerStore.$patch({ playlist })
        ElMessage.success('已添加到下一首播放')
      } else {
        playerStore.addToPlaylist(music)
        ElMessage.success('已添加到播放列表')
      }
      break
    case 'showAddToPlaylistDialog':
      currentMusicToAdd.value = music
      fetchUserPlaylists()
      isAddToPlaylistDialogVisible.value = true
      break
    case 'share':
      ElMessage.info('分享功能开发中')
      break
    case 'downloadMusic':
      downloadFile('music', music)
      break
    case 'downloadLyric':
      downloadFile('lyric', music)
      break
    case 'goToMusicDetail':
      goToMusicDetail(music.id)
      break
  }
}

// 下载文件
const downloadFile = (type, music) => {
  const typeName = type === 'music' ? '音乐' : '歌词'
  ElMessageBox.confirm(`确定要下载《${music.name}》的${typeName}吗？`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'info'
  }).then(() => {
    const url = `/api/file/download/${type}/${music.id}`
    window.open(url, '_blank')
    ElMessage.success(`${typeName}开始下载...`)
  }).catch(() => {
    ElMessage.info(`已取消下载${typeName}`)
  })
}

// 格式化时长
const formatDuration = (seconds) => {
  if (!seconds) return '00:00'

  const mins = Math.floor(seconds / 60)
  const secs = Math.floor(seconds % 60)
  return `${mins.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`
}

// 处理分页大小变化
const handleSizeChange = (size) => {
  localPageSize.value = size
  emit('update:pageSize', size)
  emit('page-change', { page: localCurrentPage.value, size })
}

// 处理页码变化
const handleCurrentChange = (page) => {
  localCurrentPage.value = page
  emit('update:currentPage', page)
  emit('page-change', { page, size: localPageSize.value })
}
</script>

<style scoped>
.dialog-playlist-container {
  min-height: 150px;
  max-height: 300px;
  overflow-y: auto;
}

.dialog-playlist-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.dialog-playlist-item {
  padding: 10px 15px;
  cursor: pointer;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.dialog-playlist-item:hover {
  background-color: #f5f7fa;
}

.music-list {
  width: 100%;
  border-radius: 8px;
  overflow: hidden;
  background-color: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.list-header {
  display: flex;
  align-items: center;
  padding: 15px;
  background-color: #f5f7fa;
  border-bottom: 1px solid #ebeef5;
  font-weight: bold;
  color: #606266;
}

.list-item {
  display: flex;
  align-items: center;
  padding: 12px 15px;
  border-bottom: 1px solid #ebeef5;
  transition: all 0.3s;
}

.list-item:hover {
  background-color: #f5f7fa;
}

.list-item.active {
  background-color: #ecf5ff;
  color: #409eff;
}

.header-item, .item-index, .item-title, .item-singer, .item-album, .item-duration, .item-actions {
  padding: 0 10px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.index, .item-index {
  width: 50px;
  text-align: center;
}

.title, .item-title {
  flex: 2;
  display: flex;
  align-items: center;
}

.singer, .item-singer {
  flex: 1;
}

.album, .item-album {
  flex: 1;
}

.duration, .item-duration {
  width: 80px;
  text-align: center;
}

.actions, .item-actions {
  width: 200px;
  text-align: right;
  display: flex;
  justify-content: flex-end;
  gap: 5px;
}

.music-cover {
  width: 40px;
  height: 40px;
  border-radius: 4px;
  overflow: hidden;
  margin-right: 10px;
}

.music-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.music-info {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.music-name {
  font-weight: bold;
  margin-bottom: 5px;
}

.music-type {
  font-size: 12px;
  color: #909399;
}

.playing-icon {
  color: #409eff;
  animation: pulse 1s infinite;
}

.pagination-container {
  padding: 15px;
  display: flex;
  justify-content: center;
}

@keyframes pulse {
  0% {
    opacity: 0.5;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0.5;
  }
}

@media (max-width: 768px) {
  .item-album, .header-item.album {
    display: none;
  }

  .item-actions {
    width: 100px;
  }
}
</style>
