<template>
  <div class="artists-page">
    <div class="container">
      <div class="page-header">
        <h1 class="page-title">作者管理</h1>
        <div class="header-actions">
          <!-- 关注类型切换 -->
          <div class="follow-type-toggle">
            <button 
              @click="switchFollowType('public')" 
              :class="['toggle-btn', { active: followType === 'public' }]"
              :disabled="artistStore.loading"
            >
              公开关注
            </button>
            <button 
              @click="switchFollowType('private')" 
              :class="['toggle-btn', { active: followType === 'private' }]"
              :disabled="artistStore.loading"
            >
              非公开关注
            </button>
          </div>
          <button @click="handleRefresh" class="btn btn-secondary" :disabled="artistStore.loading">
            <SvgIcon name="refresh" class="refresh-icon" />
            刷新
          </button>
        </div>
      </div>

      <div v-if="artistStore.error" class="error-section">
        <ErrorMessage :error="artistStore.error" @dismiss="artistStore.clearError" />
      </div>

      <div v-if="artistStore.loading" class="loading-section">
        <LoadingSpinner text="正在获取最新数据..." />
      </div>

      <div v-else class="artists-content">
        <!-- 关注列表 -->
        <div class="section">
          <div class="section-header">
            <h2 class="section-title">关注的作者</h2>
            <div v-if="artistStore.hasFollowingArtists" class="cache-indicator">
              <span v-if="artistStore.isDataStale" class="cache-status stale">
                <SvgIcon name="empty2" class="cache-icon" />
                数据已过期
              </span>
              <span v-else class="cache-status fresh">
                <SvgIcon name="success" class="cache-icon" />
                数据已缓存
              </span>
            </div>
          </div>

          <!-- 作者统计信息 -->
          <div v-if="artistStore.hasFollowingArtists" class="stats-section">
            <span>共 {{ artistStore.followingArtists.length }} 个关注的作者</span>
          </div>

          <div v-if="artistStore.followingArtists.length > 0" class="artists-grid">
            <ArtistCard v-for="artist in artistStore.followingArtists" :key="artist.id" :artist="artist"
              :show-follow-button="false" :show-unfollow-button="true" @unfollow="handleUnfollow"
              @download="openDownloadDialog" />
          </div>

          <div v-else class="empty-section">
            <div class="empty-content">
              <SvgIcon name="user" class="empty-icon" />
              <h3>暂无关注的作者</h3>
              <p>关注喜欢的作者，在这里管理他们</p>
              <div v-if="!artistStore.loading && artistStore.hasFollowingArtists" class="cache-note">
                <small>💡 提示：数据已缓存，点击刷新按钮获取最新数据</small>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 下载弹出框 -->
    <div v-if="showDownloadDialog" class="modal-overlay" @click="closeDownloadDialog">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>下载作品</h3>
          <button @click="closeDownloadDialog" class="modal-close">
            <SvgIcon name="close" class="modal-close-icon" />
          </button>
        </div>

        <div class="modal-body">
          <div class="artist-info-modal">
            <img :src="selectedArtist?.profile_image_urls.medium" :alt="selectedArtist?.name"
              class="artist-avatar-modal" crossorigin="anonymous" />
            <div>
              <h4>{{ selectedArtist?.name }}</h4>
              <p>@{{ selectedArtist?.account }}</p>
            </div>
          </div>

          <div class="download-options">
            <div class="download-input-group">
              <label for="downloadLimit">下载数量:</label>
              <select v-model="downloadLimit" id="downloadLimit" class="download-select">
                <option value="10">10个</option>
                <option value="30">30个</option>
                <option value="50">50个</option>
                <option value="100">100个</option>
                <option value="200">200个</option>
                <option value="500">500个</option>
                <option value="9999">全部</option>
              </select>
            </div>
          </div>
        </div>

        <div class="modal-footer">
          <button @click="closeDownloadDialog" class="btn btn-secondary">
            取消
          </button>
          <button @click="handleDownloadArtist" class="btn btn-primary" :disabled="downloading">
            {{ downloading ? '下载中...' : '开始下载' }}
          </button>
        </div>
      </div>
    </div>

    <!-- 下载成功提示 -->
    <div v-if="downloadSuccess" class="success-message">
      <div class="success-content">
        <SvgIcon name="success" class="success-icon" />
        <span>{{ downloadSuccess }}</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { useAuthStore } from '@/stores/auth';
import { useArtistStore } from '@/stores/artist';
import downloadService from '@/services/download';

import ArtistCard from '@/components/artist/ArtistCard.vue';

