package common.packet;

import java.io.IOException;

import common.network.IPlayer;
import common.network.Packet;
import common.network.PacketBuffer;

public class CPacketSkin implements Packet<IPlayer> {
	private byte[] texture;
	private String character;

	public CPacketSkin() {
	}

	public CPacketSkin(byte[] texture, String character) {
		this.texture = texture;
		this.character = character;
	}

	public byte[] getCompressed() {
		return this.texture;
	}

	public String getCharacter() {
		return this.character;
	}

	public void readPacketData(PacketBuffer buf) throws IOException {
		if(buf.readBoolean()) {
			this.texture = null;
			this.character = buf.readString(64);
		}
		else {
			this.texture = buf.readByteArray();
			this.character = null;
			if(this.texture.length == 0 || this.texture.length > IPlayer.MAX_SKIN_SIZE)
				this.texture = new byte[IPlayer.MAX_SKIN_SIZE];
		}
	}

	public void writePacketData(PacketBuffer buf) throws IOException {
		buf.writeBoolean(this.texture == null);
		if(this.texture == null)
			buf.writeString(this.character);
		else
			buf.writeByteArray(this.texture);
	}

	public void processPacket(IPlayer handler) {
		handler.processSkin(this);
	}
}
