package org.kne.cloud.network.klalb;

import java.net.SocketException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

public class LineDecitionComparator implements Comparator<KLALBRemoteLine> {
	private Map<KLALBRemoteLine,Long> predictedlatencys=new HashMap<>();
	public LineDecitionComparator (List<KLALBRemoteLine> krs,KLALBPacket curr,int priority) {
		for (Iterator<KLALBRemoteLine> iterator = krs.iterator(); iterator.hasNext();) {
			KLALBRemoteLine klalbRemoteSocket = (KLALBRemoteLine) iterator.next();
			long x=klalbRemoteSocket.getSndDelayFactor();
			//long x=klalbRemoteSocket.getMonitor().getLatency()>>1;
			/*long al=0;
			al=klalbRemoteSocket.statLengthBefore(priority)+curr.getLength();
			long speed=klalbRemoteSocket.getMonitor(). getOutSpeed();
			if(speed==0) {
				if(al>0) {
					x=Long.MAX_VALUE;
				}
			}else {
			x+=al*1000000000L/speed;
			}*/

			/*System.out.println(klalbRemoteSocket);
			System.out.println(klalbRemoteSocket.getSendQueue().size());
			System.out.println(x);*/
			predictedlatencys.put(klalbRemoteSocket, x);
		}
	}
	public Map<KLALBRemoteLine, Long> getPredictedlatencys() {
		return predictedlatencys;
	}

	@Override
	public int compare(KLALBRemoteLine o1, KLALBRemoteLine o2) {	
		long t1=predictedlatencys.get(o1);
		long t2=predictedlatencys.get(o2);
		if(t1>t2) {
			return 1;
		}else if(t1<t2){
			return -1;
		}else {
			return 0;
		}
	}

}
