package net.thousandparsec.netlib.tp03;

import net.thousandparsec.netlib.*;

import java.io.IOException;

/**
 * Player Data
 */
public class Player extends Response
{
	public static final int FRAME_TYPE=40;

	protected Player(int id)
	{
		super(id);
	}

	public Player()
	{
		super(FRAME_TYPE);
	}

	private int id;

	public int getId()
	{
		return this.id;
	}

	public void setId(int value)
	{
		this.id=value;
	}

	/**
	 * The player's name.
	 */
	private String name=new String();

	public String getName()
	{
		return this.name;
	}

	public void setName(String value)
	{
		this.name=value;
	}

	/**
	 * The race of the player.
	 */
	private String race=new String();

	public String getRace()
	{
		return this.race;
	}

	public void setRace(String value)
	{
		this.race=value;
	}

	@Override
	public void visit(TP03Visitor visitor) throws TPException
	{
		visitor.frame(this);
	}

	@Override
	public int findByteLength()
	{
		return super.findByteLength()
			 + 4
			 + findByteLength(this.name)
			 + findByteLength(this.race);
	}

	@Override
	public void write(TPDataOutput out, Connection<?> conn) throws IOException
	{
		super.write(out, conn);
		out.writeInteger(this.id);
		out.writeString(this.name);
		out.writeString(this.race);
	}

	/**
	 * A special "internal" constructor that reads contents from a stream.
	 */
	Player(int id, TPDataInput in) throws IOException
	{
		super(id, in);
		this.id=in.readInteger32();
		this.name=in.readString();
		this.race=in.readString();
	}

	@Override
	public String toString()
	{
		StringBuilder buf=new StringBuilder();
		buf.append("{Player");
		buf.append("; id: ");
		buf.append(String.valueOf(this.id));
		buf.append("; name: ");
		buf.append(String.valueOf(this.name));
		buf.append("; race: ");
		buf.append(String.valueOf(this.race));
		buf.append("; super:").append(super.toString());
		buf.append("}");
		return buf.toString();
	}

}
