package oldPackets;

import cz.vutbr.feec.packets.PacketGenerateException;
import cz.vutbr.feec.packets.PacketParseException;
import cz.vutbr.feec.packets.PacketUtils;
import cz.vutbr.feec.packets.ttp.GeneralTTPHeader;

import org.apache.commons.lang.builder.ToStringBuilder;

public class LMVPacket extends GeneralTTPHeader {

	private long LMSseqNumber;
	private long LMSidVector;

	public LMVPacket() {
		super(4);
	}
	/**
	 * Generate buffer of values in LMV header. <i>This method generates 
	 * specific size of bytes into the buffer.</i>
	 * @param resultArray  - memory bytes for generating buffer
	 * @param startPos - starting position in resultArray
	 */
	@Override
	public int generate(byte[] resultArray, int startPos)
			throws PacketGenerateException {

		int length = super.generate(resultArray, startPos);
		length += generateRestLMV(resultArray, startPos + length);

		return length;
	}

	private int generateRestLMV(byte[] buffer, int offset) {

		assert PacketUtils.checkSize(this.getLMSseqNumber(), 32);
		assert PacketUtils.checkSize(this.getLMSidVector(), 32);
	
		//adding LMS sequence number
		PacketUtils.setOctet4(buffer, offset, this.getLMSseqNumber());
		//adding LMS ID vector
		PacketUtils.setOctet4(buffer, offset +4, this.getLMSidVector());

		// zbytek hlavicky zabere celkem 64 bitu
		return 2*4;
	}
	/**
	 * Parsing LMV header values from buffer. <i>This method takes specific 
	 * number of bytes from buffer and parse them into LMV header values. </i>
	 * @param resultArray  - memory bytes for parsing the values
	 * @param startPos - starting position in resultArray
	 * @param length - length of header
	 */
	@Override
	public int parse(byte[] resultArray, int startPos, int length)
			throws PacketParseException {
		int len = super.parse(resultArray, startPos, length);
		len += this.parseRestLMV(resultArray, startPos, length);
		return len;
	}
	
	
	private int parseRestLMV(byte[] resultArray, int startPos, int length) throws PacketParseException{
			this.setLMSseqNumber(PacketUtils.getOctet4(resultArray, 4 + startPos));	
			this.setLMSidVector(PacketUtils.getOctet4(resultArray, 8 + startPos));
		return 4;
	}

	public long getLMSseqNumber() {
		return LMSseqNumber;
	}

	public void setLMSseqNumber(long vseqNumber) {
		LMSseqNumber = vseqNumber;
	}

	public long getLMSidVector() {
		return LMSidVector;
	}

	public void setLMSidVector(long vidVector) {
		LMSidVector = vidVector;
	}
	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return new ToStringBuilder(this).append("version", this.getVersion())
				.append("packetType",
						this.getPacketType()).append("LMSseqNumber",
						this.LMSseqNumber).append("LMSidVector",
						this.LMSidVector).append("length", this.getLength())
				.append("feedbackTreeId", this.getFeedbackTreeId()).toString();
	}
}
