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 sm) {
        this.location = sm;
    }
    
    @Override
    public void load(final ResourceManager zb) throws IOException {
        final TextureImage a3 = this.getTextureImage(zb);
        a3.throwIfError();
        final TextureMetadataSection ebs6 = a3.getTextureMetadata();
        boolean boolean4;
        boolean boolean5;
        if (ebs6 != null) {
            boolean4 = ebs6.isBlur();
            boolean5 = ebs6.isClamp();
        }
        else {
            boolean4 = false;
            boolean5 = false;
        }
        final NativeImage cxr7 = a3.getImage();
        if (!RenderSystem.isOnRenderThreadOrInit()) {
            RenderSystem.recordRenderCall(() -> this.doLoad(cxr7, boolean4, boolean5));
        }
        else {
            this.doLoad(cxr7, boolean4, boolean5);
        }
    }
    
    private void doLoad(final NativeImage cxr, final boolean boolean2, final boolean boolean3) {
        TextureUtil.prepareImage(this.getId(), 0, cxr.getWidth(), cxr.getHeight());
        cxr.upload(0, 0, 0, 0, 0, cxr.getWidth(), cxr.getHeight(), boolean2, boolean3, false, true);
    }
    
    protected TextureImage getTextureImage(final ResourceManager zb) {
        return TextureImage.load(zb, 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 ebs, final NativeImage cxr) {
            this.exception = null;
            this.metadata = ebs;
            this.image = cxr;
        }
        
        public static TextureImage load(final ResourceManager zb, final ResourceLocation sm) {
            try (final Resource za3 = zb.getResource(sm)) {
                final NativeImage cxr5 = NativeImage.read(za3.getInputStream());
                TextureMetadataSection ebs6 = null;
                try {
                    ebs6 = za3.<TextureMetadataSection>getMetadata((MetadataSectionSerializer<TextureMetadataSection>)TextureMetadataSection.SERIALIZER);
                }
                catch (RuntimeException runtimeException7) {
                    SimpleTexture.LOGGER.warn("Failed reading metadata of: {}", sm, runtimeException7);
                }
                return new TextureImage(ebs6, cxr5);
            }
            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;
            }
        }
    }
}
