import 'package:wolf_3d_dart/src/data_types/difficulty.dart';
import 'package:wolf_3d_dart/src/registry/keys/menu_pic_key.dart';
import 'package:wolf_3d_dart/src/registry/modules/menu_pic_module.dart';

/// Built-in menu-picture module for all retail Wolf3D releases (v1.0, v1.1, v1.4).
///
/// Picture indices match the [WolfMenuPic] constants that were previously the
/// sole authoritative source, preserving the original id Software VGA layout.
///
/// Note: indices are picture-table indices (not raw VGAGRAPH chunk IDs).
/// For example, `C_EPISODE1PIC` is chunk 30 → picture index 27.
class RetailMenuPicModule extends MenuPicModule {
  const RetailMenuPicModule();

  static const List<MenuPicKey> _episodeKeys = [
    MenuPicKey.episode1,
    MenuPicKey.episode2,
    MenuPicKey.episode3,
    MenuPicKey.episode4,
    MenuPicKey.episode5,
    MenuPicKey.episode6,
  ];

  static final Map<MenuPicKey, int> _indices = {
    // --- Full-screen art ---
    MenuPicKey.title: 84,
    MenuPicKey.credits: 86,
    MenuPicKey.pg13: 85,
    // --- Control-panel chrome ---
    MenuPicKey.controlBackground: 23, // C_CONTROLPIC
    MenuPicKey.footer: 15, // C_MOUSELBACKPIC (footer art)
    MenuPicKey.heading: 3, // H_TOPWINDOWPIC
    MenuPicKey.optionsLabel: 7, // C_OPTIONSPIC
    MenuPicKey.customizeLabel: 24, // C_CUSTOMIZEPIC
    // --- Cursor / markers ---
    MenuPicKey.cursorActive: 8, // C_CURSOR1PIC
    MenuPicKey.cursorInactive: 9, // C_CURSOR2PIC
    MenuPicKey.markerSelected: 11, // C_SELECTEDPIC
    MenuPicKey.markerUnselected: 10, // C_NOTSELECTEDPIC
    // --- Episode selection ---
    MenuPicKey.episode1: 27,
    MenuPicKey.episode2: 28,
    MenuPicKey.episode3: 29,
    MenuPicKey.episode4: 30,
    MenuPicKey.episode5: 31,
    MenuPicKey.episode6: 32,
    // --- Difficulty selection ---
    MenuPicKey.difficultyBaby: 16, // C_BABYMODEPIC
    MenuPicKey.difficultyEasy: 17, // C_EASYPIC
    MenuPicKey.difficultyNormal: 18, // C_NORMALPIC
    MenuPicKey.difficultyHard: 19, // C_HARDPIC
  };

  @override
  MenuPicRef? resolve(MenuPicKey key) {
    final index = _indices[key];
    return index != null ? MenuPicRef(index) : null;
  }

  @override
  MenuPicKey episodeKey(int episodeIndex) {
    if (episodeIndex >= 0 && episodeIndex < _episodeKeys.length) {
      return _episodeKeys[episodeIndex];
    }
    return MenuPicKey.episode1; // safe fallback
  }

  @override
  MenuPicKey difficultyKey(Difficulty difficulty) {
    return switch (difficulty) {
      Difficulty.baby => MenuPicKey.difficultyBaby,
      Difficulty.easy => MenuPicKey.difficultyEasy,
      Difficulty.medium => MenuPicKey.difficultyNormal,
      Difficulty.hard => MenuPicKey.difficultyHard,
    };
  }
}
