package cz.jzitnik.game.smelting;

import cz.jzitnik.game.annotations.Fuel;
import cz.jzitnik.game.annotations.ItemRegistry;
import cz.jzitnik.game.annotations.Smeltable;
import cz.jzitnik.game.entities.items.Item;
import cz.jzitnik.game.entities.items.ItemBlockSupplier;

import java.util.HashMap;
import java.util.Set;
import java.util.function.Supplier;

import org.reflections.Reflections;

public class Smelting {
    public final HashMap<String, Supplier<Item>> smeltingList = new HashMap<>();
    public final HashMap<String, Double> fuelList = new HashMap<>();

    public Smelting() {
        register();
    }

    private void register() {
        Reflections reflections = new Reflections("cz.jzitnik.game.entities.items.registry.items");
        Set<Class<?>> handlerClasses = reflections
                .getTypesAnnotatedWith(ItemRegistry.class);

        for (Class<?> clazz : handlerClasses) {
            var itemAnnotation = clazz.getAnnotation(ItemRegistry.class);
            String id = itemAnnotation.value();
            if (clazz.isAnnotationPresent(Fuel.class)) {
                var fuelAnnotation = clazz.getAnnotation(Fuel.class);
                fuelList.put(id, fuelAnnotation.value());
            }
            if (clazz.isAnnotationPresent(Smeltable.class)) {
                var smeltableAnnotation = clazz.getAnnotation(Smeltable.class);
                smeltingList.put(id, () -> ItemBlockSupplier.getItem(smeltableAnnotation.value()));
            }
        }
    }
}
