package cz.vutbr.feec.sim.ha;



import cz.vutbr.feec.packets.ttp.FTDPacket;
import cz.vutbr.feec.packets.ttp.FTIPacket;
import cz.vutbr.feec.packets.ttp.FTSPacket;
import cz.vutbr.feec.simlib.net.*;



public class FTHostState implements IHostState {
	
	FTDPacket packetFTD = new FTDPacket();
	FTIPacket packetFTI = new FTIPacket();
	FTSPacket packetFTS = new FTSPacket();
	
	private InetAddress multicastAddr;
	private InetAddress unicastAddr;
	private InetAddress sendAddr;
	private int heighTree;
	private int numberR;
	private long inPort;
	private long inPortOfNextPort;
	private long outPort;
	private long multicastPort;
	private boolean active = false;
	private int ID;
	private int time;
	private state stateOfFT;
	
	enum state {passive, activeFT,LM, exceededGroup, decreasedGroup, FTM, RFT};
	
	
	
	private int countOfFTIsend = 0;
	private int countOfFTDsend = 0;
	private int countOfFTSsend = 0;
	
	private int countOfFTIreceived = 0;
	private int countOfFTDreceived = 0;
	private int countOfFTSreceived = 0;
	
	private boolean statOfSendFTI = false;
	private boolean statOfSendFTD = false;
	private boolean statOfSendFTS = false;
	
	
	public FTHostState(InetAddress unicastAddr) {
		setUnicastAddr(unicastAddr);
	}

	public FTHostState (InetAddress unicastAddr, int act, int heigh,long portInOfNextFT, InetAddress multicastAddr,
			long portOut, int N,InetAddress addrSend, int id, long portIn, long multicastPort, int time, int statOfFT) {
		
		setUnicastAddr(unicastAddr);
		setActive(act);
		setHeightoTree(heigh);
		setPortInOfNextFT(portInOfNextFT);
		setMulticastAddr(multicastAddr);
		setNumofR(N);
		setPortOut(portOut);
		setSendIPAddr(addrSend);
		setID(id);
		setPortIn(portIn);
		setPortMulticast(multicastPort);
		setIntervalToSend(time);
		
	}

	

	@Override
	public int getHeightoTree() {
		return heighTree;
	}



	@Override
	public int getNumofR() {
		
		return numberR;
	}




	@Override
	public void setHeightoTree(int height) {
		this.heighTree = height;
		
	}










	@Override
	public void setNumofR(int N) {
		this.numberR = N;
		
	}




	public InetAddress getUnicastAddr() {
		return unicastAddr;
	}





	public void setUnicastAddr(InetAddress unicastAddr) {
		this.unicastAddr = unicastAddr;
	}





	@Override
	public boolean isActive() {
		
		return active;
	}





	@Override
	public void setActive(int act) {
		if(act == 1)
			this.active = true;
		else
			this.active = false;
		
	}





	@Override
	public InetAddress getSendIPAddr() {
		
		return sendAddr;
	}





	@Override
	public long getPortInOfNextFT() {
		
		return inPortOfNextPort;
	}





	@Override
	public long getPortOut() {
		
		return outPort;
	}





	@Override
	public void setPortInOfNextFT(long port) {
		this.inPortOfNextPort = port;
		
	}





	@Override
	public void setPortOut(long port) {
		this.outPort = port;
		
	}





	@Override
	public InetAddress getMulticastAddr() {
		
		return multicastAddr;
	}





	@Override
	public void setMulticastAddr(InetAddress addr) {
		this.multicastAddr = addr;
		
	}





	@Override
	public void setSendIPAddr(InetAddress addr) {

		this.sendAddr = addr;
		
	}

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

	@Override
	public void setID(int id) {
		this.ID = id;
		
	}

	@Override
	public long getPortIn() {
		
		return inPort;
	}

	@Override
	public void setPortIn(long port) {
		this.inPort = port;
		
	}

	

	@Override
	public void addFTDreceived() {
		this.countOfFTDreceived++;
		
	}

	@Override
	public void addFTDsend() {
		this.countOfFTDsend++;
		
	}

	@Override
	public void addFTIreceived() {
		this.countOfFTIreceived++;
		
	}

	@Override
	public void addFTIsend() {
		this.countOfFTIsend++;
		
	}

	@Override
	public void addFTSreceived() {
		this.countOfFTSreceived++;
		
	}

	@Override
	public void addFTSsend() {
		this.countOfFTSsend++;
		
	}

	@Override
	public void clearNumberofFTDreceived() {
		this.countOfFTDreceived = 0;
		
	}

	@Override
	public void clearNumberofFTDsend() {
		this.countOfFTDsend = 0;		
		
	}

