package cz.jzitnik.game.mobs;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import cz.jzitnik.game.Game;
import cz.jzitnik.game.annotations.EntitySpawn;
import cz.jzitnik.game.entities.Block;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.jline.terminal.Terminal;
import org.reflections.Reflections;

public class EntitySpawnProvider {
    private final List<EntitySpawnInterface> spawnList = new ArrayList<>();

    public void update(Game game, Terminal terminal) {
        int[] playerLocation = game.getPlayerCords();
        int playerX = playerLocation[0];
        int playerY = playerLocation[1];

        for (EntitySpawnInterface entitySpawnInterface: spawnList) {

            entitySpawnInterface.spawn(playerX, playerY, game, terminal);
        }
    }

    public EntitySpawnProvider() {
        registerHandlers();
    }

    private void registerHandlers() {
        Reflections reflections = new Reflections("cz.jzitnik.game.mobs.services");
        Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(EntitySpawn.class);

        for (Class<?> clazz : handlerClasses) {
            if (EntitySpawnInterface.class.isAssignableFrom(clazz)) {
                try {
                    EntitySpawnInterface instance = (EntitySpawnInterface) clazz.getDeclaredConstructor().newInstance();
                    spawnList.add(instance);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}