package cz.jzitnik.game.mobs;

import java.util.HashMap;
import java.util.Set;

import cz.jzitnik.game.annotations.EntityKillHandler;
import org.reflections.Reflections;

public class EntityKill {
    public final HashMap<String, EntityKillInterface> handlerList = new HashMap<>();

    public EntityKill() {
        registerHandlers();
    }

    public EntityKillInterface get(String key) {
        return handlerList.get(key);
    }

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

        for (Class<?> clazz : handlerClasses) {
            if (EntityKillInterface.class.isAssignableFrom(clazz)) {
                try {
                    EntityKillInterface handlerInstance = (EntityKillInterface) clazz.getDeclaredConstructor()
                            .newInstance();
                    EntityKillHandler annotation = clazz.getAnnotation(EntityKillHandler.class);
                    handlerList.put(annotation.value(), handlerInstance);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
