import 'package:flutter/services.dart';
import 'package:wolf_3d_dart/wolf_3d_entities.dart';
import 'package:wolf_3d_dart/wolf_3d_input.dart';

class Wolf3dFlutterInput extends Wolf3dInput {
  Set<LogicalKeyboardKey> _previousKeys = {};

  @override
  void update() {
    final pressedKeys = HardwareKeyboard.instance.logicalKeysPressed;
    final newlyPressedKeys = pressedKeys.difference(_previousKeys);

    isMovingForward = pressedKeys.contains(LogicalKeyboardKey.keyW);
    isMovingBackward = pressedKeys.contains(LogicalKeyboardKey.keyS);
    isTurningLeft = pressedKeys.contains(LogicalKeyboardKey.keyA);
    isTurningRight = pressedKeys.contains(LogicalKeyboardKey.keyD);

    isInteracting = newlyPressedKeys.contains(LogicalKeyboardKey.space);

    isFiring =
        pressedKeys.contains(LogicalKeyboardKey.controlLeft) &&
        !pressedKeys.contains(LogicalKeyboardKey.space);

    requestedWeapon = null;
    for (final LogicalKeyboardKey key in newlyPressedKeys) {
      if (key == LogicalKeyboardKey.digit1) requestedWeapon = WeaponType.knife;
      if (key == LogicalKeyboardKey.digit2) requestedWeapon = WeaponType.pistol;
      if (key == LogicalKeyboardKey.digit3) {
        requestedWeapon = WeaponType.machineGun;
      }
      if (key == LogicalKeyboardKey.digit4) {
        requestedWeapon = WeaponType.chainGun;
      }
    }

    _previousKeys = Set.from(pressedKeys);
  }
}