const router = useRouter();
const route = useRoute();
const authStore = useAuthStore();
const artistStore = useArtistStore();

// 下载弹出框相关
const showDownloadDialog = ref(false);
const selectedArtist = ref<any>(null);
const downloadLimit = ref('50');
const downloading = ref(false);
const downloadSuccess = ref<string | null>(null);

// 关注类型切换
const followType = ref<'public' | 'private'>('public');

// 获取关注的作者
const fetchFollowingArtists = async (forceRefresh = false) => {
  try {
    artistStore.loading = true;
    artistStore.error = null;
    
    const response = await artistStore.fetchFollowingArtists(forceRefresh, {
      restrict: followType.value
    });
    
    if (!response.success) {
      throw new Error('获取关注列表失败');
    }
  } catch (err) {
    artistStore.error = err instanceof Error ? err.message : '获取关注列表失败';
    console.error('获取关注列表失败:', err);
  } finally {
    artistStore.loading = false;
  }
};

// 切换关注类型
const switchFollowType = async (type: 'public' | 'private') => {
  followType.value = type;
  
  // 更新URL参数
  router.push({
    query: {
      type: type
    }
  });
  
  await fetchFollowingArtists(true);
};

// 关注作者
const handleFollow = async (artistId: number) => {
  try {
    await artistStore.followArtist(artistId);
  } catch (err) {
    console.error('关注失败:', err);
  }
};

// 取消关注
const handleUnfollow = async (artistId: number) => {
  try {
    await artistStore.unfollowArtist(artistId);
    // 重新获取数据
    await fetchFollowingArtists(true);
  } catch (err) {
    console.error('取消关注失败:', err);
  }
};

// 打开下载弹出框
const openDownloadDialog = (artist: any) => {
  selectedArtist.value = artist;
  showDownloadDialog.value = true;
};

// 关闭下载弹出框
const closeDownloadDialog = () => {
  showDownloadDialog.value = false;
  selectedArtist.value = null;
  downloadLimit.value = '50';
  downloading.value = false;
};

// 处理下载
const handleDownloadArtist = async () => {
  if (!selectedArtist.value) return;

  try {
    downloading.value = true;
    const response = await downloadService.downloadArtistArtworks(selectedArtist.value.id, {
      limit: parseInt(downloadLimit.value)
    });

    if (response.success) {
      console.log('下载任务已创建:', response.data);
      const limitText = downloadLimit.value === '9999' ? '全部' : downloadLimit.value;
      downloadSuccess.value = `下载任务已创建，将下载 ${limitText} 个作品`;

      // 关闭弹出框
      closeDownloadDialog();

      // 3秒后清除成功提示
      setTimeout(() => {
        downloadSuccess.value = null;
      }, 3000);

      router.push('/downloads');
    } else {
      throw new Error(response.error || '下载失败');
    }
  } catch (err) {
    artistStore.error = err instanceof Error ? err.message : '下载失败';
    console.error('下载失败:', err);
  } finally {
    downloading.value = false;
  }
};

// 刷新数据
const handleRefresh = async () => {
  try {
    await fetchFollowingArtists(true);
  } catch (err) {
    console.error('刷新失败:', err);
  }
};

// 监听路由变化
watch(() => route.query, () => {
  // 恢复关注类型状态
  const urlType = route.query.type as string;
  if (urlType && ['public', 'private'].includes(urlType) && urlType !== followType.value) {
    followType.value = urlType as 'public' | 'private';
    fetchFollowingArtists(true);
  }
});

onMounted(async () => {
  // 检查URL参数并恢复状态
  const urlType = route.query.type as string;
  if (urlType && ['public', 'private'].includes(urlType)) {
    followType.value = urlType as 'public' | 'private';
  }
  
  await fetchFollowingArtists();
});
</script>

<style scoped>
.artists-page {
  min-height: 100vh;
  background: #f8fafc;
  padding: 2rem 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 2rem;
}

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

.page-title {
  font-size: 2rem;
  font-weight: 700;
  color: #1f2937;
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 1rem;
}

.follow-type-toggle {
  display: flex;
  gap: 0.25rem;
  background: #f3f4f6;
  border: 1px solid #d1d5db;
  border-radius: 0.5rem;
  padding: 0.25rem;
  margin-right: 1rem;
}

.toggle-btn {
  background: none;
  border: none;
  padding: 0.5rem 1rem;
  cursor: pointer;
  font-size: 0.875rem;
  color: #6b7280;
  border-radius: 0.375rem;
  transition: all 0.2s;
  font-weight: 500;
}

.toggle-btn:hover:not(:disabled) {
  background: #e5e7eb;
  color: #374151;
}

