package cz.jzitnik.events.handlers;

import cz.jzitnik.annotations.EventHandler;
import cz.jzitnik.annotations.injectors.InjectDependency;
import cz.jzitnik.annotations.injectors.InjectState;
import cz.jzitnik.events.FullRoomDraw;
import cz.jzitnik.events.RoomChangeEvent;
import cz.jzitnik.game.GameRoom;
import cz.jzitnik.game.GameState;
import cz.jzitnik.game.utils.RoomCords;
import cz.jzitnik.utils.DependencyManager;
import cz.jzitnik.utils.events.AbstractEventHandler;
import cz.jzitnik.utils.events.EventManager;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@EventHandler(RoomChangeEvent.class)
public class RoomChangeEventHandler extends AbstractEventHandler<RoomChangeEvent> {
    public RoomChangeEventHandler(DependencyManager dm) {
        super(dm);
    }

    @InjectState
    private GameState gameState;

    @InjectDependency
    private EventManager eventManager;

    @Override
    public void handle(RoomChangeEvent event) {
        RoomCords playerCords = gameState.getPlayer().getPlayerCords();
        GameRoom currentRoom = gameState.getCurrentRoom();
        GameRoom newRoom = switch (event.door()) {
            case LEFT -> currentRoom.getLeft();
            case RIGHT -> currentRoom.getRight();
            case TOP -> currentRoom.getUp();
            case BOTTOM -> currentRoom.getDown();
        };

        if (newRoom == null) {
            return;
        }

        switch (event.door()) {
            case LEFT -> playerCords.updateCords(155, 60);
            case RIGHT -> playerCords.updateCords(30, 50);
            case TOP -> playerCords.updateCords(90, 110);
            case BOTTOM -> playerCords.updateCords(90, 10);
        }

        gameState.setCurrentRoom(newRoom);
        eventManager.emitEvent(new FullRoomDraw());
    }
}
