package net.minecraft.client.renderer.texture;

import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.metadata.MetadataSectionSerializer;
import javax.annotation.Nullable;
import java.io.Closeable;
import org.apache.logging.log4j.LogManager;
import com.mojang.blaze3d.platform.TextureUtil;
import java.io.IOException;
import com.mojang.blaze3d.platform.NativeImage;
import net.minecraft.client.resources.metadata.texture.TextureMetadataSection;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.resources.ResourceLocation;
import org.apache.logging.log4j.Logger;

public class SimpleTexture extends AbstractTexture {
    private static final Logger LOGGER;
    protected final ResourceLocation location;
    
    public SimpleTexture(final ResourceLocation uh) {
        this.location = uh;
    }
    
    @Override
    public void load(final ResourceManager abc) throws IOException {
        final TextureImage a3 = this.getTextureImage(abc);
        a3.throwIfError();
        final TextureMetadataSection ene6 = a3.getTextureMetadata();
        boolean boolean4;
        boolean boolean5;
        if (ene6 != null) {
            boolean4 = ene6.isBlur();
            boolean5 = ene6.isClamp();
        }
        else {
            boolean4 = false;
            boolean5 = false;
        }
        final NativeImage dgs7 = a3.getImage();
        if (!RenderSystem.isOnRenderThreadOrInit()) {
            RenderSystem.recordRenderCall(() -> this.doLoad(dgs7, boolean4, boolean5));
        }
        else {
            this.doLoad(dgs7, boolean4, boolean5);
        }
    }
    
    private void doLoad(final NativeImage dgs, final boolean boolean2, final boolean boolean3) {
        TextureUtil.prepareImage(this.getId(), 0, dgs.getWidth(), dgs.getHeight());
        dgs.upload(0, 0, 0, 0, 0, dgs.getWidth(), dgs.getHeight(), boolean2, boolean3, false, true);
    }
    
    protected TextureImage getTextureImage(final ResourceManager abc) {
        return TextureImage.load(abc, this.location);
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
    
    public static class TextureImage implements Closeable {
        @Nullable
        private final TextureMetadataSection metadata;
        @Nullable
        private final NativeImage image;
        @Nullable
        private final IOException exception;
        
        public TextureImage(final IOException iOException) {
            this.exception = iOException;
            this.metadata = null;
            this.image = null;
        }
        
        public TextureImage(@Nullable final TextureMetadataSection ene, final NativeImage dgs) {
            this.exception = null;
            this.metadata = ene;
            this.image = dgs;
        }
        
        public static TextureImage load(final ResourceManager abc, final ResourceLocation uh) {
            try (final Resource abb3 = abc.getResource(uh)) {
                final NativeImage dgs5 = NativeImage.read(abb3.getInputStream());
                TextureMetadataSection ene6 = null;
                try {
                    ene6 = abb3.<TextureMetadataSection>getMetadata((MetadataSectionSerializer<TextureMetadataSection>)TextureMetadataSection.SERIALIZER);
                }
                catch (RuntimeException runtimeException7) {
                    SimpleTexture.LOGGER.warn("Failed reading metadata of: {}", uh, runtimeException7);
                }
                return new TextureImage(ene6, dgs5);
            }
            catch (IOException iOException3) {
                return new TextureImage(iOException3);
            }
        }
        
        @Nullable
        public TextureMetadataSection getTextureMetadata() {
            return this.metadata;
        }
        
        public NativeImage getImage() throws IOException {
            if (this.exception != null) {
                throw this.exception;
            }
            return this.image;
        }
        
        @Override
        public void close() {
            if (this.image != null) {
                this.image.close();
            }
        }
        
        public void throwIfError() throws IOException {
            if (this.exception != null) {
                throw this.exception;
            }
        }
    }
}