.toggle-btn:disabled {
  background: #f9fafb;
  color: #9ca3af;
  cursor: not-allowed;
}

.toggle-btn.active {
  background: #4f46e5;
  color: white;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.refresh-icon {
  width: 1rem;
  height: 1rem;
  margin-right: 0.5rem;
}

.error-section,
.loading-section {
  margin-bottom: 2rem;
}

.loading-section {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 200px;
}

.section {
  margin-bottom: 3rem;
}

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

.section-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: #1f2937;
  margin: 0;
}

.cache-indicator {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.cache-status {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  font-size: 0.75rem;
  padding: 0.25rem 0.5rem;
  border-radius: 0.25rem;
  font-weight: 500;
}

.cache-status.fresh {
  background: #dcfce7;
  color: #166534;
}

.cache-status.stale {
  background: #fef3c7;
  color: #92400e;
}

.cache-icon {
  width: 0.875rem;
  height: 0.875rem;
}

.stats-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
  padding: 0.75rem 1rem;
  background: #f3f4f6;
  border-radius: 0.5rem;
  font-size: 0.875rem;
  color: #6b7280;
}

.artists-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 1.5rem;
  margin-bottom: 2rem;
}

.empty-section {
  text-align: center;
  padding: 4rem 0;
}

.empty-content {
  max-width: 400px;
  margin: 0 auto;
}

.empty-icon {
  width: 4rem;
  height: 4rem;
  color: #9ca3af;
  margin-bottom: 1rem;
}

.empty-content h3 {
  font-size: 1.5rem;
  font-weight: 600;
  color: #374151;
  margin-bottom: 0.5rem;
}

.empty-content p {
  color: #6b7280;
  line-height: 1.6;
}

.cache-note {
  margin-top: 1rem;
  padding: 0.5rem;
  background: #f3f4f6;
  border-radius: 0.375rem;
  text-align: center;
}

.cache-note small {
  color: #6b7280;
  font-size: 0.75rem;
}

/* Modal Styles */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 1rem;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
  width: 90%;
  max-width: 500px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1.5rem;
  border-bottom: 1px solid #e5e7eb;
  background: #f9fafb;
}

.modal-header h3 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 600;
  color: #1f2937;
}

.modal-close {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.5rem;
  color: #6b7280;
  transition: color 0.2s;
}

.modal-close:hover {
  color: #374151;
}

.modal-body {
  padding: 1.5rem;
  overflow-y: auto;
  flex-grow: 1;
}

.artist-info-modal {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1.5rem;
  padding-bottom: 1.5rem;
  border-bottom: 1px solid #e5e7eb;
}

.artist-avatar-modal {
  width: 4rem;
  height: 4rem;
  border-radius: 50%;
  object-fit: cover;
}

.artist-info-modal h4 {
  margin: 0 0 0.25rem 0;
  font-size: 1.125rem;
  font-weight: 600;
  color: #1f2937;
}

.artist-info-modal p {
  margin: 0;
  color: #6b7280;
  font-size: 0.875rem;
}

.download-options {
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid #e5e7eb;
}

.download-input-group {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
}

.download-input-group label {
  font-size: 0.875rem;
  color: #374151;
  font-weight: 500;
}

.download-select {
  padding: 0.75rem 1rem;
  border: 1px solid #d1d5db;
  border-radius: 0.5rem;
  font-size: 1rem;
  color: #1f2937;
  background: white;
  cursor: pointer;
  transition: border-color 0.2s;
}

.download-select:focus {
  outline: none;
  border-color: #3b82f6;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 1rem;
  padding: 1.5rem;
  border-top: 1px solid #e5e7eb;
  background: #f9fafb;
}

.success-message {
  position: fixed;
  top: 20px;
  right: 20px;
  background: #d1fae5;
  color: #065f46;
  padding: 1rem;
  border-radius: 0.5rem;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  gap: 0.75rem;
  z-index: 999;
}

.success-content {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.success-icon {
  width: 1.25rem;
  height: 1.25rem;
}

.success-message span {
  font-size: 0.875rem;
  font-weight: 500;
}

@media (max-width: 768px) {
  .container {
    padding: 0 1rem;
  }

  .page-header {
    flex-direction: column;
    gap: 1rem;
    align-items: stretch;
  }

  .header-actions {
    flex-direction: column;
    gap: 1rem;
  }

  .follow-type-toggle {
    margin-right: 0;
    justify-content: center;
  }

  .artists-grid {
    grid-template-columns: 1fr;
  }

  .stats-section {
    flex-direction: column;
    gap: 0.5rem;
    text-align: center;
  }
}
</style>