package pylos.model;

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

public class Model {
	public static final int LEVELS = 4;
	public static final int BALLS = 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 Model() {
		Position.createPositions();
	}

	public List<Position> getPositionsToPlaceBallOnBoard() {
		List<Position> list = new LinkedList<Position>();
		for (int level = 0; level < LEVELS; level++) {
			for (Position position : accessibleBalls(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 && ball.position == position)
				return true;
		}
		return false;
	}

	/**
	 * Checks if there are 4 balls under the position or if is it on the first level
	 * (so one can place a ball there)
	 */
	public boolean canPlaceBallAt(Position position) {
		if (position.z == 0)
			return true;
		for (int x = position.x; x < position.x + 2; x++) {
			for (int y = position.y; y < position.y + 2; y++) {
				if (!isBallAt(Position.at(x, y, position.z - 1)))
					return false;
			}
		}
		return true;
	}

	public List<Position> accessibleBalls(int level) {
		List<Position> list = new LinkedList<Position>();
		for (int x = 0; x < LEVELS - level; x++) {
			for (int y = 0; y < LEVELS - level; y++) {
				if (canPlaceBallAt(Position.at(x, y, level)))
					if (!isBallAt(Position.at(x, y, level)))
						list.add(Position.at(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 < LEVELS - level; x++) {
			list.add(Position.at(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 < LEVELS - level; line++) {
			for (int column = 1; column < LEVELS - level; column++) {
				List<Position> square = new LinkedList<Position>();
				square.add(Position.at(line, column, level));
				square.add(Position.at(line - 1, column, level));
				square.add(Position.at(line, column - 1, level));
				square.add(Position.at(line - 1, column - 1, level));
				squares.add(square);
			}
		}
		return squares;
	}
}

@@ -1153,12 +1153,12 @@
 if i
-s it
+t is
  on 
@@ -1173,16 +1173,58 @@
 t level%0A
+%09 * and if there is no ball at that place%0A
 %09 * (so 
@@ -1308,16 +1308,59 @@
 tion) %7B%0A
+%09%09if (isBallAt(position))%0A%09%09%09return false;%0A
 %09%09if (po
@@ -1696,32 +1696,53 @@
 st%3CPosition%3E();%0A
+%09%09Position position;%0A
 %09%09for (int x = 0
@@ -1824,27 +1824,19 @@
 %09%09%09%09
-if (canPlaceBallAt(
+position = 
 Posi
@@ -1859,20 +1859,18 @@
 vel)
-))%0A%09
+;%0A
 %09%09%09%09if (
 !isB
@@ -1869,50 +1869,39 @@
 if (
-!is
+canPlace
 BallAt(
-P
+p
 osition
-.at(x, y, level)
 ))%0A%09%09%09%09%09
 %09lis
@@ -1896,17 +1896,16 @@
 ))%0A%09%09%09%09%09
-%09
 list.add
@@ -1909,32 +1909,16 @@
 add(
-P
+p
 osition
-.at(x, y, level)
 );%0A%09

simplify accessibleBalls logic