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

import java.util.List;

import com.google.gson.JsonObject;

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

public record VertexEntry(String name, int size, GLDataType type, boolean isOptional) {
	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 type) {
		this(name, size, type, false);
	}
	
	public int byteSize() {
		return size * type.byteSize();
	}
	
	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.byteSize();
		}
		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.size();
		}
		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++).size());
		return size;
	}
}