package speiger.src.coreengine.rendering.models;

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

public class DrawCall
{
	final int glType;
	final int textureId;
	final byte[] data;
	final int vertexCount;
	
	public DrawCall(int glType, int textureId, float[] data, int vertexCount)
	{
		this.glType = glType;
		this.textureId = textureId;
		this.vertexCount = vertexCount;
		this.data = new byte[data.length*4];
		ByteBuffer.wrap(this.data).order(ByteOrder.nativeOrder()).asFloatBuffer().put(data);
	}
	
	public DrawCall(int glType, int textureId, byte[] data, int vertexCount)
	{
		this.glType = glType;
		this.textureId = textureId;
		this.data = data;
		this.vertexCount = vertexCount;
	}
	
	public byte[] getData()
	{
		return data;
	}
	
	public int getGLType()
	{
		return glType;
	}
	
	public int getTextureId()
	{
		return textureId;
	}
	
	public int getVertexCount()
	{
		return vertexCount;
	}
	
	public static byte[] merge(List<DrawCall> drawCalls)
	{
		int total = 0;
		for(int i = 0,m=drawCalls.size();i<m;i++)
		{
			total += drawCalls.get(i).getData().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.getData().length;
			System.arraycopy(call.getData(), 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].getData().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.getData().length;
			System.arraycopy(call.getData(), 0, totalData, total, length);
			total += length;
		}
		return totalData;
	}
}
