import 'package:wolf_3d_dart/wolf_3d_entities.dart';
import 'package:wolf_3d_dart/src/input/wolf_3d_input.dart';

class CliInput extends Wolf3dInput {
  // Pending buffer for asynchronous stdin events
  bool _pForward = false;
  bool _pBackward = false;
  bool _pLeft = false;
  bool _pRight = false;
  bool _pFire = false;
  bool _pInteract = false;
  WeaponType? _pWeapon;

  /// Call this directly from the stdin listener to queue inputs for the next frame
  void handleKey(List<int> bytes) {
    String char = String.fromCharCodes(bytes).toLowerCase();

    if (char == 'w') _pForward = true;
    if (char == 's') _pBackward = true;
    if (char == 'a') _pLeft = true;
    if (char == 'd') _pRight = true;

    // --- NEW MAPPINGS ---
    if (char == 'j') _pFire = true;
    if (char == ' ') _pInteract = true;

    if (char == '1') _pWeapon = WeaponType.knife;
    if (char == '2') _pWeapon = WeaponType.pistol;
    if (char == '3') _pWeapon = WeaponType.machineGun;
    if (char == '4') _pWeapon = WeaponType.chainGun;
  }

  @override
  void update() {
    // 1. Move pending inputs to the active state
    isMovingForward = _pForward;
    isMovingBackward = _pBackward;
    isTurningLeft = _pLeft;
    isTurningRight = _pRight;
    isFiring = _pFire;
    isInteracting = _pInteract;
    requestedWeapon = _pWeapon;

    // 2. Wipe the pending slate clean for the next frame
    _pForward = _pBackward = _pLeft = _pRight = _pFire = _pInteract = false;
    _pWeapon = null;
  }
}
