package slimpleslickgame;

import java.nio.ByteBuffer;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.newdawn.slick.geom.Shape;
import org.newdawn.slick.geom.Vector2f;

import util.EventProtocol;
import client.ByteMonitor;
import client.MessageWrapper;

public class LocalPlayer extends Player {

	private GameContainer gc;
	private ByteMonitor bm;
	private int time;
	private int creepID;

	public LocalPlayer(GameContainer gc, ByteMonitor bm, byte id) {
		this.gc = gc;
		this.bm = bm;
		time = 0;
		creepID = 1;
		super.id = id;
	}

	@Override
	public void init(Vector2f pos) {
		super.init(pos);
		byte[] msg = { EventProtocol.OPPONENT_PLAYER_INIT };
		bm.putArrayToServer(msg, super.id);
	}

	public void update(int delta, Shape containerShape) {
		time++;
		if (processInput(gc.getInput())) {
			if (bm != null) {
				byte[] bytes = MessageWrapper
						.appendByteArray(
								MessageWrapper
										.getPlayerPositionBytes(super.position),
								MessageWrapper
										.getPlayerDirectionBytes(super.direction));
				bm.putArrayToServer(bytes, id);
			}
			super.updatePosition(containerShape);
		}

		if (time % 60 == 0) {
			Vector2f initPos = new Vector2f(super.position.x, 0);
			super.creeps.put(creepID, new Creep(initPos));

			byte[] bytes = MessageWrapper.appendByteArray(
					MessageWrapper.appendByteArray(new byte[]{EventProtocol.CREEP_INIT, EventProtocol.CREEP_ID}, ByteBuffer.allocate(4).putInt(creepID).array()), 
					MessageWrapper.appendByteArray(new byte[]{EventProtocol.CREEP_POS}, MessageWrapper.getVector2fBytes(initPos)));
			
			bm.putArrayToServer(bytes, super.id);
			creepID++;
		}

		for (int i = 0; i < super.creeps.size(); i++) {
			Creep c = super.creeps.get(i);
			if (c != null) {
				if (c.getPosition().y > Application.HEIGHT) {
					byte[] bytes = new byte[] { EventProtocol.CREEP_DIED,
							EventProtocol.CREEP_ID, (byte) i };
					bm.putArrayToServer(bytes, super.id);
				}
				c.update(delta);
			}
		}
		gun.update(delta);
	}

	private boolean processInput(Input input) {
		Vector2f direction = new Vector2f(0, 0);
		boolean dirChanged = false;

		if (input.isKeyDown(Input.KEY_LEFT)) {
			direction.add(new Vector2f(-1, 0));
			dirChanged = true;
		}

		if (input.isKeyDown(Input.KEY_RIGHT)) {
			direction.add(new Vector2f(1, 0));
			dirChanged = true;
		}

		if (input.isKeyDown(Input.KEY_UP)) {
			direction.add(new Vector2f(0, -1));
			dirChanged = true;
		}

		if (input.isKeyDown(Input.KEY_DOWN)) {
			direction.add(new Vector2f(0, 1));
			dirChanged = true;
		}
		// TODO: add shooting capabilities
		if (input.isKeyPressed(Input.KEY_SPACE)) {
			Vector2f shotPos = new Vector2f(this.position.x
					+ this.shape.getWidth() / 2, this.position.y
					+ this.shape.getHeight() / 2);
			super.gun.shoot(shotPos);

			byte[] bytes = MessageWrapper.appendByteArray(
					MessageWrapper.appendByteArray(new byte[]{EventProtocol.BULLET_INIT, EventProtocol.BULLET_ID}, ByteBuffer.allocate(4).putInt(super.gun.getbulletID()).array()), 
					MessageWrapper.appendByteArray(new byte[]{EventProtocol.BULLET_POS}, MessageWrapper.getVector2fBytes(shotPos)));
			bm.putArrayToServer(bytes, super.id);
		}

		super.setDirection(direction);
		return dirChanged;
	}

}

@@ -883,23 +883,16 @@
 eWrapper
-%0A%09%09%09%09%09%09
 .appendB
@@ -919,35 +919,24 @@
 ssageWrapper
-%0A%09%09%09%09%09%09%09%09%09%09
 .getPlayerPo
@@ -990,19 +990,8 @@
 pper
-%0A%09%09%09%09%09%09%09%09%09%09
 .get
@@ -1798,24 +1798,55 @@
 e%5B%5D bytes = 
+MessageWrapper.appendByteArray(
 new byte%5B%5D %7B
@@ -1843,19 +1843,17 @@
 w byte%5B%5D
- %7B 
+%7B
 EventPro
@@ -1869,24 +1869,17 @@
 EP_DIED,
-%0A%09%09%09%09%09%09%09
+ 
 EventPro
@@ -1896,20 +1896,58 @@
 P_ID
+%7D
 , 
-(byte) i %7D
+ByteBuffer.allocate(4).putInt(creepID).array())
 ;%0A%09%09

fix creep died msg