package net.minecraft.client.resources;

import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.renderer.texture.TextureManager;
import java.io.InputStream;
import java.io.IOException;
import net.minecraft.server.packs.repository.PackCompatibility;
import net.minecraft.network.chat.Component;
import net.minecraft.server.packs.repository.PackSource;
import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
import net.minecraft.server.packs.PackResources;
import java.util.function.Supplier;
import net.minecraft.resources.ResourceLocation;
import javax.annotation.Nullable;
import com.mojang.blaze3d.platform.NativeImage;
import net.minecraft.server.packs.repository.Pack;

public class ResourcePack extends Pack {
    @Nullable
    private NativeImage icon;
    @Nullable
    private ResourceLocation iconLocation;
    
    public ResourcePack(final String string, final boolean boolean2, final Supplier<PackResources> supplier, final PackResources aae, final PackMetadataSection aaj, final Position b, final PackSource aas) {
        super(string, boolean2, supplier, aae, aaj, b, aas);
        this.icon = readIcon(aae);
    }
    
    public ResourcePack(final String string, final boolean boolean2, final Supplier<PackResources> supplier, final Component mr4, final Component mr5, final PackCompatibility aaq, final Position b, final boolean boolean8, final PackSource aas, @Nullable final NativeImage dgs) {
        super(string, boolean2, supplier, mr4, mr5, aaq, b, boolean8, aas);
        this.icon = dgs;
    }
    
    @Nullable
    public static NativeImage readIcon(final PackResources aae) {
        try (final InputStream inputStream2 = aae.getRootResource("pack.png")) {
            return NativeImage.read(inputStream2);
        }
        catch (IOException | IllegalArgumentException ex) {
            return null;
        }
    }
    
    public void bindIcon(final TextureManager elw) {
        if (this.iconLocation == null) {
            if (this.icon == null) {
                this.iconLocation = new ResourceLocation("textures/misc/unknown_pack.png");
            }
            else {
                this.iconLocation = elw.register("texturepackicon", new DynamicTexture(this.icon));
            }
        }
        elw.bind(this.iconLocation);
    }
    
    @Override
    public void close() {
        super.close();
        if (this.icon != null) {
            this.icon.close();
            this.icon = null;
        }
    }
}
