package oldPackets;

import java.util.Vector;

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;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import com.sun.xml.internal.bind.v2.TODO;

public class LMSPacket extends GeneralTTPHeader {
	
/*	private long LSMseqNumber;					All was removed after group confirmation
	private long sessionSize;
	private long increaseFactor;
	private long reservedEnd;*/
	
	private Vector<LMSLandMarkGroup> groupList = new Vector<LMSLandMarkGroup>();

	public void addGroupBlock(LMSLandMarkGroup item) {
		groupList.add(item);
	}
	
	public LMSLandMarkGroup getGroupBlock(int index) {
		return groupList.get(index);
	}
	public LMSPacket() {
		super(3);
	}
	/**
	 * Generate buffer of values in LMS 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 += generateRestLMS(resultArray, startPos + length);

		return length;
	}

	private int generateRestLMS(byte[] buffer, int offset) throws PacketGenerateException  {

	/*	assert PacketUtils.checkSize(this.getLMSseqNumber(), 32);
		assert PacketUtils.checkSize(this.getSessionSize(), 32);
		assert PacketUtils.checkSize(this.getIncreaseFactor(), 16);
		assert PacketUtils.checkSize(this.getReservedEnd(), 16);*/
	
	/*	//adding LMS sequence number
		PacketUtils.setOctet4(buffer, offset, this.getLMSseqNumber());
		
		//adding session size
		PacketUtils.setOctet4(buffer, offset+4, this.getSessionSize());
		
		//adding increase factor
		PacketUtils.setOctet2(buffer, offset+8, this.getIncreaseFactor());

		//adding ReservedEnd
		PacketUtils.setOctet2(buffer, offset+10, this.getReservedEnd());*/
		
		//adding Vector of IP addresses
		
		int len = 0; 			// was 12 because there we other atributies
		for (LMSLandMarkGroup group : groupList) {
			len += group.generate(buffer, offset+len);
		}
		// zbytek hlavicky zabere ...
		return len;
	}
	/**
	 * Parsing LMS header values from buffer. <i>This method takes specific 
	 * number of bytes from buffer and parse them into LMS 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.parseRestLMS(resultArray, startPos+len, length);
		return len;
	}
	
	
	private int parseRestLMS(byte[] buffer, int offset, int length) throws PacketParseException{
	
		/*	this.setLMSseqNumber(PacketUtils.getOctet4(buffer, 4 + startPos));	
			this.setSessionSize(PacketUtils.getOctet4(buffer, 8 + startPos));
			this.setIncreaseFactor(PacketUtils.getOctet2(buffer, 12 + startPos));
			this.setReservedEnd(PacketUtils.getOctet2(buffer, 14 + startPos));			*/
		
			int len = 0;		// was 12 because there we other atributies
			for (LMSLandMarkGroup group : groupList) {
				LMSLandMarkGroup item = new LMSLandMarkGroup();
				len+= item.parse(buffer, offset, length);
				this.addGroupBlock(item);
			}
		/*	for (int i = 0; i < (length - 4*4)/4; i++) {
				LMSLandMarkGroup item = new LMSLandMarkGroup();
				len += item.parse(buffer, startPos+len, length);
				this.addGroupBlock(item);
			}*/
		return len;
	}
	/*public long getLMSseqNumber() {
		return LSMseqNumber;
	}

	public void setLMSseqNumber(long mseqNumber) {
		LSMseqNumber = mseqNumber;
	}

	public long getSessionSize() {
		return sessionSize;
	}

	public void setSessionSize(long sessionSize) {
		this.sessionSize = sessionSize;
	}

	public long getIncreaseFactor() {
		return increaseFactor;
	}

	public void setIncreaseFactor(long increaseFactor) {
		this.increaseFactor = increaseFactor;
	}

	public long getReservedEnd() {
		return reservedEnd;
	}

	public void setReservedEnd(long reservedEnd) {
		this.reservedEnd = reservedEnd;
	}*/

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


	/**
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder(-693280367, -1611599541).appendSuper(
				super.hashCode()).append(this.groupList).toHashCode();
	}

	/**
	 * @see java.lang.Object#equals(Object)
	 */
	@Override
	public boolean equals(Object object) {
		if (!(object instanceof LMSPacket)) {
			return false;
		}
		LMSPacket rhs = (LMSPacket) object;
		return new EqualsBuilder().appendSuper(super.equals(object)).append(
				this.groupList, rhs.groupList).isEquals();
	}
}
