package common.tags;

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

import common.util.Util;

class TagStringArray extends Tag {
	private String[] data;

	TagStringArray() {
	}

	public TagStringArray(String[] data) {
		this.data = data;
	}

	void write(DataOutput output) throws IOException {
		output.writeInt(this.data.length);
		for(int z = 0; z < this.data.length; z++) {
			output.writeUTF(this.data[z]);
		}
	}

	void read(DataInput input, int depth, SizeTracker tracker) throws IOException {
		tracker.read(24);
		int len = input.readInt();
		this.data = new String[len];
		for(int z = 0; z < len; z++) {
			this.data[z] = input.readUTF();
			tracker.read(2 * this.data[z].length());
		}
	}

	TagType getType() {
		return TagType.STRING_ARRAY;
	}

	public String toString() {
		StringBuilder sb = new StringBuilder("[");
		for(int z = 0; z < this.data.length; z++) {
			if(z != 0)
				sb.append(',');
			sb.append("\"" + this.data[z].replace("\"", "\\\"") + "\"");
		}
		return sb.append("]").toString();
	}

	String toString(int indent, int depth) {
		StringBuilder sb = new StringBuilder("[");
		String prefix = Util.repeatString(' ', indent * (depth + 1));
		for(int z = 0; z < this.data.length; z++) {
			if(z != 0)
				sb.append(',');
			sb.append('\n').append(prefix).append("\"" + this.data[z].replace("\"", "\\\"") + "\"");
		}
		return sb.append('\n').append(Util.repeatString(' ', indent * depth)).append("]").toString();
	}

	public Tag copy() {
		String[] data = new String[this.data.length];
		System.arraycopy(this.data, 0, data, 0, this.data.length);
		return new TagStringArray(data);
	}

	public boolean equals(Object other) {
		return super.equals(other) && Arrays.equals(this.data, ((TagStringArray)other).data);
	}

	public int hashCode() {
		return super.hashCode() ^ Arrays.hashCode(this.data);
	}

	public String[] getStringArray() {
		return this.data;
	}
}