	@Override
	public void clearNumberofFTIreceived() {
		this.countOfFTIreceived = 0;
		
	}

	@Override
	public void clearNumberofFTIsend() {
		this.countOfFTIsend = 0;
		
	}

	@Override
	public void clearNumberofFTSreceived() {
		this.countOfFTSreceived = 0;
		
	}

	@Override
	public void clearNumberofFTSsend() {
		this.countOfFTSsend = 0;
		
	}

	@Override
	public int getNumberOfFTDreceived() {
		
		return countOfFTDreceived;
	}

	@Override
	public int getNumberOfFTIreceived() {
		
		return countOfFTIreceived;
	}

	@Override
	public int getNumberOfFTSreceived() {
		
		return countOfFTSreceived;
	}

	@Override
	public int getNumberOfFTDsent() {
		
		return countOfFTDsend;
	}

	@Override
	public int getNumberOfFTIsent() {
		
		return countOfFTIsend;
	}

	@Override
	public int getNumberOfFTSsent() {
		
		return countOfFTSsend;
	}

	@Override
	public boolean readyToSendFTD() {
		
		return statOfSendFTD;
	}

	@Override
	public boolean readyToSendFTI() {
		
		return statOfSendFTI;
	}

	@Override
	public boolean readyToSendFTS() {
		
		return statOfSendFTS;
	}

	@Override
	public void statOfSendFTD(boolean status) {
		this.statOfSendFTD = status;
		
	}

	@Override
	public void statOfSendFTI(boolean status) {
		this.statOfSendFTI = status;
		
	}

	@Override
	public void statOfSendFTS(boolean status) {
		this.statOfSendFTS = status;
		
	}

	@Override
	public long getPortMulticast() {
		
		return multicastPort;
	}

	@Override
	public void setPortMulticast(long port) {
		this.multicastPort = port;
		
	}

	@Override
	public int getIntervalToSend() {
		
		return time;
	}

	@Override
	public void setIntervalToSend(int time) {
		this.time = time;
		
	}

	@Override
	public int getStatusOfFT() {
		int index = 5;
		if (stateOfFT == state.RFT){
			index = 0;
		}
		if (stateOfFT == state.activeFT){
			index = 1;
		}
		if (stateOfFT == state.LM){
			index = 2;
		}
		if (stateOfFT == state.exceededGroup){
			index = 3;
		}
		if (stateOfFT == state.decreasedGroup){
			index = 4;
		}
		if (stateOfFT == state.passive){
			index = 5;
		}
		if (stateOfFT == state.FTM){
			index = 99;
		}
		return index;
	}

	@Override
	public void setStatusOfFT(int stat) {
		switch (stat) {
		case 0:
			stateOfFT = state.RFT;
			break;
		case 1:
			stateOfFT = state.activeFT;
			break;
		case 2:
			stateOfFT = state.LM;
			break;
		case 3:
			stateOfFT = state.exceededGroup;
			break;
		case 4:
			stateOfFT = state.decreasedGroup;
			break;
		case 5:
			stateOfFT = state.passive;
			break;
		case 99:
			stateOfFT = state.FTM;
			break;


		default:
			break;
		}
			
	}

	@Override
	public FTDPacket getFTDpacket() {
	
		return packetFTD;
	}

	@Override
	public FTIPacket getFTIpacket() {
		
		return packetFTI;
	}

	@Override
	public FTSPacket getFTSpacket() {
		
		return packetFTS;
	}

	@Override
	public void setFTDpacket(int version, int packetType, long fbTreeID,
			int length, long FTport, int height, int status, int reserved,
			long fbGroupSize, long reserved2) {
		
		packetFTD.setVersion(version);
		packetFTD.setPacketType(packetType);
		packetFTD.setFeedbackTreeId(fbTreeID);
		packetFTD.setLength(length);
		packetFTD.setFTPort(FTport);
		packetFTD.setHeight(height);
		packetFTD.setStatus(status);
		packetFTD.setReserved(reserved);
		packetFTD.setFeedbackGroupSize(fbGroupSize);
		packetFTD.setReservedEnd(reserved2);
		
	}

	@Override
	public void setFTIpacket(int version, int packetType, long fbTreeID,
			int length, long FTport, int infoStat, int param) {

		packetFTI.setVersion(version);
		packetFTI.setPacketType(packetType);
		packetFTI.setFeedbackTreeId(fbTreeID);
		packetFTI.setLength(length);
		packetFTI.setFeedbackTargetPort(FTport);
		packetFTI.setInformationStatus(infoStat);
		packetFTI.setParameters(param);
		
	}

	@Override
	public void receiveMessage() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void sendMessage() {
		// TODO Auto-generated method stub
		
	}

}