package cz.jzitnik.events.handlers;

import com.googlecode.lanterna.input.KeyStroke;
import cz.jzitnik.annotations.EventHandler;
import cz.jzitnik.annotations.injectors.InjectDependency;
import cz.jzitnik.annotations.injectors.InjectState;
import cz.jzitnik.events.ExitEvent;
import cz.jzitnik.events.KeyboardPressEvent;
import cz.jzitnik.events.PlayerMoveEvent;
import cz.jzitnik.game.GameState;
import cz.jzitnik.utils.DependencyManager;
import cz.jzitnik.utils.events.AbstractEventHandler;
import cz.jzitnik.utils.events.EventManager;

@EventHandler(KeyboardPressEvent.class)
public class KeyboardPressEventHandler extends AbstractEventHandler<KeyboardPressEvent> {
    public KeyboardPressEventHandler(DependencyManager dm) {
        super(dm);
    }

    @InjectDependency
    private EventManager eventManager;

    @InjectState
    private GameState gameState;

    @Override
    public void handle(KeyboardPressEvent event) {
        if (gameState.getScreen() != null) {
            gameState.getScreen().handleKeyboardAction(event);
            return;
        }

        KeyStroke keyStroke = event.getKeyStroke();

        switch (keyStroke.getKeyType()) {
            case Escape:
                eventManager.emitEvent(new ExitEvent());
                break;
            case Character:
                switch (keyStroke.getCharacter()) {
                    case 'w','a','s','d' -> eventManager.emitEvent(new PlayerMoveEvent(keyStroke));
                }
                break;
            default:
                break;
        }
    }
}
