package cc.monitors;

import cc.Client;

public class LogMonitor extends TemporalMonitor {

	private int counter = 0;
	private long sumBytes = 0;
	private long firstTimestamp = 0;
	private long lastTimestamp = 0;
	private long firstLabel = 0;
	private long lastLabel = 0;

	private void print() {
		if (lastTimestamp != firstTimestamp) {
			float seconds = ((float) (lastTimestamp - firstTimestamp)) / 1000;

			float rate = sumBytes * 8 / seconds/1000000;
			float loss = (((float) (lastLabel - firstLabel) / counter) - 1) * 100;
			float delay = (float) (lastTimestamp - firstTimestamp) / counter;
			float fps = counter / seconds;

			System.out.println(
				"Log \t"
					+ rate
					+ " Mbps\t"
					+ loss
					+ " %\t"
					+ delay
					+ " ms\t"
					+ fps
					+ " fps\t");
		} else {
			System.out.println("#Log Results Not Representative");
		}

	}

	private void refresh() {
		counter = 0;
		sumBytes = 0;
		firstTimestamp = lastTimestamp;
		firstLabel = lastLabel;
	}

	public void evaluate() {
		print();
		Client.alive();
		refresh();
	}

	public void update(Event event) {

		counter++;
		sumBytes += event.getSize();

		if (firstTime) {
			firstTimestamp = event.getTimestamp();
			firstLabel = event.getLabel();
			firstTime = false;
		}

		lastTimestamp = event.getTimestamp();
		lastLabel = event.getLabel();
		//System.out.print("|");
	}
}