/**
 * LocalStorage存储策略
 * 将数据保存在浏览器的localStorage中，持久化存储
 */
class LocalStorage {
  constructor(key) {
    this.key = `hxhq_local_${key}`;
    this.historyKey = `hxhq_local_history_${key}`;
  }

  /**
   * 保存数据到localStorage
   * @param {string} content - 要保存的内容
   * @returns {Promise<object>} 保存结果
   */
  async save(content) {
    try {
      const timestamp = Date.now();
      const version = this.getNextVersion();
      const data = {
        content,
        timestamp,
        version
      };

      // 保存当前版本
      localStorage.setItem(this.key, JSON.stringify(data));

      // 保存到历史记录
      this.saveToHistory(data);

      return {
        success: true,
        timestamp,
        version,
        storage: 'localStorage'
      };
    } catch (error) {
      return {
        success: false,
        error: error.message,
        storage: 'localStorage'
      };
    }
  }

  /**
   * 从localStorage加载数据
   * @returns {Promise<object>} 加载的数据
   */
  async load() {
    try {
      const dataStr = localStorage.getItem(this.key);
      const data = dataStr ? JSON.parse(dataStr) : null;
      
      return {
        success: true,
        data,
        storage: 'localStorage'
      };
    } catch (error) {
      return {
        success: false,
        error: error.message,
        storage: 'localStorage'
      };
    }
  }

  /**
   * 获取历史版本
   * @returns {Promise<Array>} 历史版本列表
   */
  async getHistory() {
    try {
      const historyStr = localStorage.getItem(this.historyKey);
      const history = historyStr ? JSON.parse(historyStr) : [];
      
      return {
        success: true,
        data: history,
        storage: 'localStorage'
      };
    } catch (error) {
      return {
        success: false,
        error: error.message,
        storage: 'localStorage'
      };
    }
  }

  /**
   * 保存到历史记录
   * @param {object} data - 要保存的数据
   */
  saveToHistory(data) {
    try {
      const historyStr = localStorage.getItem(this.historyKey);
      let history = historyStr ? JSON.parse(historyStr) : [];
      
      // 添加到历史记录开头
      history.unshift({...data});
      
      // 限制历史记录数量
      if (history.length > 100) {
        history = history.slice(0, 100);
      }
      
      localStorage.setItem(this.historyKey, JSON.stringify(history));
    } catch (error) {
      console.error('保存历史记录失败:', error);
    }
  }

  /**
   * 获取下一个版本号
   * @returns {number} 版本号
   */
  getNextVersion() {
    try {
      const dataStr = localStorage.getItem(this.key);
      if (!dataStr) return 1;
      
      const data = JSON.parse(dataStr);
      return data.version + 1;
    } catch (error) {
      return 1;
    }
  }

  /**
   * 获取备份文件列表
   * @returns {Promise<Array>} 备份文件列表
   */
  async getBackupFiles() {
    try {
      const files = [];
      for (let i = 0; i < localStorage.length; i++) {
        const key = localStorage.key(i);
        if (key.startsWith('backup_')) {
          const data = JSON.parse(localStorage.getItem(key));
          files.push({
            key,
            timestamp: data.timestamp,
            version: data.version,
            content: data.content.substring(0, 100) + '...'
          });
        }
      }
      return {
        success: true,
        data: files
      };
    } catch (error) {
      return {
        success: false,
        error: error.message
      };
    }
  }
}
export default LocalStorage;