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.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 hk) {
        this.generator = hk;
    }
    
    @Override
    public void run(final HashCache hm) throws IOException {
        final JsonObject jsonObject3 = new JsonObject();
        Registry.REGISTRY.keySet().forEach(uh -> jsonObject3.add(uh.toString(), RegistryDumpReport.dumpRegistry((Registry<Object>)Registry.REGISTRY.get(uh))));
        final Path path4 = this.generator.getOutputFolder().resolve("reports/registries.json");
        DataProvider.save(RegistryDumpReport.GSON, hm, jsonObject3, path4);
    }
    
    private static <T> JsonElement dumpRegistry(final Registry<T> gl) {
        final JsonObject jsonObject2 = new JsonObject();
        if (gl instanceof DefaultedRegistry) {
            final ResourceLocation uh3 = ((DefaultedRegistry)gl).getDefaultKey();
            jsonObject2.addProperty("default", uh3.toString());
        }
        final int integer3 = Registry.REGISTRY.getId(gl);
        jsonObject2.addProperty("protocol_id", integer3);
        final JsonObject jsonObject3 = new JsonObject();
        for (final ResourceLocation uh4 : gl.keySet()) {
            final T object7 = gl.get(uh4);
            final int integer4 = gl.getId(object7);
            final JsonObject jsonObject4 = new JsonObject();
            jsonObject4.addProperty("protocol_id", integer4);
            jsonObject3.add(uh4.toString(), jsonObject4);
        }
        jsonObject2.add("entries", jsonObject3);
        return jsonObject2;
    }
    
    @Override
    public String getName() {
        return "Registry Dump";
    }
    
    static {
        GSON = new GsonBuilder().setPrettyPrinting().create();
    }
}
