package speiger.src.coreengine.rendering.models;

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

import speiger.src.collections.objects.maps.impl.hash.Object2ObjectOpenHashMap;

public enum DataType
{
	BYTE("byte", 1),
	SHORT("short", 2),
	INT("int", 4),
	LONG("long", 8),
	FLOAT("float", 4),
	DOUBLE("double", 8);
	
	static final Map<String, DataType> BY_ID = new Object2ObjectOpenHashMap<>();
	final String type;
	final int byteSize;
	
	private DataType(String type, int byteSize)
	{
		this.type = type;
		this.byteSize = byteSize;
	}
	
	public String getType()
	{
		return type;
	}
	
	public int getByteSize()
	{
		return byteSize;
	}
	
	public boolean isFloatingPoint()
	{
		return this == FLOAT || this == DOUBLE;
	}
	
	public void putIntoBuffer(ByteBuffer buffer, List<Number> data, int offset, int size)
	{
		for(int i = 0;i<size;putIntoBuffer(buffer, data.get(offset+i)),i++);
	}
	
	public void putIntoBuffer(ByteBuffer buffer, Number data)
	{
		switch(this)
		{
			case BYTE:
				buffer.put(data.byteValue());
				break;
			case SHORT:
				buffer.putShort(data.shortValue());
				break;
			case INT:
				buffer.putInt(data.intValue());
				break;
			case LONG:
				buffer.putLong(data.longValue());
				break;
			case FLOAT:
				buffer.putFloat(data.floatValue());
				break;
			case DOUBLE:
				buffer.putDouble(data.doubleValue());
				break;
		}
	}
	
	public static DataType byID(String id)
	{
		return BY_ID.get(id);
	}
	
	static
	{
		for(DataType type : values())
		{
			BY_ID.put(type.getType(), type);
		}
	}
}
