import 'dart:typed_data';

/// Represents a graphical asset decoded from the VGA data chunks.
///
/// Unlike walls or sprites which are fixed at 64x64, [VgaImage] objects
/// have variable dimensions and are used for UI elements like the
/// status bar, fonts, title screens, and end-of-episode pictures.
class VgaImage {
  /// The horizontal width of the image in pixels.
  final int width;

  /// The vertical height of the image in pixels.
  final int height;

  /// The raw 8-bit paletted pixel data.
  ///
  /// Each byte in this list represents an index into the game's
  /// 256-color VGA palette. To render this image, these indices
  /// must be mapped to 32-bit colors using a [ColorPalette].
  final Uint8List pixels;

  VgaImage({
    required this.width,
    required this.height,
    required this.pixels,
  });

  /// Returns the palette index at `[srcX, srcY]` for planar VGA image data.
  ///
  /// Callers are expected to provide coordinates already clamped to the image
  /// bounds. The Wolf3D VGA format stores image bytes in 4 interleaved planes;
  /// this helper centralizes that mapping so renderers do not duplicate it.
  int decodePixel(int srcX, int srcY) {
    final int planeWidth = width ~/ 4;
    final int planeSize = planeWidth * height;
    final int plane = srcX % 4;
    final int sx = srcX ~/ 4;
    final int index = (plane * planeSize) + (srcY * planeWidth) + sx;
    return pixels[index];
  }
}
