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 abc, final ResourceLocation uh, final Executor executor) {
        super(uh);
        this.future = CompletableFuture.<TextureImage>supplyAsync(() -> TextureImage.load(abc, uh), executor);
    }
    
    @Override
    protected TextureImage getTextureImage(final ResourceManager abc) {
        if (this.future != null) {
            final TextureImage a3 = this.future.join();
            this.future = null;
            return a3;
        }
        return TextureImage.load(abc, 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 elw, final ResourceManager abc, final ResourceLocation uh, final Executor executor) {
        (this.future = CompletableFuture.<TextureImage>supplyAsync(() -> TextureImage.load(abc, this.location), Util.backgroundExecutor())).thenRunAsync(() -> elw.register(this.location, this), executor(executor));
    }
    
    private static Executor executor(final Executor executor) {
        return runnable -> executor.execute(() -> RenderSystem.recordRenderCall(runnable::run));
    }
}
