package common.packet;

import java.io.IOException;

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

public class CPacketMessage implements Packet<IPlayer>
{
	private Type type;
    private String message;

    public CPacketMessage()
    {
    }

    public CPacketMessage(Type type, String str)
    {
    	this.type = type;
        this.message = str.length() > type.length ? str.substring(0, type.length) :str;
    }
    
    public void readPacketData(PacketBuffer buf) throws IOException
    {
    	this.type = buf.readEnumValue(Type.class);
        this.message = buf.readStringFromBuffer(this.type.length);
    }
    
    public void writePacketData(PacketBuffer buf) throws IOException
    {
    	buf.writeEnumValue(this.type);
        buf.writeString(this.message);
    }
    
    public void processPacket(IPlayer handler)
    {
        handler.processMessage(this);
    }

    public Type getType()
    {
        return this.type;
    }

    public String getMessage()
    {
        return this.message;
    }
    
    public static enum Type {
    	COMMAND(IPlayer.MAX_CMD_LENGTH),
    	CHAT(IPlayer.MAX_CMD_LENGTH),
    	DISPLAY(IPlayer.MAX_NICK_LENGTH),
    	INFO(IPlayer.MAX_INFO_LENGTH);
    	// , ITEM(30);
    	
    	private final int length;
    	
    	private Type(int length) {
    		this.length = length;
    	}
    }
}
