package cz.jzitnik.game.context;

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

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

public class GlobalContextProvider {
    private final Map<Class, Object> data = new HashMap<>();
    private boolean loaded = false;

    public Object getMap(Class<?> clazz) {
        if (!loaded) {
            Reflections reflections = new Reflections("cz.jzitnik.game.context");
            Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(GlobalContext.class);

            for (Class<?> claz : handlerClasses) {
                try {
                    Object instance = claz.getDeclaredConstructor().newInstance();
                    data.put(claz, instance);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            loaded = true;
        }

        return data.get(clazz);
    }
}
