package net.minecraft.client.renderer.texture;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.Util;
import java.util.concurrent.Executor;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.ResourceManager;
import javax.annotation.Nullable;
import java.util.concurrent.CompletableFuture;

public class PreloadedTexture extends SimpleTexture {
    @Nullable
    private CompletableFuture<TextureImage> future;
    
    public PreloadedTexture(final ResourceManager zb, final ResourceLocation sm, final Executor executor) {
        super(sm);
        this.future = CompletableFuture.<TextureImage>supplyAsync(() -> TextureImage.load(zb, sm), executor);
    }
    
    @Override
    protected TextureImage getTextureImage(final ResourceManager zb) {
        if (this.future != null) {
            final TextureImage a3 = this.future.join();
            this.future = null;
            return a3;
        }
        return TextureImage.load(zb, this.location);
    }
    
    public CompletableFuture<Void> getFuture() {
        return (this.future == null) ? CompletableFuture.<Void>completedFuture((Void)null) : this.future.<Void>thenApply(a -> null);
    }
    
    @Override
    public void reset(final TextureManager eak, final ResourceManager zb, final ResourceLocation sm, final Executor executor) {
        (this.future = CompletableFuture.<TextureImage>supplyAsync(() -> TextureImage.load(zb, this.location), Util.backgroundExecutor())).thenRunAsync(() -> eak.register(this.location, this), executor(executor));
    }
    
    private static Executor executor(final Executor executor) {
        return runnable -> executor.execute(() -> RenderSystem.recordRenderCall(runnable::run));
    }
}
