package cz.vutbr.feec.sim.ha;

import org.apache.log4j.Logger;




import cz.vutbr.feec.packets.PacketParseException;
import cz.vutbr.feec.packets.ttp.*;
import cz.vutbr.feec.simlib.Status;
import cz.vutbr.feec.simlib.net.DatagramPacket;

import cz.vutbr.feec.simlib.net.NetProc;
import cz.vutbr.feec.simlib.net.Socket;


public class FeedbackInFT extends NetProc {
	private static Logger logger = Logger.getLogger(FeedbackInFT.class
			.getName());
	private Socket sock;
	private FTHostState sessionState;

	public FeedbackInFT(FTHostState sessionState, Socket localSocket) {
		super(localSocket.getAddr());
		setName("FeedbackInFT-Thr");
		this.sessionState = sessionState;
		this.sock = localSocket;
	}
	
	@Override
	public void action() {
		final int MAX_LENGTH = 1500;
		byte[] buffer = new byte[MAX_LENGTH];
		FTIPacket packetFTI = new FTIPacket();
		FTDPacket packetFTD = new FTDPacket();
		FTSPacket packetFTS = new FTSPacket();
		GeneralTTPHeader header = new GeneralTTPHeader();
		DatagramPacket pkt = new DatagramPacket(buffer, MAX_LENGTH);
		
		while (!getSimulation().isFinished()) 
		{
			
			logger.debug("Start receive, Time:" + getSimulation().getTime());
			//System.out.println("Start receive, Time:" + getSimulation().getTime());
			assert getStatus() == Status.ACTIVE;
			int len = sock.receive(this, pkt);
			if (len != -1) 
			
			{
				
				try {
					header.parse(buffer, 0, 4);
//				} catch (PacketParseException e1) {
//					// TODO Auto-generated catch block
//					e1.printStackTrace();
//				}
//				// parse request packet
//				try {
					switch (header.getPacketType()) {

					case 1:
						if (len == 12){
						packetFTD.parse(buffer, 0, len);
						
						//nastav9 parametry FT podle prijateho paketu FTD
						
						sessionState.setHeightoTree(packetFTD.getHeight());
						sessionState.setPortIn(packetFTD.getFTPort());
						sessionState.setStatusOfFT(packetFTD.getStatus());
						
						sessionState.addFTDreceived();
						
						logger.debug("RECEIVED FTD MESSAGE: port:"+pkt.getPort()+", t=" + getSimulation().getTime()+" length:"+len);
						//System.out.println("RECEIVED MESSAGE: FROM:" + pkt.getAddress()+" on port:"+pkt.getPort()+", t=" + getSimulation().getTime()+" length:"+len);
						logger.info(packetFTD.toString());
						}
						else
							System.out.println("Spatny prijem FTD");
												
						break;
						
					case 2:
						if(len == 8){
						packetFTI.parse(buffer, 0, len);
						
						sessionState.addFTIreceived();
						
						logger.debug("RECEIVED FTI MESSAGE: port:"+pkt.getPort()+", t=" + getSimulation().getTime()+" length:"+len);
						//System.out.println("RECEIVED MESSAGE: FROM:" + pkt.getAddress()+" on port:"+pkt.getPort()+", t=" + getSimulation().getTime()+" length:"+len);
						logger.info(packetFTI.toString());
						}
						
						/*
						 * prijem FTi znamena potvreni stavu por FTM nebo informace
						 * o nutnem prepocitani stromu a naslednem odeslani informace
						 * v ramci FTS
						 * */
						else
							System.out.println("Spatny prijem FTI");
						
						
						
						break;
						
					case 3:
						
						packetFTS.parse(buffer, 0, len);
						
						sessionState.addFTSreceived();
						
						logger.debug("RECEIVED FTS MESSAGE: port:"+pkt.getPort()+", t=" + getSimulation().getTime()+" length:"+len);
						//System.out.println("RECEIVED MESSAGE: FROM:" + pkt.getAddress()+" on port:"+pkt.getPort()+", t=" + getSimulation().getTime()+" length:"+len);
						logger.info(packetFTS.toString());
	
						
						break;
					
					
						
					default:
						break;
					}
					
					
					
					} 
				catch (PacketParseException e) 
					{
					
					} 
			} 
		}
	}
}
