package net.minecraft.client.renderer.texture;

import org.apache.logging.log4j.LogManager;
import com.mojang.blaze3d.platform.TextureUtil;
import net.minecraft.server.packs.resources.Resource;
import java.util.Iterator;
import java.io.IOException;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.platform.NativeImage;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import com.google.common.collect.Lists;
import java.util.List;
import org.apache.logging.log4j.Logger;

public class LayeredTexture extends AbstractTexture {
    private static final Logger LOGGER;
    public final List<String> layerPaths;
    
    public LayeredTexture(final String... arr) {
        this.layerPaths = Lists.<String>newArrayList(arr);
        if (this.layerPaths.isEmpty()) {
            throw new IllegalStateException("Layered texture with no layers.");
        }
    }
    
    @Override
    public void load(final ResourceManager zb) throws IOException {
        final Iterator<String> iterator3 = this.layerPaths.iterator();
        final String string4 = iterator3.next();
        try (final Resource za5 = zb.getResource(new ResourceLocation(string4))) {
            final NativeImage cxr7 = NativeImage.read(za5.getInputStream());
            while (iterator3.hasNext()) {
                final String string5 = iterator3.next();
                if (string5 == null) {
                    continue;
                }
                try (final Resource za6 = zb.getResource(new ResourceLocation(string5));
                     final NativeImage cxr8 = NativeImage.read(za6.getInputStream())) {
                    for (int integer13 = 0; integer13 < cxr8.getHeight(); ++integer13) {
                        for (int integer14 = 0; integer14 < cxr8.getWidth(); ++integer14) {
                            cxr7.blendPixel(integer14, integer13, cxr8.getPixelRGBA(integer14, integer13));
                        }
                    }
                }
            }
            if (!RenderSystem.isOnRenderThreadOrInit()) {
                RenderSystem.recordRenderCall(() -> this.doLoad(cxr7));
            }
            else {
                this.doLoad(cxr7);
            }
        }
        catch (IOException iOException5) {
            LayeredTexture.LOGGER.error("Couldn't load layered image", (Throwable)iOException5);
        }
    }
    
    private void doLoad(final NativeImage cxr) {
        TextureUtil.prepareImage(this.getId(), cxr.getWidth(), cxr.getHeight());
        cxr.upload(0, 0, 0, true);
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
}
