package pylos.view;

import java.util.LinkedList;

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

import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;

public class BoardGraphics {
	public static final float BOARD_HEIGHT = 1f;
	public static final float BALL_DIAMETER = 1.38f;
	static final float HORIZONTAL_SCALE = 5;
	static final float VERTICAL_SCALE = 0.5f;
	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 = BALL_DIAMETER * (BALLS_BY_SIDE / 2);

		for (Player player : Model.players) {
			LinkedList<Ball> ballsOnBoard = new LinkedList<Ball>(), ballsInSide = player.partitionBalls(ballsOnBoard);
			int n = ballsInSide.size();
			int limitFront = Math.min(BALLS_BY_SIDE, n);
			int limitZNeg = (n - limitFront) / 2;
			int limitZPos = n - limitFront - limitZNeg;

			for (int i = 0; i < limitZPos; i++) {
				ballsInSide.pop().graphics.center().move(
						(i + 1) * BALL_DIAMETER * player.side,
						BOARD_HEIGHT,
						distanceCenterSide
						);
			}

			for (int i = 0; i < limitFront; i++) {
				ballsInSide.pop().graphics.center().move(
						distanceCenterSide * player.side,
						BOARD_HEIGHT,
						(BALLS_BY_SIDE / 2 - i) * BALL_DIAMETER
						);
			}

			for (int i = 0; i < limitZNeg; i++) {
				ballsInSide.pop().graphics.center().move(
						(BALLS_BY_SIDE / 2 - i - 1) * BALL_DIAMETER * player.side,
						BOARD_HEIGHT,
						-distanceCenterSide
						);
			}

			for (Ball ball : ballsOnBoard) {
				place(ball.graphics, ball.x, ball.y, ball.level);
			}
		}
	}

	public void place(Geometry ball, int x, int y, int level) {
		float offset = (Model.ballsBySideAtLevel(level) - 1) * BALL_DIAMETER / 2;
		ball.center().move(
				x * BALL_DIAMETER - offset,
				BoardGraphics.BOARD_HEIGHT + level * BALL_DIAMETER,
				y * BALL_DIAMETER - offset
				);
	}
}

@@ -722,24 +722,237 @@
  board;%0A%09%7D%0A%0A
+%09/*%0A%09 * The balls on the board must be placed this way (to look like natural moves)%0A%09 * 13                      14%0A%09 * 11                      12%0A%09 *  9                      10%0A%09 *  7  5  3  1  0  2  4  6  8%0A%09 */%0A
 %09public void
@@ -1351,16 +1351,37 @@
 tZNeg;%0A%0A
+%09%09%09// 0-8 front line%0A
 %09%09%09for (
@@ -1400,20 +1400,21 @@
  %3C limit
-ZPos
+Front
 ; i++) %7B
@@ -1470,31 +1470,26 @@
 %09%09%09%09
-(i + 1) * BALL_DIAMETER
+distanceCenterSide
  * p
@@ -1530,41 +1530,138 @@
 %09%09%09%09
-distanceCenterSide%0A%09%09%09%09%09%09);%0A%09%09%09%7D%0A
+((i + 1) / 2) * BALL_DIAMETER * (i %25 2 == 1 ? 1 : -1)%0A%09%09%09%09%09%09);%0A%09%09%09%7D%0A%0A%09%09%09// 9-14 sides%0A%09%09%09final int offset = BALLS_BY_SIDE / 2 - 1;
 %0A%09%09%09
@@ -1677,37 +1677,36 @@
 i = 0; i %3C limit
-Front
+ZPos
 ; i++) %7B%0A%09%09%09%09bal
@@ -1750,34 +1750,44 @@
 (%0A%09%09%09%09%09%09
-distanceCenterSide
+(offset - i) * BALL_DIAMETER
  * playe
@@ -1824,82 +1824,44 @@
 %09%09%09%09
-(BALLS_BY_SIDE / 2 - i) * BALL_DIAMETER%0A%09%09%09%09%09%09);%0A%09%09%09%7D%0A%0A%09%09%09for (int i = 0; 
+distanceCenterSide%0A%09%09%09%09%09%09);%0A%09%09%09%09if (
 i %3C 
@@ -1869,25 +1869,21 @@
 imitZNeg
-; i++
 ) %7B%0A
+%09
 %09%09%09%09ball
@@ -1930,34 +1930,20 @@
 %09%09%09%09
-(BALLS_BY_SIDE / 2 - i - 1
+%09(offset - i
 ) * 
@@ -1963,32 +1963,33 @@
  * player.side,%0A
+%09
 %09%09%09%09%09%09BOARD_HEIG
@@ -1998,16 +1998,17 @@
 ,%0A%09%09%09%09%09%09
+%09
 -distanc
@@ -2021,27 +2021,34 @@
 rSide%0A%09%09%09%09%09%09
+%09
 );%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%0A%09%09%09for

correct ball placement on sides (not really kind code)