package net.minecraft.server.packs;

import org.apache.logging.log4j.LogManager;
import com.google.gson.JsonObject;
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 javax.annotation.Nullable;
import net.minecraft.server.packs.metadata.MetadataSectionSerializer;
import java.io.IOException;
import java.io.InputStream;
import net.minecraft.resources.ResourceLocation;
import java.io.File;
import org.apache.logging.log4j.Logger;

public abstract class AbstractResourcePack implements Pack {
    private static final Logger LOGGER;
    protected final File file;
    
    public AbstractResourcePack(final File file) {
        this.file = file;
    }
    
    private static String getPathFromLocation(final PackType yf, final ResourceLocation sm) {
        return String.format("%s/%s/%s", yf.getDirectory(), sm.getNamespace(), sm.getPath());
    }
    
    protected static String getRelativePath(final File file1, final File file2) {
        return file1.toURI().relativize(file2.toURI()).getPath();
    }
    
    @Override
    public InputStream getResource(final PackType yf, final ResourceLocation sm) throws IOException {
        return this.getResource(getPathFromLocation(yf, sm));
    }
    
    @Override
    public boolean hasResource(final PackType yf, final ResourceLocation sm) {
        return this.hasResource(getPathFromLocation(yf, sm));
    }
    
    protected abstract InputStream getResource(final String string) throws IOException;
    
    @Override
    public InputStream getRootResource(final String string) throws IOException {
        if (string.contains("/") || string.contains("\\")) {
            throw new IllegalArgumentException("Root resources can only be filenames, not paths (no / allowed!)");
        }
        return this.getResource(string);
    }
    
    protected abstract boolean hasResource(final String string);
    
    protected void logWarning(final String string) {
        AbstractResourcePack.LOGGER.warn("ResourcePack: ignored non-lowercase namespace: {} in {}", string, this.file);
    }
    
    @Nullable
    @Override
    public <T> T getMetadataSection(final MetadataSectionSerializer<T> yi) throws IOException {
        try (final InputStream inputStream3 = this.getResource("pack.mcmeta")) {
            return AbstractResourcePack.<T>getMetadataFromStream(yi, inputStream3);
        }
    }
    
    @Nullable
    public static <T> T getMetadataFromStream(final MetadataSectionSerializer<T> yi, final InputStream inputStream) {
        JsonObject jsonObject3;
        try (final BufferedReader bufferedReader4 = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
            jsonObject3 = GsonHelper.parse(bufferedReader4);
        }
        catch (IOException | JsonParseException ex2) {
            final Exception ex;
            final Exception exception4 = ex;
            AbstractResourcePack.LOGGER.error("Couldn't load {} metadata", yi.getMetadataSectionName(), exception4);
            return null;
        }
        if (!jsonObject3.has(yi.getMetadataSectionName())) {
            return null;
        }
        try {
            return yi.fromJson(GsonHelper.getAsJsonObject(jsonObject3, yi.getMetadataSectionName()));
        }
        catch (JsonParseException jsonParseException4) {
            AbstractResourcePack.LOGGER.error("Couldn't load {} metadata", yi.getMetadataSectionName(), jsonParseException4);
            return null;
        }
    }
    
    @Override
    public String getName() {
        return this.file.getName();
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
