/**
 * 简化版性能管理器
 * 提供基础的性能统计功能
 */
export class PerformanceManager {
  constructor() {
    this.stats = {
      totalExecutions: 0,
      totalUndos: 0,
      totalRedos: 0,
      totalExecutionTime: 0,
      totalUndoTime: 0,
      totalRedoTime: 0,
      commandStats: new Map(), // 每个命令的统计信息
      recentOperations: [], // 最近的操作记录
    };

    this.maxRecentOperations = 100;
  }

  /**
   * 记录命令执行
   */
  recordExecution(commandName, duration) {
    this.stats.totalExecutions++;
    this.stats.totalExecutionTime += duration;

    this._updateCommandStats(commandName, "executions", duration);
    this._addRecentOperation("execute", commandName, duration);
  }

  /**
   * 记录撤销操作
   */
  recordUndo(commandName, duration) {
    this.stats.totalUndos++;
    this.stats.totalUndoTime += duration;

    this._updateCommandStats(commandName, "undos", duration);
    this._addRecentOperation("undo", commandName, duration);
  }

  /**
   * 记录重做操作
   */
  recordRedo(commandName, duration) {
    this.stats.totalRedos++;
    this.stats.totalRedoTime += duration;

    this._updateCommandStats(commandName, "redos", duration);
    this._addRecentOperation("redo", commandName, duration);
  }

  /**
   * 获取统计信息
   */
  getStats() {
    const avgExecutionTime =
      this.stats.totalExecutions > 0
        ? this.stats.totalExecutionTime / this.stats.totalExecutions
        : 0;

    const avgUndoTime =
      this.stats.totalUndos > 0 ? this.stats.totalUndoTime / this.stats.totalUndos : 0;

    const avgRedoTime =
      this.stats.totalRedos > 0 ? this.stats.totalRedoTime / this.stats.totalRedos : 0;

    return {
      overview: {
        totalExecutions: this.stats.totalExecutions,
        totalUndos: this.stats.totalUndos,
        totalRedos: this.stats.totalRedos,
        avgExecutionTime: Number(avgExecutionTime.toFixed(2)),
        avgUndoTime: Number(avgUndoTime.toFixed(2)),
        avgRedoTime: Number(avgRedoTime.toFixed(2)),
      },
      commandBreakdown: Array.from(this.stats.commandStats.entries()).map(([name, stats]) => ({
        commandName: name,
        executions: stats.executions,
        undos: stats.undos,
        redos: stats.redos,
        avgExecutionTime:
          stats.executions > 0
            ? Number((stats.totalExecutionTime / stats.executions).toFixed(2))
            : 0,
        avgUndoTime: stats.undos > 0 ? Number((stats.totalUndoTime / stats.undos).toFixed(2)) : 0,
        avgRedoTime: stats.redos > 0 ? Number((stats.totalRedoTime / stats.redos).toFixed(2)) : 0,
      })),
      recentOperations: this.stats.recentOperations.slice(-20), // 最近20个操作
    };
  }

  /**
   * 获取慢命令报告
   */
  getSlowCommandsReport(threshold = 100) {
    const slowCommands = [];

    for (const [name, stats] of this.stats.commandStats.entries()) {
      const avgExecTime = stats.executions > 0 ? stats.totalExecutionTime / stats.executions : 0;
      const avgUndoTime = stats.undos > 0 ? stats.totalUndoTime / stats.undos : 0;

      if (avgExecTime > threshold || avgUndoTime > threshold) {
        slowCommands.push({
          commandName: name,
          avgExecutionTime: Number(avgExecTime.toFixed(2)),
          avgUndoTime: Number(avgUndoTime.toFixed(2)),
          executions: stats.executions,
          undos: stats.undos,
        });
      }
    }

    return slowCommands.sort(
      (a, b) =>
        Math.max(b.avgExecutionTime, b.avgUndoTime) - Math.max(a.avgExecutionTime, a.avgUndoTime)
    );
  }

  /**
   * 重置统计信息
   */
  reset() {
    this.stats = {
      totalExecutions: 0,
      totalUndos: 0,
      totalRedos: 0,
      totalExecutionTime: 0,
      totalUndoTime: 0,
      totalRedoTime: 0,
      commandStats: new Map(),
      recentOperations: [],
    };
  }

  /**
   * 更新命令统计信息
   * @private
   */
  _updateCommandStats(commandName, type, duration) {
    if (!this.stats.commandStats.has(commandName)) {
      this.stats.commandStats.set(commandName, {
        executions: 0,
        undos: 0,
        redos: 0,
        totalExecutionTime: 0,
        totalUndoTime: 0,
        totalRedoTime: 0,
      });
    }

    const stats = this.stats.commandStats.get(commandName);

    if (type === "executions") {
      stats.executions++;
      stats.totalExecutionTime += duration;
    } else if (type === "undos") {
      stats.undos++;
      stats.totalUndoTime += duration;
    } else if (type === "redos") {
      stats.redos++;
      stats.totalRedoTime += duration;
    }
  }

  /**
   * 添加最近操作记录
   * @private
   */
  _addRecentOperation(type, commandName, duration) {
    this.stats.recentOperations.push({
      type,
      commandName,
      duration: Number(duration.toFixed(2)),
      timestamp: Date.now(),
    });

    // 限制记录数量
    if (this.stats.recentOperations.length > this.maxRecentOperations) {
      this.stats.recentOperations.shift();
    }
  }
}
