package client;

import java.util.LinkedList;
import java.util.Queue;

import org.newdawn.slick.geom.Vector2f;

import util.EventProtocol;

/**
 * Thread to parse bytes to gamestats
 * 
 * @author timdolck
 *
 */
public class ParseBytes extends Thread {

	GameStatsEvents gsMonitor;
	ByteMonitor bMonitor;

	public ParseBytes(GameStatsEvents gsMonitor, ByteMonitor bMonitor) {
		this.gsMonitor = gsMonitor;
		this.bMonitor = bMonitor;
	}

	@Override
	public void run() {
		GameEvent event;
		while (true) {
			byte[] byteArray = bMonitor.readArrayFromServer();
			System.out.println("polled new msg:");
			for(byte b : byteArray){
				System.out.print(" "+b);
			}
			System.out.println();
			Queue<Byte> byteQueue = new LinkedList<Byte>();
			for (Byte b : byteArray) {
				byteQueue.add(b);
			}
			byte p = byteQueue.poll();
			if(p != EventProtocol.PLAYER_ID){
				System.err.println("ERROR IN PROTOCOL, recieved:" + p + " excpected:" + EventProtocol.PLAYER_ID);
				continue;
			}
				
			byte id = byteQueue.poll();
			event = new GameEvent(id);
			
			// TODO: remove carriagereturn in end of byte array?
			Byte b = null;
			if ((b = byteQueue.poll()) != null) {
				switch (b) {
					case EventProtocol.LOCAL_PLAYER_INIT:
						gsMonitor.addLocalPlayer(id);
						break;
					case EventProtocol.OPPONENT_PLAYER_INIT:
						System.out.println(gsMonitor.addOpponentPlayer(id));
						break;
					case EventProtocol.PLAYER_POS:
						float xpos = bytesToFloat(byteQueue);
						float ypos = bytesToFloat(byteQueue);
						Vector2f pos = new Vector2f(xpos, ypos);
						try {
							event.putPosition(pos);
						} catch (Exception e) {
							e.printStackTrace();
						}
						break;
					case EventProtocol.PLAYER_DIR:
						float xdir = bytesToFloat(byteQueue);
						float ydir = bytesToFloat(byteQueue);
						Vector2f dir = new Vector2f(xdir, ydir);
						try {
							event.putDirection(dir);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						break;

				}
			}
			gsMonitor.put(id, event);
		}
	}

	private float bytesToFloat(Queue<Byte> byteQueue) {
		byte[] floatBytes = new byte[4];
		for (int i = 0; i < 4; i++) {
			floatBytes[i] = byteQueue.poll();
		}

		int asInt = (floatBytes[0] & 0xFF) | ((floatBytes[1] & 0xFF) << 8)
				| ((floatBytes[2] & 0xFF) << 16)
				| ((floatBytes[3] & 0xFF) << 24);
		return Float.intBitsToFloat(asInt);

	}

}

@@ -10,16 +10,44 @@
 lient;%0A%0A
+import java.nio.ByteBuffer;%0A
 import j
@@ -2257,20 +2257,82 @@
 l();%0A%09%09%7D
+%0A%09%09return ByteBuffer.wrap(floatBytes).asFloatBuffer().get();
 %0A%0A%09%09
+/*
 int asIn
@@ -2503,15 +2503,25 @@
 asInt);%0A
+%09%09*/%0A%09%09%0A%09%09
 %0A%09%7D%0A%0A%7D%0A

change parsing to bytebuffer