package cz.jzitnik.events.handlers;

import com.googlecode.lanterna.TerminalSize;
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.PlayVideo;
import cz.jzitnik.events.TerminalResizeEvent;
import cz.jzitnik.game.GameState;
import cz.jzitnik.states.ScreenBuffer;
import cz.jzitnik.ui.pixels.Empty;
import cz.jzitnik.ui.pixels.Pixel;
import cz.jzitnik.utils.DependencyManager;
import cz.jzitnik.utils.events.AbstractEventHandler;
import cz.jzitnik.utils.events.EventManager;
import lombok.extern.slf4j.Slf4j;

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

    @InjectDependency
    private EventManager eventManager;

    @InjectState
    private ScreenBuffer screenBuffer;

    @InjectState
    private GameState gameState;

    @Override
    public void handle(TerminalResizeEvent event) {
        TerminalSize size = event.getNewSize();
        int width = size.getColumns();
        int height = size.getRows() * 2;

        Pixel[][] buffer = new Pixel[height][width];
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                buffer[y][x] = new Empty();
            }
        }
        screenBuffer.setRenderedBuffer(buffer);

        if (gameState.getScreen() != null) {
            gameState.getScreen().fullRender();
        } else {
            eventManager.emitEvent(new FullRoomDraw(true));
        }
    }
}
