/*
 * This file was automatically generated by sel-utils and
 * released under the MIT License.
 * 
 * License: https://github.com/sel-project/sel-utils/blob/master/LICENSE
 * Repository: https://github.com/sel-project/sel-utils
 * Generated from https://github.com/sel-project/sel-utils/blob/master/xml/protocol/raknet8.xml
 */
package sul.protocol.raknet8.encapsulated;

import java.util.Arrays;

import sul.utils.*;

public class ServerHandshake extends Packet {

	public static final byte ID = (byte)16;

	public static final boolean CLIENTBOUND = true;
	public static final boolean SERVERBOUND = false;

	@Override
	public int getId() {
		return ID;
	}

	public sul.protocol.raknet8.types.Address clientAddress;
	public short mtuLength;
	public sul.protocol.raknet8.types.Address[] systemAddresses = new sul.protocol.raknet8.types.Address[10];
	public long pingId;
	public long serverId;

	public ServerHandshake() {}

	public ServerHandshake(sul.protocol.raknet8.types.Address clientAddress, short mtuLength, sul.protocol.raknet8.types.Address[] systemAddresses, long pingId, long serverId) {
		this.clientAddress = clientAddress;
		this.mtuLength = mtuLength;
		this.systemAddresses = systemAddresses;
		this.pingId = pingId;
		this.serverId = serverId;
	}

	@Override
	public int length() {
		int length=clientAddress.length() + 19; for(sul.protocol.raknet8.types.Address cldvqrcv:systemAddresses){ length+=cldvqrcv.length(); } return length;
	}

	@Override
	public byte[] encode() {
		this._buffer = new byte[this.length()];
		this.writeBigEndianByte(ID);
		this.writeBytes(clientAddress.encode());
		this.writeBigEndianShort(mtuLength);
		for(sul.protocol.raknet8.types.Address cldvqrcv:systemAddresses){ this.writeBytes(cldvqrcv.encode()); }
		this.writeBigEndianLong(pingId);
		this.writeBigEndianLong(serverId);
		return this.getBuffer();
	}

	@Override
	public void decode(byte[] buffer) {
		this._buffer = buffer;
		readBigEndianByte();
		clientAddress=new sul.protocol.raknet8.types.Address(); clientAddress._index=this._index; clientAddress.decode(this._buffer); this._index=clientAddress._index;
		mtuLength=readBigEndianShort();
		final int bncrbfzj=10; systemAddresses=new sul.protocol.raknet8.types.Address[bncrbfzj]; for(int cldvqrcv=0;cldvqrcv<systemAddresses.length;cldvqrcv++){ systemAddresses[cldvqrcv]=new sul.protocol.raknet8.types.Address(); systemAddresses[cldvqrcv]._index=this._index; systemAddresses[cldvqrcv].decode(this._buffer); this._index=systemAddresses[cldvqrcv]._index; }
		pingId=readBigEndianLong();
		serverId=readBigEndianLong();
	}

	public static ServerHandshake fromBuffer(byte[] buffer) {
		ServerHandshake ret = new ServerHandshake();
		ret.decode(buffer);
		return ret;
	}

	@Override
	public String toString() {
		return "ServerHandshake(clientAddress: " + this.clientAddress.toString() + ", mtuLength: " + this.mtuLength + ", systemAddresses: " + Arrays.deepToString(this.systemAddresses) + ", pingId: " + this.pingId + ", serverId: " + this.serverId + ")";
	}

}