import 'package:wolf_3d_dart/src/engine/wolf_3d_engine_base.dart';

import 'renderer_backend.dart';

/// Shared terminal orchestration for CLI renderers.
abstract class CliRendererBackend<T> extends RendererBackend<T> {
  /// Resolves the framebuffer dimensions required by this renderer.
  ///
  /// The default uses the full terminal size.
  ({int width, int height}) terminalFrameBufferSize(int columns, int rows) {
    return (width: columns, height: rows);
  }

  /// Applies terminal-size policy and updates the engine framebuffer.
  ///
  /// Returns `false` when the terminal is too small for this renderer.
  bool prepareTerminalFrame(
    WolfEngine engine, {
    required int columns,
    required int rows,
  }) {
    if (!isTerminalSizeSupported(columns, rows)) {
      return false;
    }

    final size = terminalFrameBufferSize(columns, rows);
    engine.setFrameBuffer(size.width, size.height);
    return true;
  }

  /// Builds the standard terminal size warning shown by the CLI host.
  String buildTerminalSizeWarning({required int columns, required int rows}) {
    return '\x1b[31m[ ERROR ] TERMINAL TOO SMALL\x1b[0m\n\n'
        '$terminalSizeRequirement\n'
        'Current size: \x1b[33m${columns}x$rows\x1b[0m\n\n'
        'Please resize your window to resume the game...';
  }
}
