<template>
  <div class="downloader-page">
    <h1 class="page-title">Openings Downloader</h1>

    <!-- Storage Stats Bar -->
    <div class="storage-bar">
      <div class="storage-info">
        <span class="storage-label">Storage:</span>
        <span class="storage-values">{{ formatBytes(storageStats.used_bytes) }} / {{ formatBytes(storageStats.limit_bytes) }}</span>
        <span class="storage-percent" :class="{ warning: storageStats.used_percent > 80, danger: storageStats.used_percent > 95 }">
          ({{ storageStats.used_percent.toFixed(1) }}%)
        </span>
        <span class="openings-count">{{ storageStats.openings_count }} openings</span>
      </div>
      <div class="progress-bar">
        <div
          class="progress-fill"
          :class="{ warning: storageStats.used_percent > 80, danger: storageStats.used_percent > 95 }"
          :style="{ width: Math.min(storageStats.used_percent, 100) + '%' }"
        ></div>
      </div>
      <div v-if="!storageStats.can_download" class="storage-warning">
        Storage limit exceeded! Cannot download new files.
      </div>
    </div>

    <div class="main-content">
      <!-- Left Panel: Search & Results -->
      <div class="search-panel">
        <!-- Search Section -->
        <div class="search-section">
          <div class="search-input-group">
            <input
              v-model="searchQuery"
              type="text"
              placeholder="Search anime..."
              @keyup.enter="searchAnime"
              class="search-input"
            />
            <button @click="searchAnime" class="btn btn-primary" :disabled="searching || !searchQuery.trim()">
              {{ searching ? 'Searching...' : 'Search' }}
            </button>
          </div>
          <div class="search-filters">
            <input v-model.number="yearFilter" type="number" placeholder="Year" class="filter-input" />
            <select v-model="statusFilter" class="filter-select">
              <option value="">Any status</option>
              <option value="ongoing">Ongoing</option>
              <option value="released">Released</option>
              <option value="announced">Announced</option>
            </select>
          </div>
        </div>

        <!-- Search Results -->
        <div class="search-results" v-if="searchResults.length">
          <h3>Search Results ({{ searchResults.length }})</h3>
          <div class="results-grid">
            <div
              v-for="anime in searchResults"
              :key="anime.animethemes_slug"
              class="anime-card"
              :class="{ selected: selectedAnime?.animethemes_slug === anime.animethemes_slug }"
              @click="selectAnime(anime.animethemes_slug)"
            >
              <img
                v-if="anime.poster_url"
                :src="anime.poster_url"
                :alt="anime.title_english"
                class="anime-poster"
              />
              <div v-else class="anime-poster placeholder">No Image</div>
              <div class="anime-info">
                <div class="anime-title">{{ anime.title_english }}</div>
                <div class="anime-year" v-if="anime.year">{{ anime.year }}</div>
              </div>
            </div>
          </div>
        </div>

        <div v-else-if="searched && !searching" class="no-results">
          No anime found for "{{ lastSearchQuery }}"
        </div>
      </div>

      <!-- Right Panel: Selected Anime & Queue -->
      <div class="detail-panel">
        <!-- Selected Anime Detail -->
        <div class="anime-detail" v-if="selectedAnime">
          <div class="detail-header">
            <img
              v-if="selectedAnime.poster_url"
              :src="selectedAnime.poster_url"
              class="detail-poster"
            />
            <div class="detail-info">
              <h2>{{ selectedAnime.title_english }}</h2>
              <p v-if="selectedAnime.year" class="year-info">Year: {{ selectedAnime.year }}</p>
            </div>
          </div>

          <div v-if="loadingDetail" class="loading">Loading themes...</div>

          <div v-else-if="selectedAnime.themes?.length" class="themes-section">
            <div class="themes-header">
              <h3>Available Themes ({{ selectedAnime.themes.length }})</h3>
              <button
                @click="addAllToQueue"
                class="btn btn-success"
                :disabled="!canAddAll || !storageStats.can_download"
              >
                Add All to Queue
              </button>
            </div>

            <div class="themes-list">
              <div
                v-for="theme in selectedAnime.themes"
                :key="theme.id"
                class="theme-item"
                :class="{ downloaded: theme.is_downloaded, 'in-queue': theme.download_status }"
              >
                <div class="theme-info">
                  <span class="theme-name" :class="theme.theme_type.toLowerCase()">{{ theme.full_name }}</span>
                  <span class="song-title" v-if="theme.song_title">{{ theme.song_title }}</span>
                  <span class="artist" v-if="theme.artist">by {{ theme.artist }}</span>
                </div>
                <div class="theme-status">
                  <span v-if="theme.is_downloaded" class="status-badge done">
                    Downloaded ({{ formatBytes(theme.file_size_bytes) }})
                  </span>
                  <span v-else-if="theme.download_status" :class="'status-badge ' + theme.download_status">
                    {{ theme.download_status }}
                  </span>
                  <button
                    v-else-if="theme.video_url"
                    @click="addToQueue([theme.id])"
                    class="btn btn-small btn-primary"
                    :disabled="!storageStats.can_download"
                  >
                    Add
                  </button>
                  <span v-else class="status-badge unavailable">No source</span>
                </div>
              </div>
            </div>
          </div>

          <div v-else class="no-themes">
            No themes found for this anime on AnimeThemes
          </div>
        </div>

        <!-- Download Queue -->
        <DownloadQueue
          :queue="queueStatus"
          @cancel="cancelTask"
          @retry="retryTask"
          @refresh="refreshQueue"
          @clear="clearQueue"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, computed, onMounted, onUnmounted } from 'vue'
