import { ref, onMounted } from 'vue';
import createStorage, { Subscription, Channel, SourceValidity, Preferences, PlayHistory, CacheMeta } from '../storage/index.js';

const storage = createStorage();
const isReady = ref(false);

export { Subscription, Channel, SourceValidity, Preferences, PlayHistory, CacheMeta };

export function useStorage() {
  onMounted(async () => {
    if (storage.init) {
      await storage.init();
    }
    isReady.value = true;
  });

  // 基础操作
  const get = async (key) => {
    return await storage.get(key);
  };

  const set = async (key, value) => {
    return await storage.set(key, value);
  };

  const remove = async (key) => {
    return await storage.remove(key);
  };

  // 频道数据
  const getChannels = async () => {
    const channels = await storage.getChannels();
    return channels.map(c => new Channel(c));
  };

  const setChannels = async (channels) => {
    return await storage.setChannels(channels);
  };

  const getGroups = async () => {
    return await storage.getGroups();
  };

  // 检查列表缓存是否有效
  const isListCacheValid = async () => {
    const prefs = await storage.getPreferences();
    const ttl = prefs.listCacheTTL || 24 * 60 * 60 * 1000;
    return await storage.isCacheValid('channels', ttl);
  };

  // 线路有效性
  const getValidity = async (url) => {
    const v = await storage.getValidity(url);
    return v ? new SourceValidity(v) : null;
  };

  const setValidity = async (url, validity) => {
    return await storage.setValidity(url, validity);
  };

  const getAllValidity = async () => {
    const list = await storage.getAllValidity();
    return list.map(v => new SourceValidity(v));
  };

  // 检查有效性缓存是否有效
  const isValidityCacheValid = async (url) => {
    const prefs = await storage.getPreferences();
    const ttl = prefs.validityCacheTTL || 12 * 60 * 60 * 1000;
    const validity = await storage.getValidity(url);
    if (!validity || !validity.checkedAt) return false;
    return Date.now() - validity.checkedAt < ttl;
  };

  // 用户偏好
  const getPreferences = async () => {
    const prefs = await storage.getPreferences();
    return new Preferences(prefs);
  };

  const setPreferences = async (prefs) => {
    return await storage.setPreferences(prefs);
  };

  // 播放历史
  const getHistory = async (limit = 50) => {
    const list = await storage.getHistory(limit);
    return list.map(h => new PlayHistory(h));
  };

  const addHistory = async (item) => {
    return await storage.addHistory(item);
  };

  const clearHistory = async () => {
    return await storage.clearHistory();
  };

  // 订阅源
  const getSubscriptions = async () => {
    const list = await storage.getSubscriptions();
    return list.map(s => new Subscription(s));
  };

  const setSubscriptions = async (subs) => {
    return await storage.setSubscriptions(subs);
  };

  // 缓存元数据
  const getCacheMeta = async (key) => {
    const meta = await storage.getCacheMeta(key);
    return meta ? new CacheMeta(meta) : null;
  };

  const setCacheMeta = async (key, meta) => {
    return await storage.setCacheMeta(key, meta);
  };

  const isCacheValid = async (key, ttl) => {
    return await storage.isCacheValid(key, ttl);
  };

  // 清空所有数据
  const clear = async () => {
    return await storage.clear();
  };

  return {
    isReady,
    // 基础
    get,
    set,
    remove,
    clear,
    // 频道
    getChannels,
    setChannels,
    getGroups,
    isListCacheValid,
    // 有效性
    getValidity,
    setValidity,
    getAllValidity,
    isValidityCacheValid,
    // 偏好
    getPreferences,
    setPreferences,
    // 历史
    getHistory,
    addHistory,
    clearHistory,
    // 订阅
    getSubscriptions,
    setSubscriptions,
    // 元数据
    getCacheMeta,
    setCacheMeta,
    isCacheValid,
  };
}

// 创建单例
let storageInstance = null;
export function useStorageSingleton() {
  if (!storageInstance) {
    storageInstance = useStorage();
  }
  return storageInstance;
}
