package speiger.src.coreengine.utils.io.dataTag.array;

import java.io.IOException;
import java.util.Arrays;

import speiger.src.coreengine.utils.io.dataTag.DataTag;
import speiger.src.coreengine.utils.io.dataTag.compression.DataTagLimiter;
import speiger.src.coreengine.utils.io.dataTag.compression.TagCompressor;
import speiger.src.coreengine.utils.io.dataTag.compression.TagDecompressor;
import speiger.src.coreengine.utils.io.streams.ExtendedDataInput;
import speiger.src.coreengine.utils.io.streams.ExtendedDataOutput;

public class DoubleArrayTag implements DataTag
{
	double[] value;
	
	public DoubleArrayTag()
	{
		this(new double[0]);
	}
	
	public DoubleArrayTag(double[] data)
	{
		value = data;
	}
	
	@Override
	public int getId()
	{
		return 15;
	}
	
	@Override
	public String getTypeName()
	{
		return "Double Array";
	}
	
	@Override
	public long countBytes(long input)
	{
		return input + 4L + (value.length * 8L);
	}
	
	@Override
	public void write(ExtendedDataOutput output, TagCompressor compressor) throws IOException
	{
		output.writeVarInt(value.length);
		for(int i = 0,m=value.length;i<m;i++)
		{
			output.writeDouble(value[i]);
		}
	}
	
	@Override
	public void read(ExtendedDataInput input, TagDecompressor decompressor, int depthLimit, DataTagLimiter limiter) throws IOException
	{
		int size = input.readVarInt();
		limiter.countBytes((size * 8) + 4);
		value = new double[size];
		for(int i = 0;i<size;i++)
		{
			value[i] = input.readDouble();
		}
	}
	
	@Override
	public DoubleArrayTag copy()
	{
		return new DoubleArrayTag(Arrays.copyOf(value, value.length));
	}
	
	public double[] get()
	{
		return value;
	}
	
	@Override
	public int hashCode()
	{
		return Arrays.hashCode(value);
	}
	
	@Override
	public boolean equals(Object obj)
	{
		return obj instanceof DoubleArrayTag && Arrays.equals(((DoubleArrayTag)obj).value, value);
	}
	
	@Override
	public String toString()
	{
		StringBuilder stringbuilder = new StringBuilder("[D;");
		for(int i = 0, m = value.length;i < m;++i)
		{
			if(i != 0)
			{
				stringbuilder.append(',');
			}
			stringbuilder.append(value[i]).append('d');
		}
		return stringbuilder.append(']').toString();
	}
	
	@Override
	public String prettyPrint(String indent, int depth)
	{
		StringBuilder builder = new StringBuilder("[").append(TYPE_HIGHLIGHT).append("D").append(RESET_HIGHLIGHT).append(";");
		for(int i = 0, m = value.length;i < m;++i)
		{
			if(i != 0)
			{
				builder.append(',');
			}
			builder.append(NUMBER_HIGHLIGHT).append(value[i]).append(TYPE_HIGHLIGHT).append('d').append(RESET_HIGHLIGHT);
		}
		return builder.append(']').toString();
	}
}