import DownloadQueue from './DownloadQueue.vue'

// State
const searchQuery = ref('')
const lastSearchQuery = ref('')
const yearFilter = ref(null)
const statusFilter = ref('')
const searching = ref(false)
const searched = ref(false)
const searchResults = ref([])
const selectedAnime = ref(null)
const loadingDetail = ref(false)

const storageStats = reactive({
  used_bytes: 0,
  limit_bytes: 107374182400, // 100 GB default
  used_percent: 0,
  available_bytes: 107374182400,
  can_download: true,
  openings_count: 0,
})

const queueStatus = reactive({
  tasks: [],
  total_queued: 0,
  total_downloading: 0,
  total_done: 0,
  total_failed: 0,
  estimated_queue_size_bytes: 0,
  worker_running: false,
})

let refreshInterval = null

// Computed
const canAddAll = computed(() => {
  if (!selectedAnime.value?.themes) return false
  return selectedAnime.value.themes.some(
    t => !t.is_downloaded && !t.download_status && t.video_url
  )
})

// Methods
function formatBytes(bytes) {
  if (!bytes) return '0 B'
  const units = ['B', 'KB', 'MB', 'GB', 'TB']
  let i = 0
  let value = bytes
  while (value >= 1024 && i < units.length - 1) {
    value /= 1024
    i++
  }
  return `${value.toFixed(1)} ${units[i]}`
}

async function searchAnime() {
  if (!searchQuery.value.trim()) return

  searching.value = true
  searched.value = false
  lastSearchQuery.value = searchQuery.value
  searchResults.value = []
  selectedAnime.value = null

  try {
    const params = new URLSearchParams({ query: searchQuery.value, limit: '30' })
    if (yearFilter.value) params.append('year', yearFilter.value)
    if (statusFilter.value) params.append('status', statusFilter.value)

    const response = await fetch(`/api/downloader/search?${params}`)
    const data = await response.json()
    searchResults.value = data.results || []
  } catch (error) {
    console.error('Search error:', error)
  } finally {
    searching.value = false
    searched.value = true
  }
}

async function selectAnime(slug) {
  loadingDetail.value = true
  selectedAnime.value = null

  try {
    const response = await fetch(`/api/downloader/anime/${slug}`)
    const data = await response.json()
    selectedAnime.value = data
  } catch (error) {
    console.error('Error loading anime detail:', error)
  } finally {
    loadingDetail.value = false
  }
}

async function addToQueue(themeIds) {
  try {
    const response = await fetch('/api/downloader/queue/add', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ theme_ids: themeIds }),
    })
    const data = await response.json()
    if (response.ok) {
      Object.assign(queueStatus, data)
      // Refresh selected anime to update statuses
      if (selectedAnime.value) {
        await selectAnime(selectedAnime.value.animethemes_slug)
      }
    } else {
      alert(data.detail || 'Failed to add to queue')
    }
  } catch (error) {
    console.error('Error adding to queue:', error)
  }
}

