package com.mojang.blaze3d.vertex;

import com.mojang.math.Matrix4f;
import com.mojang.datafixers.util.Pair;
import java.nio.ByteBuffer;
import java.util.concurrent.CompletableFuture;
import com.mojang.blaze3d.systems.RenderSystem;

public class VertexBuffer implements AutoCloseable {
    private int id;
    private final VertexFormat format;
    private int vertexCount;
    
    public VertexBuffer(final VertexFormat dhq) {
        this.format = dhq;
        RenderSystem.glGenBuffers(integer -> this.id = integer);
    }
    
    public void bind() {
        RenderSystem.glBindBuffer(34962, () -> this.id);
    }
    
    public void upload(final BufferBuilder dhg) {
        if (!RenderSystem.isOnRenderThread()) {
            RenderSystem.recordRenderCall(() -> this.upload_(dhg));
        }
        else {
            this.upload_(dhg);
        }
    }
    
    public CompletableFuture<Void> uploadLater(final BufferBuilder dhg) {
        if (!RenderSystem.isOnRenderThread()) {
            return CompletableFuture.runAsync(() -> this.upload_(dhg), runnable -> RenderSystem.recordRenderCall(runnable::run));
        }
        this.upload_(dhg);
        return CompletableFuture.<Void>completedFuture((Void)null);
    }
    
    private void upload_(final BufferBuilder dhg) {
        final Pair<BufferBuilder.DrawState, ByteBuffer> pair3 = dhg.popNextBuffer();
        if (this.id == -1) {
            return;
        }
        final ByteBuffer byteBuffer4 = (ByteBuffer)pair3.getSecond();
        this.vertexCount = byteBuffer4.remaining() / this.format.getVertexSize();
        this.bind();
        RenderSystem.glBufferData(34962, byteBuffer4, 35044);
        unbind();
    }
    
    public void draw(final Matrix4f b, final int integer) {
        RenderSystem.pushMatrix();
        RenderSystem.loadIdentity();
        RenderSystem.multMatrix(b);
        RenderSystem.drawArrays(integer, 0, this.vertexCount);
        RenderSystem.popMatrix();
    }
    
    public static void unbind() {
        RenderSystem.glBindBuffer(34962, () -> 0);
    }
    
    @Override
    public void close() {
        if (this.id >= 0) {
            RenderSystem.glDeleteBuffers(this.id);
            this.id = -1;
        }
    }
}
