package cz.jzitnik.game.mobs;

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

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

public class EntityHurtAnimation {
    public final HashMap<String, EntityHurtAnimationChanger> handlerList = new HashMap<>();

    public EntityHurtAnimation() {
        registerHandlers();
    }

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

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

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