package common.tileentity;

import common.entity.npc.EntityNPC;
import common.network.Packet;
import common.packet.SPacketUpdateSign;
import common.tags.TagObject;

public class TileEntitySign extends TileEntity {
	public final String[] signText = new String[] {"", "", "", ""};

	/**
	 * The index of the line currently being edited. Only used on client side, but
	 * defined on both. Note this is only really used when the > < are going to be
	 * visible.
	 */
//	public int lineBeingEdited = -1;
//	public String command;
//	private boolean isEditable = true;
	private EntityNPC player;
//	private boolean oldFormat;

	public void writeTags(TagObject compound) {
		super.writeTags(compound);

		for(int i = 0; i < 4; ++i) {
			compound.setString("Text" + (i + 1), this.signText[i]);
		}

//		if(this.command != null)
//			compound.setString("Command", this.command);
	}

	public void readTags(TagObject compound) {
//		this.isEditable = false;
		super.readTags(compound);

		for(int i = 0; i < 4; ++i) {
			this.signText[i] = compound.getString("Text" + (i + 1));
		}
		
//		if(compound.hasString("Command"))
//			this.command = compound.getString("Command");

//		if(!compound.getBoolean("NewFormat")) {
//			Log.debug("Konvertiere Schild bei " + this.pos.getX() + "," + this.pos.getY() + "," + this.pos.getZ() + " ...");
//			byte newComp = 0;
//			byte quotes = 0;
//			String[] old = new String[4];
//			for(int i = 0; i < 4; ++i) {
//				old[i] = this.signText[i];
////				if(ChatFormat.hasLegacy(this.signText[i])) {
//				this.signText[i] = ChatFormat.replaceLegacy(this.signText[i]);
////				}
//				if(this.signText[i].startsWith("{") && this.signText[i].endsWith("}")) {
//					try {
//						TextComponent comp = TextSerializer.toComponent(this.signText[i]);
//						this.signText[i] = comp.getFormattedText();
//						newComp++;
//					}
//					catch(Throwable e) {
//					}
//				}
//				else if(this.signText[i].startsWith("\"") && this.signText[i].endsWith("\"")) {
//					newComp++;
//					quotes |= 1 << i;
//				}
//			}
//			for(int i = 0; i < 4; ++i) {
//				if(newComp == 4 && (quotes & (1 << i)) != 0) {
//					this.signText[i] = this.signText[i].substring(1, this.signText[i].length() - 1);
//				}
//				if(old[i] != this.signText[i]) {
//					Log.debug("Zeile " + (i + 1) + ": '" + ChatFormat.stripCodes(this.signText[i]) + "'");
//				}
//			}
//			this.oldFormat = true;
//		}
	}

//	public boolean validate() {
//		super.validate();
//		if(this.oldFormat) {
//			this.oldFormat = false;
//			return true;
//		}
//		return false;
//	}

	/**
	 * Allows for a specialized description packet to be created. This is often used
	 * to sync tile entity data from the server to the client easily. For example
	 * this is used by signs to synchronise the text to be displayed.
	 */
	public Packet getDescriptionPacket() {
		String[] aichatcomponent = new String[4];
		System.arraycopy(this.signText, 0, aichatcomponent, 0, 4);
//		Sign sign = Server.getServer().getSigns().getEntry(new WorldPos(this).toString());
		return new SPacketUpdateSign(this.worldObj, this.pos, aichatcomponent);
	}

//	public boolean hasSpecialNBT() {
//		return true;
//	}

//	public boolean getIsEditable() {
//		return this.isEditable;
//	}
//
//	/**
//	 * Sets the sign's isEditable flag to the specified parameter.
//	 */
//	public void setEditable(boolean isEditableIn) {
//		this.isEditable = isEditableIn;
//
//		if(!isEditableIn) {
//			this.player = null;
//		}
//	}

	public void setPlayer(EntityNPC playerIn) {
		this.player = playerIn;
	}

	public EntityNPC getPlayer() {
		return this.player;
	}
    
    public int getColor() {
    	return 0x00ffff;
    }
}
