package br.ufrj.cos.redes.receiver;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import br.ufrj.cos.redes.fileAccess.Chunk;

public class Player {
	private OutputStream playerOStream;
	private PrintWriter timestampLogWriter;
	private PrintWriter latencyLogWriter;
	private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
	
	
	public Player(OutputStream ostream, OutputStream timestampLogStream, OutputStream latencyLogStream) {
		this.playerOStream = ostream;
		this.timestampLogWriter = new PrintWriter(timestampLogStream);
		this.latencyLogWriter = new PrintWriter(latencyLogStream);
		initLogs();
	}
	
	public void play(Chunk chunk) throws IOException {
		Calendar calendar = Calendar.getInstance();
		long latency = calendar.getTimeInMillis() - chunk.getLatencyInfo().getPlayedLatency();
		chunk.getLatencyInfo().setPlayedLatency(latency);
		chunk.getTimestampInfo().setPlayedTimeStamp(chunk.getTimestampInfo().getReceivedTimeStamp() + latency);
		System.out.println(formatter.format(calendar.getTime()) + ": Playing chunk with seqNum " + chunk.getSeqNum());
		playerOStream.write(chunk.getBytes(), 0, chunk.getActualChunkLength());
		logTimestampInfo(chunk);
		logLatencyInfo(chunk);
	}
	
	private void logLatencyInfo(Chunk chunk) {
		latencyLogWriter.println(chunk.getSeqNum() + "," + 
				  chunk.getLatencyInfo().getTransmitionLatency() + "," +
				  chunk.getLatencyInfo().getReceivedLatency() + "," + 
				  chunk.getLatencyInfo().getPlayedLatency());
		latencyLogWriter.flush();	
	}

	private void initLogs() {
		timestampLogWriter.println("seqNum,Transmited timestamp,Received timestamp,Played timestamp");
		latencyLogWriter.println("seqNum,Transmited latency,Received latency,Played latency");
	}
	
	private void logTimestampInfo(Chunk chunk) {
		timestampLogWriter.println(chunk.getSeqNum() + "," + 
						  chunk.getTimestampInfo().getTransmitionTimeStamp() + "," +
						  chunk.getTimestampInfo().getReceivedTimeStamp() + "," + 
						  chunk.getTimestampInfo().getPlayedTimeStamp());
		timestampLogWriter.flush();
	}
}

@@ -453,16 +453,132 @@
 Sz%22);%0A%09%0A
+%09private boolean isFirst;%0A%09private Calendar firstChunkPlayedRealTimestamp;%0A%09private long firstChunkPlayedTimestamp;%0A
 %09%0A%09publi
@@ -832,16 +832,75 @@
 tream);%0A
+%09%09%0A%09%09this.isFirst = true;%0A%09%09firstChunkPlayedTimestamp = 0;%0A
 %09%09initLo
@@ -962,17 +962,16 @@
 ption %7B%0A
-%09
 %09Calenda
@@ -1012,23 +1012,163 @@
 );%0A%09
-%09long latency =
+if (isFirst) %7B%0A%09%09firstChunkPlayedRealTimestamp = calendar;%0A%09%09chunk.getTimestampInfo().setPlayedTimeStamp(chunk.getTimestampInfo().getReceivedTimeStamp() + 
  cal
@@ -1238,64 +1238,315 @@
 cy()
+)
 ;%0A%09%09
-chunk.getLatencyInfo().setPlayedLatency(latency
+firstChunkPlayedTimestamp = chunk.getTimestampInfo().getPlayedTimeStamp();%0A%09%09isFirst = false;%0A%09%7D else %7B%0A%09%09chunk.getTimestampInfo().setPlayedTimeStamp(calendar.getTimeInMillis() - firstChunkPlayedRealTimestamp.getTimeInMillis() + firstChunkPlayedTimestamp
 );%0A%09
+%7D%0A%0A
 %09
+chunk.getLatencyInfo().setPlayedLatency(
 chun
@@ -1558,33 +1558,33 @@
 TimestampInfo().
-s
+g
 etPlayedTimeStam
@@ -1577,32 +1577,36 @@
 PlayedTimeStamp(
+) - 
 chunk.getTimesta
@@ -1640,22 +1640,11 @@
 mp()
- + latency
 );%0A
-%09
 %09Sys
@@ -1751,17 +1751,16 @@
 Num());%0A
-%09
 %09playerO
@@ -1824,17 +1824,16 @@
 gth());%0A
-%09
 %09logTime
@@ -1850,17 +1850,16 @@
 chunk);%0A
-%09
 %09logLate
@@ -1874,17 +1874,16 @@
 chunk);%0A
-%09
 %7D%0A%09%0A%09pri
@@ -2720,10 +2720,11 @@
 h();%0A%09%7D%0A
+%0A
 %7D%0A

add new time calculation