package speiger.src.coreengine.platform.graphics.opengl.mesh;

import org.jspecify.annotations.Nullable;

import speiger.src.collections.ints.maps.interfaces.Int2ObjectMap;
import speiger.src.coreengine.platform.graphics.api.buffer.VertexBuffer;
import speiger.src.coreengine.platform.graphics.api.buffer.states.IndeciesType;
import speiger.src.coreengine.platform.graphics.api.mesh.DynamicMesh;

public class GLDynamicMesh extends DynamicMesh {
	boolean managed;
	boolean invalid = false;

	public GLDynamicMesh(Int2ObjectMap<VertexBuffer> buffers, @Nullable VertexBuffer indeciesBuffer, @Nullable IndeciesType type, boolean managed) {
		super(buffers, indeciesBuffer, type);
		this.managed = managed;
	}

	@Override
	public boolean isRemoved() { return invalid; }
	
	@Override
	public void remove() {
		if(invalid) return;
		invalid = true;
		if(!managed) return;
		if(indeciesBuffer != null) indeciesBuffer.remove();
		buffers.values().forEach(VertexBuffer::close);
	}

	@Override
	public void closeWithBuffers() {
		boolean wasManaged = managed;
		managed = true;
		close();
		managed = wasManaged;
	}
	
}
