package net.minecraft.server.packs.resources;

import org.apache.logging.log4j.LogManager;
import java.io.InputStream;
import java.util.Iterator;
import java.io.IOException;
import com.google.gson.JsonParseException;
import net.minecraft.util.GsonHelper;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import com.google.common.collect.Maps;
import net.minecraft.util.profiling.ProfilerFiller;
import com.google.gson.Gson;
import org.apache.logging.log4j.Logger;
import com.google.gson.JsonObject;
import net.minecraft.resources.ResourceLocation;
import java.util.Map;

public abstract class SimpleJsonResourceReloadListener extends SimplePreparableReloadListener<Map<ResourceLocation, JsonObject>> {
    private static final Logger LOGGER;
    private static final int PATH_SUFFIX_LENGTH;
    private final Gson gson;
    private final String directory;
    
    public SimpleJsonResourceReloadListener(final Gson gson, final String string) {
        this.gson = gson;
        this.directory = string;
    }
    
    @Override
    protected Map<ResourceLocation, JsonObject> prepare(final ResourceManager zb, final ProfilerFiller aio) {
        final Map<ResourceLocation, JsonObject> map4 = Maps.newHashMap();
        final int integer5 = this.directory.length() + 1;
        for (final ResourceLocation sm7 : zb.listResources(this.directory, string -> string.endsWith(".json"))) {
            final String string2 = sm7.getPath();
            final ResourceLocation sm8 = new ResourceLocation(sm7.getNamespace(), string2.substring(integer5, string2.length() - SimpleJsonResourceReloadListener.PATH_SUFFIX_LENGTH));
            try (final Resource za10 = zb.getResource(sm7);
                 final InputStream inputStream12 = za10.getInputStream();
                 final Reader reader14 = new BufferedReader(new InputStreamReader(inputStream12, StandardCharsets.UTF_8))) {
                final JsonObject jsonObject16 = GsonHelper.<JsonObject>fromJson(this.gson, reader14, JsonObject.class);
                if (jsonObject16 != null) {
                    final JsonObject jsonObject17 = map4.put(sm8, jsonObject16);
                    if (jsonObject17 != null) {
                        throw new IllegalStateException("Duplicate data file ignored with ID " + sm8);
                    }
                }
                else {
                    SimpleJsonResourceReloadListener.LOGGER.error("Couldn't load data file {} from {} as it's null or empty", sm8, sm7);
                }
            }
            catch (JsonParseException | IllegalArgumentException | IOException ex2) {
                final Exception ex;
                final Exception exception10 = ex;
                SimpleJsonResourceReloadListener.LOGGER.error("Couldn't parse data file {} from {}", sm8, sm7, exception10);
            }
        }
        return map4;
    }
    
    static {
        LOGGER = LogManager.getLogger();
        PATH_SUFFIX_LENGTH = ".json".length();
    }
}
