package speiger.src.coreengine.rendering.models;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;

import speiger.src.coreengine.rendering.utils.values.GLMode;

public record DrawCall(GLMode glType, int textureId, byte[] data, int size) {
	
	@Deprecated(forRemoval = true)
	public DrawCall(GLMode glType, int textureId, float[] data, int size) {
		this(glType, textureId, convert(data), size);
	}
	
	private static byte[] convert(float[] data) {
		ByteBuffer buffer = ByteBuffer.allocate(data.length * 4).order(ByteOrder.nativeOrder());
		buffer.asFloatBuffer().put(data);
		return buffer.array();
	}
		
	public static byte[] merge(List<DrawCall> drawCalls) {
		int total = 0;
		for(int i = 0,m = drawCalls.size();i < m;i++) {
			total += drawCalls.get(i).data().length;
		}
		byte[] totalData = new byte[total];
		total = 0;
		for(int i = 0,m = drawCalls.size();i < m;i++) {
			DrawCall call = drawCalls.get(i);
			int length = call.data().length;
			System.arraycopy(call.data(), 0, totalData, total, length);
			total += length;
		}
		return totalData;
	}
	
	public static byte[] merge(DrawCall... drawCalls) {
		int total = 0;
		for(int i = 0,m = drawCalls.length;i < m;i++) {
			total += drawCalls[i].data().length;
		}
		byte[] totalData = new byte[total];
		total = 0;
		for(int i = 0,m = drawCalls.length;i < m;i++) {
			DrawCall call = drawCalls[i];
			int length = call.data().length;
			System.arraycopy(call.data(), 0, totalData, total, length);
			total += length;
		}
		return totalData;
	}
}
