package smims.networking.model;

import java.util.ArrayList;

public class TurnManager {
	private Turn currentTurn;
	private final ArrayList<Player> players;
	private final Board board;
	private final DiceRoller diceRoller;
	
	public TurnManager(ArrayList<Player> players, Board board, DiceRoller diceRoller) {
		this.players = players;
		this.board = board;
		this.diceRoller = diceRoller;
	}
	
	private void startNewTurn(Player player) {
		currentTurn = new Turn(player, board, diceRoller);
	}
	
	public Turn getCurrentTurn() {
		return currentTurn;
	}
	
	public void rollDice(int playerId) throws NotYourTurnException, MoveNotAllowedException {
		ensureIsPlayersTurn(playerId);
		getCurrentTurn().rollDice();
		changeTurnIfOver();
	}

	private void changeTurnIfOver() {
		if (getCurrentTurn().isOver()) {
			Player nextPlayer = getNextPlayer();
			startNewTurn(nextPlayer);
		}
	}

	private Player getNextPlayer() {
		int previousPlayerPosition = players.indexOf(getCurrentTurn().getPlayer());
		return players.get((previousPlayerPosition + 1) % players.size());
	}

	void ensureIsPlayersTurn(int playerId) throws NotYourTurnException {
		if (!currentTurn.isOfPlayer(playerId)) {
			throw new NotYourTurnException();
		}
	}
	
	public void moveCharacter(int playerId, int characterPos) throws NotYourTurnException, MoveNotAllowedException {
		ensureIsPlayersTurn(playerId);
		getCurrentTurn().moveCharacter(playerId, characterPos);
		changeTurnIfOver();
	}
	
	public int whoseTurn() {
		return getCurrentTurn().getPlayer().getPlayerId();
	}
}

@@ -304,16 +304,151 @@
 ller) %7B%0A
+%09%09if (players == null %7C%7C players.size() == 0 %7C%7C board == null %7C%7C diceRoller == null) %7B%0A%09%09%09throw new IllegalArgumentException();%0A%09%09%7D%0A%09%09%0A
 %09%09this.p
@@ -519,16 +519,48 @@
 Roller;%0A
+%09%09startNewTurn(players.get(0));%0A
 %09%7D%0A%09%0A%09pr

Set up turn manger correctly