<template>
  <div class="modal-overlay" @click="$emit('close')">
    <div class="modal-content" @click.stop>
      <div class="modal-header">
        <h3>设置</h3>
        <button class="close-btn" @click="$emit('close')">×</button>
      </div>
      
      <div class="settings-list">
        <!-- 自动播放 -->
        <div class="setting-item">
          <div class="setting-info">
            <span class="setting-label">自动播放</span>
            <span class="setting-desc">切换频道后自动开始播放</span>
          </div>
          <label class="switch">
            <input type="checkbox" v-model="settings.autoPlay" @change="saveSettings" />
            <span class="slider"></span>
          </label>
        </div>

        <!-- 首选画质 -->
        <div class="setting-item">
          <div class="setting-info">
            <span class="setting-label">首选画质</span>
            <span class="setting-desc">优先选择指定清晰度的线路</span>
          </div>
          <select v-model="settings.preferredQuality" @change="saveSettings">
            <option value="auto">自动</option>
            <option value="hd">高清</option>
            <option value="sd">标清</option>
          </select>
        </div>

        <!-- 缓存有效期 -->
        <div class="setting-item">
          <div class="setting-info">
            <span class="setting-label">频道列表缓存</span>
            <span class="setting-desc">频道数据缓存有效期</span>
          </div>
          <select v-model="listCacheHours" @change="updateCacheTTL">
            <option :value="6">6小时</option>
            <option :value="12">12小时</option>
            <option :value="24">1天</option>
            <option :value="72">3天</option>
          </select>
        </div>

        <div class="setting-item">
          <div class="setting-info">
            <span class="setting-label">线路检测缓存</span>
            <span class="setting-desc">线路可用性检测缓存有效期</span>
          </div>
          <select v-model="validityCacheHours" @change="updateCacheTTL">
            <option :value="1">1小时</option>
            <option :value="6">6小时</option>
            <option :value="12">12小时</option>
            <option :value="24">1天</option>
          </select>
        </div>

        <div class="divider"></div>

        <!-- 缓存管理 -->
        <div class="setting-item cache-actions">
          <div class="setting-info">
            <span class="setting-label">缓存管理</span>
            <span class="setting-desc">清除本地缓存数据</span>
          </div>
          <div class="cache-buttons">
            <button class="action-btn" @click="clearChannelCache">
              清除频道缓存
            </button>
            <button class="action-btn" @click="clearValidityCache">
              清除检测缓存
            </button>
            <button class="action-btn danger" @click="clearAllCache">
              清除全部缓存
            </button>
          </div>
        </div>

        <div class="divider"></div>

        <!-- 数据刷新 -->
        <div class="setting-item">
          <div class="setting-info">
            <span class="setting-label">刷新数据</span>
            <span class="setting-desc">立即从订阅源重新加载频道</span>
          </div>
          <button class="action-btn primary" @click="reloadChannels" :disabled="reloading">
            {{ reloading ? '刷新中...' : '立即刷新' }}
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useStorage } from '../../composables/useStorage.js';

const emit = defineEmits(['close', 'reload']);
const storage = useStorage();

const settings = ref({
  autoPlay: true,
  preferredQuality: 'auto',
  listCacheTTL: 24 * 60 * 60 * 1000,
  validityCacheTTL: 12 * 60 * 60 * 1000,
});

const listCacheHours = ref(24);
const validityCacheHours = ref(12);
const reloading = ref(false);

// 加载设置
onMounted(async () => {
  const prefs = await storage.getPreferences();
  settings.value = { ...settings.value, ...prefs };
  listCacheHours.value = Math.floor(prefs.listCacheTTL / (60 * 60 * 1000)) || 24;
  validityCacheHours.value = Math.floor(prefs.validityCacheTTL / (60 * 60 * 1000)) || 12;
});

// 保存设置
async function saveSettings() {
  await storage.setPreferences(settings.value);
}

// 更新缓存时间
async function updateCacheTTL() {
  settings.value.listCacheTTL = listCacheHours.value * 60 * 60 * 1000;
  settings.value.validityCacheTTL = validityCacheHours.value * 60 * 60 * 1000;
  await saveSettings();
}

// 清除频道缓存
async function clearChannelCache() {
  if (!confirm('确定要清除频道列表缓存吗？')) return;
  await storage.setChannels([]);
  await storage.setCacheMeta('channels', null);
  alert('频道缓存已清除');
}

// 清除检测缓存
async function clearValidityCache() {
  if (!confirm('确定要清除线路检测缓存吗？')) return;
  const all = await storage.getAllValidity();
  for (const v of all) {
    await storage.remove(`validity_${v.url}`);
  }
  alert('检测缓存已清除');
}

// 清除全部缓存
async function clearAllCache() {
  if (!confirm('确定要清除全部缓存数据吗？包括收藏和历史记录？')) return;
  await storage.clear();
  alert('全部缓存已清除');
  emit('reload');
  emit('close');
}

// 刷新频道
async function reloadChannels() {
  reloading.value = true;
  emit('reload');
  setTimeout(() => {
    reloading.value = false;
    emit('close');
  }, 1000);
}
</script>

<style scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 300;
}

.modal-content {
  background: #1a1a1a;
  border-radius: 12px;
  width: 90%;
  max-width: 500px;
  max-height: 80vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.modal-header h3 {
  margin: 0;
  font-size: 18px;
  color: #fff;
}

.close-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: #fff;
  font-size: 20px;
  cursor: pointer;
}

.settings-list {
  padding: 16px 20px;
}

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
}

.setting-item.cache-actions {
  flex-direction: column;
  align-items: flex-start;
  gap: 12px;
}

.setting-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.setting-label {
  font-size: 14px;
  color: #fff;
}

.setting-desc {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.5);
}

.divider {
  height: 1px;
  background: rgba(255, 255, 255, 0.1);
  margin: 8px 0;
}

/* Switch 开关 */
.switch {
  position: relative;
  width: 44px;
  height: 24px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 24px;
  transition: 0.3s;
}

.slider:before {
  content: '';
  position: absolute;
  height: 18px;
  width: 18px;
  left: 3px;
  bottom: 3px;
  background: #fff;
  border-radius: 50%;
  transition: 0.3s;
}

input:checked + .slider {
  background: #00ff88;
}

input:checked + .slider:before {
  transform: translateX(20px);
}

/* Select 下拉框 */
select {
  padding: 8px 12px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 6px;
  color: #fff;
  font-size: 13px;
  cursor: pointer;
}

select option {
  background: #1a1a1a;
}

/* 按钮 */
.cache-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.action-btn {
  padding: 8px 16px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 6px;
  color: #fff;
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;
}

.action-btn:hover {
  background: rgba(255, 255, 255, 0.2);
}

.action-btn.primary {
  background: #00ff88;
  color: #000;
  border-color: #00ff88;
}

.action-btn.primary:hover {
  background: #00cc6a;
}

.action-btn.danger {
  background: rgba(255, 68, 68, 0.2);
  border-color: rgba(255, 68, 68, 0.5);
  color: #ff4444;
}

.action-btn.danger:hover {
  background: rgba(255, 68, 68, 0.3);
}

.action-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
</style>
