package cz.vutbr.feec.packets.ttp;

import java.util.Vector;

import cz.vutbr.feec.packets.IPacket;
import cz.vutbr.feec.packets.PacketGenerateException;
import cz.vutbr.feec.packets.PacketParseException;
import cz.vutbr.feec.packets.PacketUtils;

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

public class FTSGroupBlockLM implements IPacket {
	
	private int SFTST;
	private int lenght;
	private int reserved;
	private int vectorFormat;
	private long feedbackTargerIP;
	
/*	private long groupSize;
	private int padding;
	private int priority;
	private int height;
	private long vectorFormat;*/
	
	
	private Vector<FTS_GB_LMidVectorGroup> groupList = new Vector<FTS_GB_LMidVectorGroup>();

	public void addGroupBlock(FTS_GB_LMidVectorGroup group) {
		groupList.add(group);
	}

	public FTS_GB_LMidVectorGroup getGroupBlock(int index) {

		return groupList.get(index);
	}
	
	public int generate(byte[] buffer, int offset)
			throws PacketGenerateException {
		assert PacketUtils.checkSize(getSFTST(), 8);
		assert PacketUtils.checkSize(getLenght(), 8);
		assert PacketUtils.checkSize(getReserved(), 11);
		assert PacketUtils.checkSize(getVectorFormat(), 5);
		assert PacketUtils.checkSize(getFeedbackTargerIP(), 32);
		
/*		assert PacketUtils.checkSize(getGroupSize(), 16);
		assert PacketUtils.checkSize(getPadding(), 1);
		assert PacketUtils.checkSize(getPriority(), 7);
		assert PacketUtils.checkSize(getHeight(), 3);
		assert PacketUtils.checkSize(getVectorFormat(), 5);*/
		
		PacketUtils.setOctet1(buffer, offset, getSFTST());
		PacketUtils.setOctet1(buffer, offset + 1, getLenght());
		PacketUtils.setOctet2(buffer, offset + 2,
				(((getReserved() << 11) & 0xFFE0) | (getVectorFormat() & 0x001F)));
		PacketUtils.setOctet4(buffer, offset + 4, getFeedbackTargerIP());
		
				
/*		PacketUtils.setOctet2(buffer, offset + 8, getGroupSize());
		
		byte P_Prio = (byte) ((padding << 7) & 0x80 | priority & 0x7F);
		buffer[offset + 10] = P_Prio;
		
		byte Hei_VF = (byte) ((height << 5) & 0xE0 | vectorFormat & 0x1F);
		buffer[offset + 11] = Hei_VF;
	*/		
		
		int len = 8; 			//  12 because there are 12*4 bites on at start of the group
		
		for (FTS_GB_LMidVectorGroup group : groupList) {
			len += group.generate(buffer, offset+len);
		}
		

		return len;
	}

	public int parse(byte[] buffer, int offset, int length)
			throws PacketParseException {
		
		setSFTST(PacketUtils.getOctet1(buffer, offset));
		setLenght(PacketUtils.getOctet1(buffer, 1 + offset));
		long RES_VF = PacketUtils.getOctet2(buffer, 2 + offset);
		setReserved((int)(RES_VF & 0xFFE0) >> 11);
		setVectorFormat((int)(RES_VF & 0x001F));
		setFeedbackTargerIP(PacketUtils.getOctet4(buffer, 4 + offset));
		
/*		setGroupSize(PacketUtils.getOctet2(buffer, 8+ offset));
		
		int P_Prio = PacketUtils.getOctet1(buffer, 10 + offset);
		setPadding((P_Prio & 0x80) >>7);
		setPriority(P_Prio & 0x7F);
		
		int Hei_VF = PacketUtils.getOctet1(buffer, 11 + offset);
		setHeight((Hei_VF & 0xE0) >> 5);
		setVectorFormat((Hei_VF & 0x1F));
	*/	
		int len=8;
/*		for (FTS_GB_LMidVectorGroup group : groupList) {
			FTS_GB_LMidVectorGroup item = new FTS_GB_LMidVectorGroup();
			len+= item.parse(buffer, offset, length);
			this.addGroupBlock(item);
		}
*/		
		
		int restLength = getLenght()-8;
		while (restLength > 0) {
			FTS_GB_LMidVectorGroup item = new FTS_GB_LMidVectorGroup();
			len += item.parse(buffer, offset + len);
			restLength -= 4;
			this.addGroupBlock(item);
		}
		return len;
	}

	
	
	public long getFeedbackTargerIP() {
		return feedbackTargerIP;
	}

	public void setFeedbackTargerIP(long feedbackTargerIP) {
		this.feedbackTargerIP = feedbackTargerIP;
	}



/*	public long getGroupSize() {
		return groupSize;
	}

	public void setGroupSize(long groupSize) {
		this.groupSize = groupSize;
	}
*/


	public int getLenght() {
		return lenght;
	}

	public void setLenght(int lenght) {
		this.lenght = lenght;
	}


/*
	public long getLMidVector() {
		return LMidVector;
	}

	public void setLMidVector(long midVector) {
		LMidVector = midVector;
	}
*/
	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return new ToStringBuilder(this)
				.append("lenght", this.lenght).append(
						"feedbackTargerIP", this.feedbackTargerIP).toString();
	}

	public int getSFTST() {
		return SFTST;
	}

	public void setSFTST(int sftst) {
		SFTST = sftst;
	}

	public int getReserved() {
		return reserved;
	}

	public void setReserved(int reserved) {
		this.reserved = reserved;
	}

	public int getVectorFormat() {
		return vectorFormat;
	}

	public void setVectorFormat(int vectorFormat) {
		this.vectorFormat = vectorFormat;
	}


	
}
