import 'package:wolf_3d_dart/src/registry/keys/hud_key.dart';
import 'package:wolf_3d_dart/src/registry/modules/hud_module.dart';

/// Built-in HUD module for all retail Wolf3D releases (v1.0, v1.1, v1.4).
///
/// VGA indices match the hard-coded values that were previously scattered
/// across [RendererBackend], preserving the original id Software HUD layout.
///
/// Face health bands follow the original formula:
///   `106 + ((100 - health) ~/ 16).clamp(0, 6) * 3`
class RetailHudModule extends HudModule {
  const RetailHudModule();

  // Ordered face keys from healthiest to most wounded (bands 0–6).
  static const List<HudKey> _faceBands = [
    HudKey.faceHealthy,
    HudKey.faceScratched,
    HudKey.faceHurt,
    HudKey.faceWounded,
    HudKey.faceBadlyWounded,
    HudKey.faceDying,
    HudKey.faceNearDeath,
  ];

  static final Map<HudKey, int> _indices = {
    HudKey.statusBar: 83,
    // Digits — '0' is index 96; subsequent digits follow sequentially.
    HudKey.digit0: 96,
    HudKey.digit1: 97,
    HudKey.digit2: 98,
    HudKey.digit3: 99,
    HudKey.digit4: 100,
    HudKey.digit5: 101,
    HudKey.digit6: 102,
    HudKey.digit7: 103,
    HudKey.digit8: 104,
    HudKey.digit9: 105,
    // BJ face bands (each band's base; 3 animated variants follow).
    HudKey.faceHealthy: 106,
    HudKey.faceScratched: 109,
    HudKey.faceHurt: 112,
    HudKey.faceWounded: 115,
    HudKey.faceBadlyWounded: 118,
    HudKey.faceDying: 121,
    HudKey.faceNearDeath: 124,
    HudKey.faceDead: 127,
    // Weapon icons.
    HudKey.pistolIcon: 89,
    HudKey.machineGunIcon: 90,
    HudKey.chainGunIcon: 91,
  };

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

  @override
  HudKey faceKeyForHealth(int health) {
    if (health <= 0) return HudKey.faceDead;
    final band = ((100 - health) ~/ 16).clamp(0, _faceBands.length - 1);
    return _faceBands[band];
  }
}
