package pylos.model;

import java.util.LinkedList;
import java.util.List;

public class Model {
	public static final int levels = 4;
	public static final int nbBalls = 30;
	public static final List<Ball> balls = new LinkedList<Ball>();
	public static final Player player1 = new Player(+1);
	public static final Player player2 = new Player(-1);
	public static final Player[] players = { player1, player2 };

	public static Player currentPlayer = player1;

	public List<Position> getPositionsToPlaceBallOnBoard() {
		List<Position> list = canPlaceBallAt(0);
		for (int level = 0; level < 4; level++) {
			for (Position position : canPlaceBallAt(level)) {
				list.add(position);
			}
		}

		return list;
	}

	public boolean isBoardEmpty() {
		for (Ball ball : balls) {
			if (ball.onBoard)
				return false;
		}
		return true;
	}

	public boolean isBallAt(Position position) { // does ball is on this position ?
		for (Ball ball : balls) {
			if(ball.onBoard){
				if (position.x == ball.position.x && position.y == ball.position.y && position.z == ball.position.z)
					return true;
			}
		}
		return false;
	}

	/**
	 * Checks if there are 4 balls under the position (so one can place a ball there)
	 */
	public boolean are4BallsUnder(Position position) {
		if (position.z == 0) // TODO: this does not belong here
			return true;
		for (int x = position.x; x < position.x + 2; x++) {
			for (int y = position.y; y < position.y + 2; y++) {
				if (!isBallAt(new Position(x, y, position.z - 1)))
					return false;
			}
		}
		return true;
	}

	public List<Position> canPlaceBallAt(int level) {
		List<Position> list = new LinkedList<Position>();
		for (int x = 0; x < 4 - level; x++) {
			for (int y = 0; y < 4 - level; y++) {
				if (are4BallsUnder(new Position(x, y, level)))
					if (!isBallAt(new Position(x, y, level)))
						list.add(new Position(x, y, level));
			}
		}
		return list;
	}

	public List<Position> getWhereToPlaceBallToCarryUp(Ball ball) {
		List<Position> list = new LinkedList<Position>();
		// TODO
		return list;
	}

	public static int ballsBySideAtLevel(int level) {
		return levels - level;
	}

	public static List<Position> getAllPositionsForLevelLineByLine(int level, int line) { // to check if any lines
		List<Position> list = new LinkedList<Position>();
		for (int x = 0; x < 4 - level; x++) {
			list.add(new Position(x, line, level));
		}
		return list;
	}

	public static List<List<Position>> getAllPositionForLevelSquareBySquare(int level) { // to check if any squares
		List<List<Position>> squares = new LinkedList<List<Position>>();
		for (int line = 1; line < 4 - level; line++) {
			for (int colomn = 1; colomn < 4 - level; colomn++) {
				List<Position> square = new LinkedList<Position>();
				square.add(new Position(line, colomn, level));
				square.add(new Position(line - 1, colomn, level));
				square.add(new Position(line, colomn - 1, level));
				square.add(new Position(line - 1, colomn - 1, level));
				squares.add(square);
			}
		}
		return squares;
	}
}

@@ -534,24 +534,33 @@
 t = 
-canPlaceBallAt(0
+new LinkedList%3CPosition%3E(
 );%0A%09
@@ -630,30 +630,31 @@
 ition : 
-canPlac
+accessibl
 eBall
-At
+s
 (level))
@@ -689,17 +689,16 @@
 %09%09%7D%0A%09%09%7D%0A
-%0A
 %09%09return
@@ -945,16 +945,17 @@
  %7B%0A%09%09%09if
+ 
 (ball.on
@@ -960,16 +960,17 @@
 onBoard)
+ 
 %7B%0A%09%09%09%09if
@@ -1170,16 +1170,51 @@
 position
+ or if is it on the first level%0A%09 *
  (so one
@@ -1259,30 +1259,30 @@
 boolean 
-are4BallsUnder
+canPlaceBallAt
 (Positio
@@ -1321,43 +1321,8 @@
 = 0)
- // TODO: this does not belong here
 %0A%09%09%09
@@ -1568,30 +1568,31 @@
 sition%3E 
-canPlac
+accessibl
 eBall
-At
+s
 (int lev
@@ -1742,22 +1742,22 @@
 if (
-are4BallsUnder
+canPlaceBallAt
 (new

rename a few methods and correct getPositionsToPlaceBallOnBoard list initialization