package cz.jzitnik.game.handlers.events.handlers.mobs;

import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.CustomMobFallingAfterMineLogic;
import cz.jzitnik.game.annotations.MineEventHandler;
import cz.jzitnik.game.entities.Block;
import cz.jzitnik.tui.ScreenRenderer;

public class MobMineHandler {
    @MineEventHandler
    public void handle(ScreenRenderer screenRenderer, Game game, int x, int y) {
        var world = game.getWorld();

        var mobs = world[y - 1][x].stream().filter(block -> block.isMob() && !block.getBlockId().equals("steve")).toList();

        for (Block mob : mobs) {
            if (mob.getClass().isAnnotationPresent(CustomMobFallingAfterMineLogic.class)) {
                var annotation = mob.getClass().getAnnotation(CustomMobFallingAfterMineLogic.class);
                var handlerClass = annotation.value();
                try {
                    var instance = handlerClass.getDeclaredConstructor().newInstance();

                    instance.handle(game, x, y);
                    continue;
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }

            world[y - 1][x].remove(mob);
            world[y][x].add(mob);
        }
    }
}
