package speiger.src.coreengine.rendering.models.loader;

import java.util.List;

import speiger.src.collections.bytes.lists.ByteArrayList;
import speiger.src.collections.bytes.lists.ByteList;
import speiger.src.collections.ints.lists.IntArrayList;
import speiger.src.collections.ints.lists.IntList;

public class MergedModelData extends SimpleModelData
{
	int[] indexes;
	
	protected MergedModelData(String name, byte[] data, int[] indecies, int[] indexes)
	{
		super(name, null, data, indecies);
		this.indexes = indexes;
	}
	
	public int[] getVertexOffsets()
	{
		return indexes;
	}
	
	public int getVertexCount(int index)
	{
		return indexes[index];
	}
	
	public static MergedModelData merge(String newName, List<SimpleModelData> data)
	{
		return merge(newName, 28, data);
	}
	
	public static MergedModelData merge(String newName, int vertexBytes, List<SimpleModelData> data)
	{
		ByteList byteData = new ByteArrayList();
		IntList indeciesData = new IntArrayList();
		int[] offsets = new int[data.size() * 2];
		int offset = 0;
		int indeciesOffset = 0;
		for(int i = 0,m=data.size();i<m;i++)
		{
			offset = byteData.size() / vertexBytes;
			byteData.addAll(data.get(i).getModelData());
			int[] indexes = data.get(i).getIndecies();
			offsets[(i * 2) + 1] = indeciesOffset * 4;
			offsets[i * 2] = indexes.length;
			indeciesOffset += indexes.length;
			for(int x = 0,n=indexes.length;x<n;x++)
			{
				indeciesData.add(indexes[x] + offset);
			}
		}
		return new MergedModelData(newName, byteData.toByteArray(), indeciesData.toIntArray(), offsets);
	}
}
