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 Spear of Destiny demo/shareware (`.SDM`).
///
/// VGA picture indices are derived from `GFXV_SDM.H` (`chunkId - STARTPICS`).
class SpearDemoHudModule extends HudModule {
  const SpearDemoHudModule();

  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: 73, // STATUSBARPIC (76 - 3)
    HudKey.digit0: 84, // N_0PIC
    HudKey.digit1: 85,
    HudKey.digit2: 86,
    HudKey.digit3: 87,
    HudKey.digit4: 88,
    HudKey.digit5: 89,
    HudKey.digit6: 90,
    HudKey.digit7: 91,
    HudKey.digit8: 92,
    HudKey.digit9: 93,
    HudKey.faceHealthy: 94, // FACE1APIC
    HudKey.faceScratched: 97,
    HudKey.faceHurt: 100,
    HudKey.faceWounded: 103,
    HudKey.faceBadlyWounded: 106,
    HudKey.faceDying: 109,
    HudKey.faceNearDeath: 112,
    HudKey.faceDead: 122, // BJOUCHPIC
    HudKey.faceGotGatling: 116, // GOTGATLINGPIC
    HudKey.faceGodMode: 117, // GODMODEFACE1PIC
    HudKey.pistolIcon: 77, // GUNPIC
    HudKey.machineGunIcon: 78, // MACHINEGUNPIC
    HudKey.chainGunIcon: 79, // GATLINGGUNPIC
    HudKey.noKeyIcon: 80,
    HudKey.goldKeyIcon: 81,
    HudKey.silverKeyIcon: 82,
  };

  @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];
  }
}