async function addAllToQueue() {
  if (!selectedAnime.value?.id) return

  try {
    const response = await fetch('/api/downloader/queue/add-all', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ anime_id: selectedAnime.value.id }),
    })
    const data = await response.json()
    if (response.ok) {
      Object.assign(queueStatus, data)
      await selectAnime(selectedAnime.value.animethemes_slug)
    } else {
      alert(data.detail || 'Failed to add all to queue')
    }
  } catch (error) {
    console.error('Error adding all to queue:', error)
  }
}

async function cancelTask(taskId) {
  try {
    const response = await fetch(`/api/downloader/queue/${taskId}`, { method: 'DELETE' })
    if (response.ok) {
      await refreshQueue()
    }
  } catch (error) {
    console.error('Error cancelling task:', error)
  }
}

async function retryTask(taskId) {
  try {
    const response = await fetch(`/api/downloader/queue/${taskId}/retry`, { method: 'POST' })
    if (response.ok) {
      await refreshQueue()
    }
  } catch (error) {
    console.error('Error retrying task:', error)
  }
}

async function refreshQueue() {
  try {
    const response = await fetch('/api/downloader/queue')
    const data = await response.json()
    Object.assign(queueStatus, data)
  } catch (error) {
    console.error('Error refreshing queue:', error)
  }
}

async function clearQueue() {
  try {
    const response = await fetch('/api/downloader/queue/clear?include_failed=true', { method: 'DELETE' })
    if (response.ok) {
      await refreshQueue()
    }
  } catch (error) {
    console.error('Error clearing queue:', error)
  }
}

async function refreshStorage() {
  try {
    const response = await fetch('/api/downloader/storage')
    const data = await response.json()
    Object.assign(storageStats, data)
  } catch (error) {
    console.error('Error refreshing storage:', error)
  }
}

async function refreshAll() {
  await Promise.all([refreshQueue(), refreshStorage()])
  // Also refresh selected anime if any
  if (selectedAnime.value?.animethemes_slug) {
    const response = await fetch(`/api/downloader/anime/${selectedAnime.value.animethemes_slug}`)
    if (response.ok) {
      selectedAnime.value = await response.json()
    }
  }
}

// Lifecycle
onMounted(async () => {
  await refreshAll()
  // Auto-refresh every 3 seconds
  refreshInterval = setInterval(refreshAll, 3000)
})

onUnmounted(() => {
  if (refreshInterval) {
    clearInterval(refreshInterval)
  }
})
</script>

<style scoped>
.downloader-page {
  padding: 1rem;
  max-width: 1600px;
  margin: 0 auto;
}

.page-title {
  background: linear-gradient(90deg, #00d4ff, #7b2cbf);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 1rem;
}

/* Storage Bar */
.storage-bar {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 1.5rem;
}

.storage-info {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 0.5rem;
  flex-wrap: wrap;
}

.storage-label {
  color: #888;
}

.storage-values {
  font-weight: bold;
  color: #fff;
}

.storage-percent {
  color: #00ff88;
}

.storage-percent.warning {
  color: #ffaa00;
}

.storage-percent.danger {
  color: #ff4444;
}

.openings-count {
  color: #00d4ff;
  margin-left: auto;
}

.progress-bar {
  height: 8px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 4px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #00d4ff, #00ff88);
  transition: width 0.3s ease;
}

.progress-fill.warning {
  background: linear-gradient(90deg, #ffaa00, #ff8800);
}

.progress-fill.danger {
  background: linear-gradient(90deg, #ff4444, #ff0000);
}

.storage-warning {
  color: #ff4444;
  font-weight: bold;
  margin-top: 0.5rem;
  padding: 0.5rem;
  background: rgba(255, 68, 68, 0.1);
  border-radius: 4px;
}

/* Main Content */
.main-content {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
}

@media (max-width: 1200px) {
  .main-content {
    grid-template-columns: 1fr;
  }
}

/* Search Panel */
.search-panel {
  background: rgba(255, 255, 255, 0.03);
  border-radius: 8px;
  padding: 1rem;
}

.search-section {
  margin-bottom: 1rem;
}

.search-input-group {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.search-input {
  flex: 1;
  padding: 0.75rem 1rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.3);
  color: #fff;
  font-size: 1rem;
}

.search-input:focus {
  outline: none;
  border-color: #00d4ff;
}

.search-filters {
  display: flex;
  gap: 0.5rem;
}

.filter-input,
.filter-select {
  padding: 0.5rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.3);
  color: #fff;
  font-size: 0.9rem;
}

.filter-input {
  width: 100px;
}

/* Results Grid */
.results-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 1rem;
  max-height: 600px;
  overflow-y: auto;
}

.anime-card {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
}

.anime-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 212, 255, 0.2);
}

