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 LongArrayTag implements DataTag
{
	long[] value;
	
	public LongArrayTag()
	{
		this(new long[0]);
	}
	
	public LongArrayTag(long[] data)
	{
		value = data;
	}
	
	@Override
	public int getId()
	{
		return 13;
	}
	
	@Override
	public String getTypeName()
	{
		return "Long 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.writeLong(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 long[size];
		for(int i = 0;i<size;i++)
		{
			value[i] = input.readLong();
		}
	}
	
	@Override
	public LongArrayTag copy()
	{
		return new LongArrayTag(Arrays.copyOf(value, value.length));
	}
	
	public long[] get()
	{
		return value;
	}
	
	@Override
	public int hashCode()
	{
		return Arrays.hashCode(value);
	}
	
	@Override
	public boolean equals(Object obj)
	{
		return obj instanceof LongArrayTag && Arrays.equals(((LongArrayTag)obj).value, value);
	}
	
	@Override
	public String toString()
	{
		StringBuilder stringbuilder = new StringBuilder("[L;");
		for(int i = 0, m = value.length;i < m;++i)
		{
			if(i != 0)
			{
				stringbuilder.append(',');
			}
			stringbuilder.append(value[i]).append('l');
		}
		return stringbuilder.append(']').toString();
	}
	
	@Override
	public String prettyPrint(String indent, int depth)
	{
		StringBuilder builder = new StringBuilder("[").append(TYPE_HIGHLIGHT).append("L").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('l').append(RESET_HIGHLIGHT);
		}
		return builder.append(']').toString();
	}
}
