package net.minecraft.world.level.storage.loot;

import org.apache.logging.log4j.LogManager;
import java.util.Set;
import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.server.packs.resources.ResourceManager;
import com.google.gson.JsonElement;
import com.google.common.collect.ImmutableMap;
import net.minecraft.resources.ResourceLocation;
import java.util.Map;
import com.google.gson.Gson;
import org.apache.logging.log4j.Logger;
import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener;

public class LootTables extends SimpleJsonResourceReloadListener {
    private static final Logger LOGGER;
    private static final Gson GSON;
    private Map<ResourceLocation, LootTable> tables;
    private final PredicateManager predicateManager;
    
    public LootTables(final PredicateManager day) {
        super(LootTables.GSON, "loot_tables");
        this.tables = ImmutableMap.of();
        this.predicateManager = day;
    }
    
    public LootTable get(final ResourceLocation uh) {
        return this.tables.getOrDefault(uh, LootTable.EMPTY);
    }
    
    @Override
    protected void apply(final Map<ResourceLocation, JsonElement> map, final ResourceManager abc, final ProfilerFiller ami) {
        final ImmutableMap.Builder<ResourceLocation, LootTable> builder5 = ImmutableMap.<ResourceLocation, LootTable>builder();
        final JsonElement jsonElement2 = map.remove(BuiltInLootTables.EMPTY);
        if (jsonElement2 != null) {
            LootTables.LOGGER.warn("Datapack tried to redefine {} loot table, ignoring", BuiltInLootTables.EMPTY);
        }
        LootTable daw2;
        final ImmutableMap.Builder<ResourceLocation, LootTable> builder6;
        map.forEach((uh, jsonElement) -> {
            try {
                daw2 = LootTables.GSON.<LootTable>fromJson(jsonElement, LootTable.class);
                builder6.put(uh, daw2);
            }
            catch (Exception exception4) {
                LootTables.LOGGER.error("Couldn't parse loot table {}", uh, exception4);
            }
            return;
        });
        builder5.put(BuiltInLootTables.EMPTY, LootTable.EMPTY);
        final ImmutableMap<ResourceLocation, LootTable> immutableMap7 = builder5.build();
        final ValidationContext dbe8 = new ValidationContext(LootContextParamSets.ALL_PARAMS, this.predicateManager::get, immutableMap7::get);
        immutableMap7.forEach((uh, daw) -> validate(dbe8, uh, daw));
        dbe8.getProblems().forEach((string1, string2) -> LootTables.LOGGER.warn("Found validation problem in " + string1 + ": " + string2));
        this.tables = immutableMap7;
    }
    
    public static void validate(final ValidationContext dbe, final ResourceLocation uh, final LootTable daw) {
        daw.validate(dbe.setParams(daw.getParamSet()).enterTable("{" + uh + "}", uh));
    }
    
    public static JsonElement serialize(final LootTable daw) {
        return LootTables.GSON.toJsonTree(daw);
    }
    
    public Set<ResourceLocation> getIds() {
        return this.tables.keySet();
    }
    
    static {
        LOGGER = LogManager.getLogger();
        GSON = Deserializers.createLootTableSerializer().create();
    }
}
