package pylos.view;

import java.util.LinkedList;

import pylos.model.Ball;
import pylos.model.Model;
import pylos.model.Player;

import com.jme3.scene.Spatial;

public class BoardGraphics {
	static final float HORIZONTAL_SCALE = 5;
	static final float VERTICAL_SCALE = 0.5f;
	static final float BOARD_HEIGHT = 0.9f;
	static final int BALLS_BY_SIDE = 9;
	static final int BALLS_BY_SMALL_SIDE = 3;
	Spatial board;

	public BoardGraphics(View view) {
		board = view.getAssetManager().loadModel("Models/Board/Board.mesh.xml");
		board.scale(HORIZONTAL_SCALE, VERTICAL_SCALE, HORIZONTAL_SCALE);
	}

	public Spatial getSpatial() {
		return board;
	}

	public void drawBalls() {
		float distanceCenterSide = BallGraphics.DIAMETER * (BALLS_BY_SIDE / 2);

		for (Player player : Model.players) {
			LinkedList<Ball> ballsInSide = player.ballsInSide();
			int limit = Math.min(BALLS_BY_SIDE, ballsInSide.size());

			for (int i = 0; i < limit; i++) {
				ballsInSide.pop().graphics.setPosition(
						distanceCenterSide * player.side,
						BOARD_HEIGHT,
						(i - BALLS_BY_SIDE / 2) * BallGraphics.DIAMETER
						);
			}

			limit = Math.min(BALLS_BY_SMALL_SIDE, ballsInSide.size());
			for (int i = 0; i < limit; i++) {
				ballsInSide.pop().graphics.setPosition(
						(i + 1) * BallGraphics.DIAMETER * player.side,
						BOARD_HEIGHT,
						distanceCenterSide
						);
			}

			limit = Math.min(BALLS_BY_SMALL_SIDE, ballsInSide.size());
			for (int i = 0; i < limit; i++) {
				ballsInSide.pop().graphics.setPosition(
						(i + 1) * BallGraphics.DIAMETER * player.side,
						BOARD_HEIGHT,
						-distanceCenterSide
						);
			}
		}
	}
}

@@ -848,59 +848,166 @@
 int 
-limit = Math.min(BALLS_BY_SIDE, ballsInSide.size())
+n = ballsInSide.size();%0A%09%09%09int limitFront = Math.min(BALLS_BY_SIDE, n);%0A%09%09%09int limitZNeg = (n - limitFront) / 2;%0A%09%09%09int limitZPos = n - limitFront - limitZNeg
 ;%0A%0A%09
@@ -1025,32 +1025,36 @@
 i = 0; i %3C limit
+ZPos
 ; i++) %7B%0A%09%09%09%09bal
@@ -1096,34 +1096,47 @@
 (%0A%09%09%09%09%09%09
-distanceCenterSide
+(i + 1) * BallGraphics.DIAMETER
  * playe
@@ -1173,132 +1173,41 @@
 %09%09%09%09
-(i - BALLS_BY_SIDE / 2) * BallGraphics.DIAMETER%0A%09%09%09%09%09%09);%0A%09%09%09%7D%0A%0A%09%09%09limit = Math.min(BALLS_BY_SMALL_SIDE, ballsInSide.size());
+distanceCenterSide%0A%09%09%09%09%09%09);%0A%09%09%09%7D%0A
 %0A%09%09%09
@@ -1223,32 +1223,37 @@
 i = 0; i %3C limit
+Front
 ; i++) %7B%0A%09%09%09%09bal
@@ -1299,39 +1299,26 @@
 %09%09%09%09
-(i + 1) * BallGraphics.DIAMETER
+distanceCenterSide
  * p
@@ -1359,103 +1359,70 @@
 %09%09%09%09
-distanceCenterSide%0A%09%09%09%09%09%09);%0A%09%09%09%7D%0A%0A%09%09%09limit = Math.min(BALLS_BY_SMALL_SIDE, ballsInSide.size());
+(BALLS_BY_SIDE / 2 - i) * BallGraphics.DIAMETER%0A%09%09%09%09%09%09);%0A%09%09%09%7D%0A
 %0A%09%09%09
@@ -1446,16 +1446,20 @@
  %3C limit
+ZNeg
 ; i++) %7B
@@ -1502,35 +1502,55 @@
 osition(%0A%09%09%09%09%09%09(
+BALLS_BY_SIDE / 2 - 
 i 
-+
+-
  1) * BallGraphi

reorder the placement of balls on the sides, to be incremental from left to right for the 1st Player