.anime-card.selected {
  border: 2px solid #00d4ff;
}

.anime-poster {
  width: 100%;
  aspect-ratio: 2/3;
  object-fit: cover;
}

.anime-poster.placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.05);
  color: #666;
}

.anime-info {
  padding: 0.5rem;
}

.anime-title {
  font-size: 0.85rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.anime-year {
  font-size: 0.75rem;
  color: #888;
}

.no-results {
  text-align: center;
  padding: 2rem;
  color: #888;
}

/* Detail Panel */
.detail-panel {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.anime-detail {
  background: rgba(255, 255, 255, 0.03);
  border-radius: 8px;
  padding: 1rem;
}

.detail-header {
  display: flex;
  gap: 1rem;
  margin-bottom: 1rem;
}

.detail-poster {
  width: 120px;
  height: 180px;
  object-fit: cover;
  border-radius: 8px;
}

.detail-info h2 {
  margin: 0 0 0.5rem 0;
  font-size: 1.2rem;
}

.japanese-title {
  color: #888;
  font-size: 0.9rem;
}

.year-info {
  color: #00d4ff;
}

/* Themes Section */
.themes-section {
  margin-top: 1rem;
}

.themes-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.themes-header h3 {
  margin: 0;
}

.themes-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-height: 400px;
  overflow-y: auto;
}

.theme-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 8px;
}

.theme-item.downloaded {
  border-left: 3px solid #00ff88;
}

.theme-item.in-queue {
  border-left: 3px solid #00d4ff;
}

.theme-info {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.theme-name {
  font-weight: bold;
  padding: 0.125rem 0.5rem;
  border-radius: 4px;
  display: inline-block;
  width: fit-content;
}

.theme-name.op {
  background: rgba(0, 212, 255, 0.2);
  color: #00d4ff;
}

.theme-name.ed {
  background: rgba(123, 44, 191, 0.2);
  color: #b57edc;
}

.song-title {
  color: #fff;
  font-size: 0.9rem;
}

.artist {
  color: #888;
  font-size: 0.8rem;
}

.theme-status {
  display: flex;
  align-items: center;
}

.status-badge {
  padding: 0.25rem 0.75rem;
  border-radius: 12px;
  font-size: 0.8rem;
  font-weight: 500;
}

.status-badge.done {
  background: rgba(0, 255, 136, 0.2);
  color: #00ff88;
}

.status-badge.queued {
  background: rgba(255, 170, 0, 0.2);
  color: #ffaa00;
}

.status-badge.downloading,
.status-badge.converting,
.status-badge.uploading {
  background: rgba(0, 212, 255, 0.2);
  color: #00d4ff;
}

.status-badge.failed {
  background: rgba(255, 68, 68, 0.2);
  color: #ff4444;
}

.status-badge.unavailable {
  background: rgba(136, 136, 136, 0.2);
  color: #888;
}

.no-themes {
  text-align: center;
  padding: 2rem;
  color: #888;
}

.loading {
  text-align: center;
  padding: 2rem;
  color: #00d4ff;
}

/* Buttons */
.btn {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  font-weight: 500;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.2s;
}

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

.btn:not(:disabled):hover {
  transform: translateY(-1px);
}

.btn-primary {
  background: linear-gradient(90deg, #00d4ff, #0099cc);
  color: #fff;
}

.btn-success {
  background: linear-gradient(90deg, #00ff88, #00cc6a);
  color: #000;
}

.btn-small {
  padding: 0.375rem 0.75rem;
  font-size: 0.85rem;
}
</style>
