import 'package:wolf_3d_dart/wolf_3d_data_types.dart';

/// Handles identity and difficulty-based logic for enemies in map data.
///
/// Enemies in Wolf3D use a base 8-ID block (4 static, 4 patrol) and jump by
/// a tier offset (36 for most, 18 for Mutants) for higher difficulties.
class EnemyMapData {
  /// The starting ID for the "Easy" tier of this enemy (e.g., 108 for Guards).
  final int baseId;

  /// The ID jump between difficulty tiers (36 by default, 18 for Mutants).
  final int tierOffset;

  const EnemyMapData(this.baseId, {this.tierOffset = 36});

  /// Evaluates an [id] to see if it belongs to this enemy type and is valid
  /// for the current [difficulty].
  ///
  /// Returns the normalized "Easy" base ID (e.g., 108-115 for a Guard) if
  /// the enemy should spawn, or `null` if it should be skipped.
  int? getNormalizedId(int id, Difficulty difficulty) {
    // 1. Check Easy Tier (Base IDs)
    // The original C logic always spawns these regardless of difficulty.
    if (id >= baseId && id < baseId + 8) {
      return id;
    }

    // 2. Check Medium Tier (+1 tierOffset)
    int mediumBase = baseId + tierOffset;
    if (id >= mediumBase && id < mediumBase + 8) {
      // In Dart enum, Medium (Bring 'em on!) is level 1.
      if (difficulty.level < 1) return null;
      return id - tierOffset; // Shift down to base Easy IDs
    }

    // 3. Check Hard Tier (+2 tierOffsets)
    int hardBase = baseId + (tierOffset * 2);
    if (id >= hardBase && id < hardBase + 8) {
      // In Dart enum, Hard (Death incarnate!) is level 2.
      if (difficulty.level < 2) return null;
      return id - (tierOffset * 2); // Shift down to base Easy IDs
    }

    return null;
  }

  /// Returns true if the normalized ID represents a standing (static) version.
  bool isStatic(int normalizedId) {
    // The first 4 IDs are always Stand (matching the C switch statements)
    return normalizedId >= baseId && normalizedId < baseId + 4;
  }

  /// Returns true if the normalized ID represents a patrolling version.
  bool isPatrol(int normalizedId) {
    // The next 4 IDs are always Patrol
    return normalizedId >= baseId + 4 && normalizedId < baseId + 8;
  }
}
