package de.thdeg.greiner.superpangworld.game.managers;

import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.moveable.PlayerObject;

import java.awt.event.KeyEvent;
import java.util.Arrays;

/**
 * The manager which handles the user input and controls the player object.
 */
class InputManager {

    /** The gameView, which displays the player object */
    private final GameView gameView;
    /** The player object */
    private final PlayerObject playerObject;

    /**
     * Create a manager handling the user input.
     *
     * @param gameView the GameView
     * @param playerObject the player object
     */
    public InputManager(GameView gameView, PlayerObject playerObject){
        this.gameView = gameView;
        this.playerObject = playerObject;
    }

    /**
     * Read the user inputs and perform actions accordingly.
     */
    public void updateUserInputs() {
        Integer[] pressedButtons = gameView.getKeyCodesOfCurrentlyPressedKeys();
        if(Arrays.stream(pressedButtons).anyMatch(k -> k == KeyEvent.VK_SPACE)){
            playerObject.shoot();
        }
        for (int keyCode : pressedButtons) {
            switch(keyCode) {
                case KeyEvent.VK_LEFT:
                    playerObject.left();
                    break;
                case KeyEvent.VK_RIGHT:
                    playerObject.right();
                    break;
                default:
                    continue;
            }
        }
    }
}
