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 logWriter;
	private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSz");
	
	public Player(OutputStream ostream, OutputStream logStream) {
		this.playerOStream = ostream;
		this.logWriter = new PrintWriter(logStream);
		initLog();
	}
	
	public void play(Chunk chunk) throws IOException {
		Calendar calendar = Calendar.getInstance();
		chunk.setPlayedTimeStamp(calendar.getTimeInMillis() - chunk.getPlayedTimeStamp());
		System.out.println(formatter.format(calendar.getTime()) + ": Playing chunk with seqNum " + chunk.getSeqNum());
		playerOStream.write(chunk.getBytes(), 0, chunk.getActualChunkLength());
		log(chunk);
	}
	
	private void initLog() {
		logWriter.println("seqNum,transTimestamp,recvTimestamp,playedTimestamp");
	}
	
	private void log(Chunk chunk) {
		logWriter.println(chunk.getSeqNum() + "," + 
						  chunk.getTransTimeStamp() + "," +
						  chunk.getRecTimeStamp() + "," + 
						  chunk.getPlayedTimeStamp());
		logWriter.flush();
	}
}

@@ -302,17 +302,65 @@
 tWriter 
-l
+timestampLogWriter;%0A%09private PrintWriter latencyL
 ogWriter
@@ -453,16 +453,18 @@
 Sz%22);%0A%09%0A
+%09%0A
 %09public 
@@ -505,17 +505,57 @@
 tStream 
-l
+timestampLogStream, OutputStream latencyL
 ogStream
@@ -597,17 +597,89 @@
 %0A%09%09this.
-l
+timestampLogWriter = new PrintWriter(timestampLogStream);%0A%09%09this.latencyL
 ogWriter
@@ -698,16 +698,23 @@
 Writer(l
+atencyL
 ogStream
@@ -725,16 +725,17 @@
 %09initLog
+s
 ();%0A%09%7D%0A%09
@@ -839,33 +839,23 @@
 ;%0A%09%09
-chunk.setPlayedTimeStamp(
+long latency = 
 cale
@@ -880,36 +880,198 @@
 is() - chunk.get
-Play
+LatencyInfo().getPlayedLatency();%0A%09%09chunk.getLatencyInfo().setPlayedLatency(latency);%0A%09%09chunk.getTimestampInfo().setPlayedTimeStamp(chunk.getTimestampInfo().getReceiv
 edTimeStamp());%0A
@@ -1063,24 +1063,34 @@
 dTimeStamp()
+ + latency
 );%0A%09%09System.
@@ -1276,51 +1276,400 @@
 %09log
-(chunk);%0A%09%7D%0A%09%0A%09private void initLog() %7B%0A%09%09l
+TimestampInfo(chunk);%0A%09%09logLatencyInfo(chunk);%0A%09%7D%0A%09%0A%09private void logLatencyInfo(Chunk chunk) %7B%0A%09%09latencyLogWriter.println(chunk.getSeqNum() + %22,%22 + %0A%09%09%09%09  chunk.getLatencyInfo().getTransmitionLatency() + %22,%22 +%0A%09%09%09%09  chunk.getLatencyInfo().getReceivedLatency() + %22,%22 + %0A%09%09%09%09  chunk.getLatencyInfo().getPlayedLatency());%0A%09%09latencyLogWriter.flush();%09%0A%09%7D%0A%0A%09private void initLogs() %7B%0A%09%09timestampL
 ogWr
@@ -1693,14 +1693,20 @@
 Num,
-t
+T
 rans
-T
+mited t
 imes
@@ -1714,13 +1714,18 @@
 amp,
-recvT
+Received t
 imes
@@ -1733,15 +1733,16 @@
 amp,
-p
+P
 layed
-T
+ t
 imes
@@ -1749,16 +1749,105 @@
 tamp%22);%0A
+%09%09latencyLogWriter.println(%22seqNum,Transmited latency,Received latency,Played latency%22);%0A
 %09%7D%0A%09%0A%09pr
@@ -1860,16 +1860,29 @@
 void log
+TimestampInfo
 (Chunk c
@@ -1891,17 +1891,26 @@
 nk) %7B%0A%09%09
-l
+timestampL
 ogWriter
@@ -1963,20 +1963,45 @@
 unk.getT
-rans
+imestampInfo().getTransmition
 TimeStam
@@ -2029,19 +2029,43 @@
 hunk.get
-Rec
+TimestampInfo().getReceived
 TimeStam
@@ -2091,16 +2091,35 @@
   chunk.
+getTimestampInfo().
 getPlaye
@@ -2135,17 +2135,26 @@
 p());%0A%09%09
-l
+timestampL
 ogWriter

set played timestamp and latency. log timestamp and latency times