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 ByteArrayTag implements DataTag
{
	byte[] value;
	
	public ByteArrayTag()
	{
		this(new byte[0]);
	}
	
	public ByteArrayTag(byte[] data)
	{
		value = data;
	}
	
	@Override
	public int getId()
	{
		return 9;
	}
	
	@Override
	public String getTypeName()
	{
		return "Byte Array";
	}
	
	@Override
	public long countBytes(long input)
	{
		return input + 4L + value.length;
	}
	
	@Override
	public void write(ExtendedDataOutput output, TagCompressor compressor) throws IOException
	{
		output.writeBytes(value);
	}
	
	@Override
	public void read(ExtendedDataInput input, TagDecompressor decompressor, int depthLimit, DataTagLimiter limiter) throws IOException
	{
		value = input.readBytes();
		limiter.countBytes((value.length) + 4);
	}
	
	@Override
	public ByteArrayTag copy()
	{
		return new ByteArrayTag(Arrays.copyOf(value, value.length));
	}
	
	public byte[] get()
	{
		return value;
	}
	
	@Override
	public int hashCode()
	{
		return Arrays.hashCode(value);
	}
	
	@Override
	public boolean equals(Object obj)
	{
		return obj instanceof ByteArrayTag && Arrays.equals(((ByteArrayTag)obj).value, value);
	}
	
	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder("[B;");
		for(int i = 0, m = value.length;i < m;++i)
		{
			if(i != 0)
			{
				builder.append(',');
			}
			builder.append(value[i]).append('b');
		}
		return builder.append(']').toString();
	}
	
	@Override
	public String prettyPrint(String indent, int depth)
	{
		StringBuilder builder = new StringBuilder("[").append(TYPE_HIGHLIGHT).append("B").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('b').append(RESET_HIGHLIGHT);
		}
		return builder.append(']').toString();
	}
}
