package net.minecraft.data.info;

import com.google.gson.GsonBuilder;
import java.util.Iterator;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.DefaultedRegistry;
import java.io.IOException;
import java.nio.file.Path;
import com.google.gson.JsonElement;
import net.minecraft.core.WritableRegistry;
import net.minecraft.core.Registry;
import com.google.gson.JsonObject;
import net.minecraft.data.HashCache;
import net.minecraft.data.DataGenerator;
import com.google.gson.Gson;
import net.minecraft.data.DataProvider;

public class RegistryDumpReport implements DataProvider {
    private static final Gson GSON;
    private final DataGenerator generator;
    
    public RegistryDumpReport(final DataGenerator gy) {
        this.generator = gy;
    }
    
    @Override
    public void run(final HashCache ha) throws IOException {
        final JsonObject jsonObject3 = new JsonObject();
        Registry.REGISTRY.keySet().forEach(sm -> jsonObject3.add(sm.toString(), RegistryDumpReport.dumpRegistry(Registry.REGISTRY.get(sm))));
        final Path path4 = this.generator.getOutputFolder().resolve("reports/registries.json");
        DataProvider.save(RegistryDumpReport.GSON, ha, jsonObject3, path4);
    }
    
    private static <T> JsonElement dumpRegistry(final WritableRegistry<T> gh) {
        final JsonObject jsonObject2 = new JsonObject();
        if (gh instanceof DefaultedRegistry) {
            final ResourceLocation sm3 = ((DefaultedRegistry)gh).getDefaultKey();
            jsonObject2.addProperty("default", sm3.toString());
        }
        final int integer3 = Registry.REGISTRY.getId(gh);
        jsonObject2.addProperty("protocol_id", integer3);
        final JsonObject jsonObject3 = new JsonObject();
        for (final ResourceLocation sm4 : gh.keySet()) {
            final T object7 = gh.get(sm4);
            final int integer4 = gh.getId(object7);
            final JsonObject jsonObject4 = new JsonObject();
            jsonObject4.addProperty("protocol_id", integer4);
            jsonObject3.add(sm4.toString(), jsonObject4);
        }
        jsonObject2.add("entries", jsonObject3);
        return jsonObject2;
    }
    
    @Override
    public String getName() {
        return "Registry Dump";
    }
    
    static {
        GSON = new GsonBuilder().setPrettyPrinting().create();
    }
}
