package jrummikub.model;

import java.util.ArrayList;
import java.util.List;

/** Class managing the overall and momentary RoundState */
public class RoundState implements IRoundState {
	private IGameSettings gameSettings;

	private ITable table;
	private List<Player> players;
	private int activePlayer;
	private StoneHeap gameHeap;

	/**
	 * Create a new RoundState with an empty table
	 * 
	 * @param gameSettings
	 *          the game settings
	 */
	public RoundState(IGameSettings gameSettings) {
		this.gameSettings = gameSettings;

		table = new Table();
		players = new ArrayList<Player>();

		for (PlayerSettings playerSettings : gameSettings.getPlayerList()) {
			players.add(new Player(playerSettings));
		}

		activePlayer = 0;
		gameHeap = new StoneHeap();
	}

	@Override
	public ITable getTable() {
		return table;
	}

	@Override
	public void setTable(ITable table) {
		this.table = table;
	}

	@Override
	public int getPlayerCount() {
		return players.size();
	}

	@Override
	public void nextPlayer() {
		activePlayer = (activePlayer + 1) % players.size();
	}

	@Override
	public IPlayer getNthNextPlayer(int i) {
		return players.get((activePlayer + i) % players.size());
	}

	@Override
	public IPlayer getActivePlayer() {
		return players.get(activePlayer);
	}

	@Override
	public StoneHeap getGameHeap() {
		return gameHeap;
	}

	@Override
	public IGameSettings getGameSettings() {
		return gameSettings;
	}
}
