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

import java.util.List;

import com.google.gson.JsonObject;

import speiger.src.coreengine.rendering.models.GLDataType;
import speiger.src.coreengine.utils.helpers.JsonUtil;

public class VertexEntry
{
	String name;
	int size;
	GLDataType glType;
	boolean optional;
	
	public VertexEntry(JsonObject obj)
	{
		this(obj.get("type").getAsString(), obj.get("length").getAsInt(), GLDataType.byName(obj.get("glType").getAsString()), JsonUtil.getOrDefault(obj, "optional", false));
	}
	
	public VertexEntry(String name, int size, GLDataType glType)
	{
		this(name, size, glType, false);
	}
	
	public VertexEntry(String name, int size, GLDataType glType, boolean optional)
	{
		this.name = name;
		this.size = size;
		this.glType = glType;
		this.optional = optional;
	}
	
	public String getName()
	{
		return name;
	}
	
	public int getSize()
	{
		return size;
	}
	
	public GLDataType getType()
	{
		return glType;
	}
	
	public boolean isOptional()
	{
		return optional;
	}
	
	public static int caculateByteSize(List<VertexEntry> array, boolean excludeOptional)
	{
		int result = 0;
		for(int i = 0,m=array.size();i<m;i++)
		{
			VertexEntry entry = array.get(i);
			if(entry.isOptional() && excludeOptional) continue;
			result += entry.getSize() * entry.getType().getByteSize();
		}
		return result;
	}
	
	public static int calculateStride(List<VertexEntry> array, boolean excludeOptional)
	{
		return excludeOptional ? calculateNonOptionalStride(array) : caclulateStride(array);
	}
	
	public static int calculateNonOptionalStride(List<VertexEntry> array)
	{
		int size = 0;
		for(int i = 0,m=array.size();i<m;i++)
		{
			VertexEntry entry = array.get(i);
			if(entry.isOptional()) continue;
			size += entry.getSize();
		}
		return size;
	}
	
	public static int caclulateStride(List<VertexEntry> array)
	{
		int size = 0;
		for(int i = 0,m=array.size();i<m;size+=array.get(i++).getSize());
		return size;
	}
}