package jrummikub.control;

import static jrummikub.model.StoneColor.*;
import static org.junit.Assert.*;

import java.awt.Color;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import jrummikub.control.turn.TurnMode;
import jrummikub.model.GameSettings;
import jrummikub.model.GameState;
import jrummikub.model.Hand;
import jrummikub.model.IHand;
import jrummikub.model.IPlayer;
import jrummikub.model.IRoundState;
import jrummikub.model.MockRoundState;
import jrummikub.model.MockTable;
import jrummikub.model.PlayerSettings;
import jrummikub.model.Position;
import jrummikub.model.RoundState;
import jrummikub.model.Score;
import jrummikub.model.Stone;
import jrummikub.model.StoneSet;
import jrummikub.util.IListener1;
import jrummikub.util.Pair;
import jrummikub.view.IView.BottomPanelType;
import jrummikub.view.MockView;

import org.junit.Before;
import org.junit.Test;

/**
 * Tests for {@link RoundControl}
 */
public class RoundControlTest {
	private MockView view;
	private MockRoundState testRoundState;
	private RoundControl testRound;
	private MockTable testTable;

	private GameSettings gameSettings;
	private IRoundState roundState;
	private RoundControl roundControl;

	private boolean roundEnded;
	private Score roundScore;
	protected boolean roundRestarted;

	Stone blueFour = new Stone(4, BLUE);
	Stone blackFour = new Stone(4, BLACK);
	Stone redFour = new Stone(4, RED);
	Stone orangeFour = new Stone(4, ORANGE);

	Stone blueFive = new Stone(5, BLUE);
	Stone blueSix = new Stone(6, BLUE);
	Stone blueSeven = new Stone(7, BLUE);
	Stone blackSeven = new Stone(7, BLACK);
	Stone redSeven = new Stone(7, RED);
	Stone orangeSeven = new Stone(7, ORANGE);

	Stone blueOne = new Stone(1, BLUE);
	Stone blueTwo = new Stone(2, BLUE);
	Stone blueThree = new Stone(3, BLUE);

	Stone blueTen = new Stone(10, BLUE);
	Stone redTen = new Stone(10, RED);
	Stone blueEleven = new Stone(11, BLUE);

	Stone redEight = new Stone(8, RED);
	Stone redNine = new Stone(9, RED);
	Stone redEleven = new Stone(11, RED);

	Stone blueEight = new Stone(8, BLUE);
	Stone blackEight = new Stone(8, BLACK);
	Stone orangeEight = new Stone(8, ORANGE);

	Stone redOne = new Stone(1, RED);
	Stone blackOne = new Stone(1, BLACK);

	/**
	 * For each test create a round control initialized by a mock model and view
	 */
	@Before
	public void setup() {
		view = new MockView();
		testRoundState = new MockRoundState();
		testRoundState.gameState = new GameState();
		testRound = new RoundControl(testRoundState, view);
		Stone stone = testRoundState.getStoneHeap().drawStone();
		testRoundState.table.drop(new StoneSet(stone), new Position(5, 0));
		testTable = new MockTable();
		testTable.sets.add(testRoundState.table.sets.get(0));
		testRoundState.table.clonedTable = testTable;
		roundEnded = false;

		gameSettings = new GameSettings();

		gameSettings.getPlayerList().add(new PlayerSettings("Ida", Color.RED));
		gameSettings.getPlayerList().add(
				new PlayerSettings("Matthias", Color.YELLOW));
		gameSettings.getPlayerList().add(
				new PlayerSettings("Jannis", Color.GREEN));
		gameSettings.getPlayerList().add(
				new PlayerSettings("Bennet", Color.BLACK));
		roundState = new RoundState(gameSettings, new GameState());
		roundControl = new RoundControl(roundState, view);
	}

	private void checkCorrectlyDealt() {
		GameSettings settings = testRoundState.getGameSettings();
		int totalStones = settings.getHighestValue()
				* settings.getStoneSetNumber()
				* settings.getStoneColors().size() + settings.getJokerNumber();
		assertEquals(
				totalStones - testRoundState.getPlayerCount()
						* settings.getNumberOfStonesDealt()
						- testRoundState.table.getSize(), testRoundState
						.getStoneHeap().getSize());
		for (int i = 0; i < testRoundState.getPlayerCount(); i++) {
			assertEquals(settings.getNumberOfStonesDealt(), testRoundState
					.getNthNextPlayer(i).getHand().getSize());
		}
	}

	private void checkTurnStartSetUp() {
		assertNotNull(view.currentPlayerName);
		// TODO Check player list in view

		assertSame(BottomPanelType.START_TURN_PANEL, view.bottomPanelType);
		assertFalse(view.startTurnEvent.listeners.isEmpty());
		checkTableDisplay();
	}

	private void checkTableDisplay() {
		Iterator<Pair<StoneSet, Position>> stoneSetsView = view.tablePanel.stoneSets
				.iterator();
		Iterator<Pair<StoneSet, Position>> stoneSetsModel = testRoundState.table
				.clone().iterator();

		while (stoneSetsView.hasNext()) {
			assertTrue(stoneSetsModel.hasNext());
			assertSame(stoneSetsView.next(), stoneSetsModel.next());
		}
		assertFalse(stoneSetsModel.hasNext());
	}

	private void resetTurnStart() {
		view.currentPlayerName = null;
		// TODO reset player list
		view.bottomPanelType = null;
	}

	/**
	 * Check correctly dealt with more than 14 stones/ different than default
	 * stones
	 */
	@Test
	public void checkCorrectlyDealtMoreStones() {
		testRoundState.getGameSettings().setNumberOfStonesDealt(15);
		testRound.deal();
		checkCorrectlyDealt();
	}

	// laidOut test cases
	/** Threshold=30 */
	@Test
	public void laidOutValidTooFew() {
		roundControl.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}

		IHand hand = roundState.getActivePlayer().getHand();
		hand.drop(blueOne, new Position(0, 0));
		hand.drop(blueTwo, new Position(0, 0));
		hand.drop(blueThree, new Position(0, 0));
		view.startTurnEvent.emit();

		assertFalse(roundState.getActivePlayer().getLaidOut());

		view.handPanel.stoneClickEvent.emit(blueOne, false);
		view.handPanel.stoneClickEvent.emit(blueTwo, true);
		view.handPanel.stoneClickEvent.emit(blueThree, true);

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.playerPanel.endTurnEvent.emit();
		assertFalse(roundState
				.getNthNextPlayer(roundState.getPlayerCount() - 1).getLaidOut());
		assertEquals(0, roundState.getTable().getSize());
		assertEquals(14 + 6, roundState.getActivePlayer().getHand().getSize());
	}

	/** */
	@Test
	public void laidOutValidUnchanged() {
		roundControl.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}

		view.startTurnEvent.emit();

		assertFalse(roundState.getActivePlayer().getLaidOut());

		view.playerPanel.endTurnEvent.emit();
		assertFalse(roundState.getNthNextPlayer(-1).getLaidOut());
		assertEquals(0, roundState.getTable().getSize());

		assertEquals(14 + 1, roundState.getNthNextPlayer(-1).getHand()
				.getSize());
	}

	/** Threshold=30 */
	@Test
	public void laidOutInvalidEnough() {
		roundControl.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}

		IHand hand = roundState.getActivePlayer().getHand();
		dropStonesOnHand(hand, Arrays.asList(blueOne, blueTwo, blueThree,
				blueTen, redTen, blueEleven));
		view.startTurnEvent.emit();

		view.handPanel.stoneClickEvent.emit(blueOne, false);
		view.handPanel.stoneClickEvent.emit(blueTwo, true);
		view.handPanel.stoneClickEvent.emit(blueThree, true);

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		assertFalse(roundState.getActivePlayer().getLaidOut());

		view.handPanel.stoneClickEvent.emit(blueTen, false);
		view.handPanel.stoneClickEvent.emit(redTen, true);
		view.handPanel.stoneClickEvent.emit(blueEleven, true);

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.playerPanel.endTurnEvent.emit();
		view.acknowledgeInvalidEvent.emit();

		assertFalse(roundState.getNthNextPlayer(-1).getLaidOut());
		assertEquals(0, roundState.getTable().getSize());

		assertEquals(14 + 9, roundState.getNthNextPlayer(-1).getHand()
				.getSize());
	}

	private void dropStonesOnHand(IHand hand, List<Stone> stones) {
		for (int i = 0; i < stones.size(); i++) {
			hand.drop(stones.get(i), new Position(0, 0));
		}
	}

	/** Threshold=30 */
	@Test
	public void laidOutTooFewChangedTable() {
		roundControl.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}
		// Fake Turn to put stones on the table
		IHand hand = roundState.getActivePlayer().getHand();
		dropStonesOnHand(hand, Arrays.asList(blueOne, blueTwo, blueThree,
				blueSeven, redSeven, blackSeven, orangeSeven));
		view.startTurnEvent.emit();

		view.handPanel.stoneClickEvent.emit(redSeven, false);
		view.handPanel.stoneClickEvent.emit(blueSeven, true);
		view.handPanel.stoneClickEvent.emit(blackSeven, true);
		view.handPanel.stoneClickEvent.emit(orangeSeven, true);

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.handPanel.stoneClickEvent.emit(blueOne, false);
		view.handPanel.stoneClickEvent.emit(blueTwo, true);
		view.handPanel.stoneClickEvent.emit(blueThree, true);

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.playerPanel.endTurnEvent.emit();
		assertEquals(2, roundState.getTable().getSize());

		hand = roundState.getActivePlayer().getHand();
		dropStonesOnHand(hand, Arrays.asList(redEight, redNine, redTen));
		hand.drop(redEight, new Position(0, 0));
		hand.drop(redNine, new Position(0, 0));
		hand.drop(redTen, new Position(0, 0));
		view.startTurnEvent.emit();

		view.tablePanel.stoneClickEvent.emit(redSeven, false);
		view.handPanel.stoneClickEvent.emit(redEight, true);
		view.handPanel.stoneClickEvent.emit(redNine, true);
		view.handPanel.stoneClickEvent.emit(redTen, true);

		assertFalse(roundState.getActivePlayer().getLaidOut());

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.playerPanel.endTurnEvent.emit();
		view.acknowledgeInvalidEvent.emit();
		assertFalse(roundState.getNthNextPlayer(-1).getLaidOut());
		assertEquals(2, roundState.getTable().getSize());
		assertEquals(14 + 6, roundState.getNthNextPlayer(-1).getHand()
				.getSize());
	}

	/** Threshold=30 */
	@Test
	public void laidOutEnoughChangedTable() {
		roundControl.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}
		// Fake Turn to put stones on the table
		IHand hand = roundState.getActivePlayer().getHand();
		dropStonesOnHand(hand, Arrays.asList(blueOne, blueTwo, blueThree,
				blueSeven, redSeven, blackSeven, orangeSeven));
		view.startTurnEvent.emit();

		view.handPanel.stoneClickEvent.emit(redSeven, false);
		view.handPanel.stoneClickEvent.emit(blueSeven, true);
		view.handPanel.stoneClickEvent.emit(blackSeven, true);
		view.handPanel.stoneClickEvent.emit(orangeSeven, true);

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.handPanel.stoneClickEvent.emit(blueOne, false);
		view.handPanel.stoneClickEvent.emit(blueTwo, true);
		view.handPanel.stoneClickEvent.emit(blueThree, true);

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.playerPanel.endTurnEvent.emit();
		assertEquals(2, roundState.getTable().getSize());

		hand = roundState.getActivePlayer().getHand();
		dropStonesOnHand(hand,
				Arrays.asList(redEight, redNine, redTen, redEleven));
		view.startTurnEvent.emit();

		view.tablePanel.stoneClickEvent.emit(redSeven, false);
		view.handPanel.stoneClickEvent.emit(redEight, true);
		view.handPanel.stoneClickEvent.emit(redNine, true);
		view.handPanel.stoneClickEvent.emit(redTen, true);
		view.handPanel.stoneClickEvent.emit(redEleven, true);

		assertFalse(roundState.getActivePlayer().getLaidOut());

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.playerPanel.endTurnEvent.emit();
		view.acknowledgeInvalidEvent.emit();
		assertFalse(roundState.getNthNextPlayer(-1).getLaidOut());
		assertEquals(2, roundState.getTable().getSize());
		assertEquals(14 + 7, roundState.getNthNextPlayer(-1).getHand()
				.getSize());
	}

	/** */
	@Test
	public void laidOutJustChangedTable() {
		roundControl.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}
		// Fake Turn to put stones on the table
		IHand hand = roundState.getActivePlayer().getHand();
		hand.drop(blueFive, new Position(0, 0));
		hand.drop(blueSix, new Position(0, 0));
		hand.drop(blueSeven, new Position(0, 0));

		hand.drop(redFour, new Position(0, 0));
		hand.drop(blueFour, new Position(0, 0));
		hand.drop(blackFour, new Position(0, 0));
		hand.drop(orangeFour, new Position(0, 0));
		view.startTurnEvent.emit();

		view.handPanel.stoneClickEvent.emit(redFour, false);
		view.handPanel.stoneClickEvent.emit(blueFour, true);
		view.handPanel.stoneClickEvent.emit(blackFour, true);
		view.handPanel.stoneClickEvent.emit(orangeFour, true);

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.handPanel.stoneClickEvent.emit(blueFive, false);
		view.handPanel.stoneClickEvent.emit(blueSix, true);
		view.handPanel.stoneClickEvent.emit(blueSeven, true);

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.playerPanel.endTurnEvent.emit();
		assertEquals(2, roundState.getTable().getSize());

		view.startTurnEvent.emit();
		view.tablePanel.stoneClickEvent.emit(blueFour, false);
		view.tablePanel.stoneClickEvent.emit(blueFive, true);
		view.tablePanel.stoneClickEvent.emit(blueSix, true);
		view.tablePanel.stoneClickEvent.emit(blueSeven, true);

		assertFalse(roundState.getActivePlayer().getLaidOut());

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.playerPanel.endTurnEvent.emit();
		view.acknowledgeInvalidEvent.emit();
		assertFalse(roundState.getNthNextPlayer(-1).getLaidOut());
		assertEquals(2, roundState.getTable().getSize());
		assertEquals(14 + 3, roundState.getNthNextPlayer(-1).getHand()
				.getSize());
	}

	/** Threshold=30 */
	@Test
	public void laidOutValid() {
		roundControl.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}
		// Fake Turn to put stones on the table
		IHand hand = roundState.getActivePlayer().getHand();
		hand.drop(blueOne, new Position(0, 0));
		hand.drop(blueTwo, new Position(0, 0));
		hand.drop(blueThree, new Position(0, 0));

		hand.drop(redSeven, new Position(0, 0));
		hand.drop(blueSeven, new Position(0, 0));
		hand.drop(blackSeven, new Position(0, 0));
		hand.drop(orangeSeven, new Position(0, 0));
		view.startTurnEvent.emit();

		view.handPanel.stoneClickEvent.emit(redSeven, false);
		view.handPanel.stoneClickEvent.emit(blueSeven, true);
		view.handPanel.stoneClickEvent.emit(blackSeven, true);
		view.handPanel.stoneClickEvent.emit(orangeSeven, true);

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.handPanel.stoneClickEvent.emit(blueOne, false);
		view.handPanel.stoneClickEvent.emit(blueTwo, true);
		view.handPanel.stoneClickEvent.emit(blueThree, true);

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.playerPanel.endTurnEvent.emit();
		assertTrue(roundState.getNthNextPlayer(-1).getLaidOut());
		assertEquals(2, roundState.getTable().getSize());
		assertEquals(14, roundState.getNthNextPlayer(-1).getHand().getSize());
	}

	/** */
	@Test
	public void testDeal() {
		testRound.deal();
		checkCorrectlyDealt();
	}

	/** */
	@Test
	public void testStartRound() {
		testRound.startRound();
		checkCorrectlyDealt();

		checkTurnStartSetUp();
	}

	/** */
	@Test
	public void testTableDisplay() {
		testRound.startRound();
		checkCorrectlyDealt();
		view.startTurnEvent.emit();
		checkTableDisplay();
		view.getPlayerPanel().endTurnEvent.emit();
	}

	/** */
	@Test
	public void testTableValidTableChanged() {
		testRoundState.players.get(0).setLaidOut(true);
		testRound.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}
		MockTable oldTable = testRoundState.table;
		oldTable.sets.clear();

		testTable.valid = true;
		testTable.sets.clear();
		testTable.drop(
				new StoneSet(Arrays.asList(blueEight, blackEight, redEight,
						orangeEight)), new Position(0, 0));
		oldTable.clonedTable = testTable;

		view.startTurnEvent.emit();
		assertSame(BottomPanelType.HUMAN_HAND_PANEL, view.bottomPanelType);

		resetTurnStart();
		view.getPlayerPanel().endTurnEvent.emit();

		assertSame(testTable, testRoundState.setTable);
		assertEquals(14, testRoundState.players.get(0).getHand().getSize());
		assertEquals(1, testRoundState.activePlayer);

		checkTurnStartSetUp();
	}

	/** */
	@Test
	public void testTableInvalidTableChanged() {
		testRound.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}
		MockTable oldTable = testRoundState.table;
		oldTable.sets.clear();

		testTable.valid = false;
		testTable.sets.clear();
		testTable.drop(
				new StoneSet(Arrays.asList(blueEight, blackEight, redEight,
						blackFour)), new Position(0, 0));
		oldTable.clonedTable = testTable;

		view.startTurnEvent.emit();
		assertSame(BottomPanelType.HUMAN_HAND_PANEL, view.bottomPanelType);

		resetTurnStart();
		view.getPlayerPanel().endTurnEvent.emit();
		view.acknowledgeInvalidEvent.emit();

		assertNull(testRoundState.setTable);
		assertEquals(1, testRoundState.activePlayer);
		assertEquals(14 + 4 + 3, testRoundState.players.get(0).hand.getSize());
		checkTurnStartSetUp();
	}

	/** */
	@Test
	public void testTableValidTabelUnchanged() {
		testRoundState.players.get(0).setLaidOut(true);
		testRound.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}
		MockTable oldTable = testRoundState.table;
		oldTable.sets.clear();

		testTable.valid = true;
		testTable.sets.clear();
		oldTable.clonedTable = testTable;

		view.startTurnEvent.emit();
		assertSame(BottomPanelType.HUMAN_HAND_PANEL, view.bottomPanelType);
		resetTurnStart();
		view.getPlayerPanel().endTurnEvent.emit();

		assertSame(testTable, testRoundState.setTable);
		assertEquals(14 + 1, testRoundState.players.get(0).hand.getSize());
		assertEquals(1, testRoundState.activePlayer);

		checkTurnStartSetUp();
	}

	/** */
	@Test
	public void testTableInvalidTabelReordered() {
		testRoundState.players.get(1).setLaidOut(true);
		testRound.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}
		MockTable oldTable = testRoundState.table;
		oldTable.sets.clear();
		oldTable.drop(
				new StoneSet(Arrays.asList(blueEight, blackEight, redEight,
						orangeEight)), new Position(0, 0));

		testTable.valid = false;
		testTable.sets.clear();

		oldTable.clonedTable = testTable;
		testTable.drop(new StoneSet(Arrays.asList(blueEight, blackEight)),
				new Position(0, 0));
		testTable.drop(new StoneSet(Arrays.asList(redEight, orangeEight)),
				new Position(0, 0));
		view.startTurnEvent.emit();
		assertSame(BottomPanelType.HUMAN_HAND_PANEL, view.bottomPanelType);

		resetTurnStart();
		view.getPlayerPanel().endTurnEvent.emit();
		view.acknowledgeInvalidEvent.emit();

		assertNull(testRoundState.setTable);
		assertEquals(14 + 3, testRoundState.players.get(0).hand.getSize());
		assertEquals(1, testRoundState.activePlayer);

		checkTurnStartSetUp();
	}

	/** */
	@Test
	public void testWinning() {
		testRound.getEndOfRoundEvent().add(new IListener1<Score>() {
			@Override
			public void handle(Score roundScore) {
				roundEnded = true;
			}
		});

		testRound.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}
		MockTable oldTable = testRoundState.table;
		testTable.valid = true;
		oldTable.clonedTable = testTable;

		IHand hand = testRoundState.getActivePlayer().getHand();

		hand.drop(redEight, new Position(0, 0));
		hand.drop(blueEight, new Position(0, 0));
		hand.drop(blackEight, new Position(0, 0));
		hand.drop(orangeEight, new Position(0, 0));
		view.startTurnEvent.emit();
		assertSame(BottomPanelType.HUMAN_HAND_PANEL, view.bottomPanelType);

		view.handPanel.stoneClickEvent.emit(redEight, false);
		view.handPanel.stoneClickEvent.emit(blueEight, true);
		view.handPanel.stoneClickEvent.emit(blackEight, true);
		view.handPanel.stoneClickEvent.emit(orangeEight, true);

		view.tablePanel.clickEvent.emit(new Position(0, 0));

		view.playerPanel.endTurnEvent.emit();

		for (int i = 0; i < 3; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}

		testRoundState.getActivePlayer().setHand(new Hand());
		view.startTurnEvent.emit();

		assertFalse(roundEnded);
		view.playerPanel.endTurnEvent.emit();
		assertTrue(roundEnded);
	}

	/** */
	@Test
	public void heapIsEmpty() {
		roundControl.getEndOfRoundEvent().add(new IListener1<Score>() {
			@Override
			public void handle(Score roundScore) {
				roundEnded = true;
			}
		});

		roundState.getStoneHeap().drawStones(106 - 14 * 4 - 1);

		roundControl.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}

		IPlayer player1 = roundState.getActivePlayer();

		view.startTurnEvent.emit();
		view.playerPanel.endTurnEvent.emit(); // player 1 draws a card here

		assertSame(player1, roundState.getNthNextPlayer(-1));

		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			view.playerPanel.endTurnEvent.emit();
		}
		assertTrue(roundEnded);
	}

	/** */
	@Test
	public void testScore() {

		testRound.getEndOfRoundEvent().add(new IListener1<Score>() {
			@Override
			public void handle(Score score) {
				roundEnded = true;
				roundScore = score;
			}
		});

		testRound.startRound();

		for (int i = 0; i < 4; i++) {
			testRoundState.players.get(i).hand = new Hand();
		}

		testRoundState.players.get(0).laidOut = true;
		testRoundState.players.get(0).hand.drop(new Stone(1, RED),
				new Position(0, 0));
		testRoundState.players.get(0).hand.drop(new Stone(2, RED),
				new Position(0, 0));
		testRoundState.players.get(1).laidOut = true;
		testRoundState.players.get(1).hand.drop(new Stone(RED), new Position(0,
				0));
		testRoundState.players.get(2).laidOut = false;
		testRoundState.players.get(2).hand.drop(new Stone(9, RED),
				new Position(0, 0));
		testRoundState.players.get(2).hand.drop(new Stone(10, RED),
				new Position(0, 0));
		testRoundState.players.get(2).hand.drop(new Stone(11, RED),
				new Position(0, 0));
		testRoundState.players.get(3).laidOut = true;

		testRound.endOfRound();
		assertTrue(roundEnded);

		for (int i = 0; i < 4; i++) {
			assertTrue(roundScore.getWinners().get(i) == (i == 3));
		}
		assertEquals(-3, (int) roundScore.getPoints().get(0));
		assertEquals(-50, (int) roundScore.getPoints().get(1));
		assertEquals(-200, (int) roundScore.getPoints().get(2));
		assertEquals(253, (int) roundScore.getPoints().get(3));
	}

	/** */
	@Test
	public void testScoreWhenHeapEmpty() {

		testRound.getEndOfRoundEvent().add(new IListener1<Score>() {
			@Override
			public void handle(Score score) {
				roundEnded = true;
				roundScore = score;
			}
		});

		testRound.startRound();

		for (int i = 0; i < 4; i++) {
			testRoundState.players.get(i).hand = new Hand();
		}

		testRoundState.players.get(0).laidOut = true;
		testRoundState.players.get(0).hand.drop(new Stone(1, RED),
				new Position(0, 0));
		testRoundState.players.get(0).hand.drop(new Stone(2, RED),
				new Position(0, 0));
		testRoundState.players.get(1).laidOut = true;
		testRoundState.players.get(1).hand.drop(new Stone(3, RED),
				new Position(0, 0));
		testRoundState.players.get(2).laidOut = true;
		testRoundState.players.get(2).hand.drop(new Stone(3, BLUE),
				new Position(0, 0));
		testRoundState.players.get(3).laidOut = false;
		testRoundState.players.get(3).hand.drop(new Stone(13, RED),
				new Position(0, 0));

		testRound.endOfRound();
		assertTrue(roundEnded);

		assertFalse(roundScore.getWinners().get(0));
		assertTrue(roundScore.getWinners().get(1));
		assertTrue(roundScore.getWinners().get(2));
		assertFalse(roundScore.getWinners().get(3));

		assertEquals(-3, (int) roundScore.getPoints().get(0));
		assertEquals(-3, (int) roundScore.getPoints().get(1));
		assertEquals(-3, (int) roundScore.getPoints().get(2));
		assertEquals(-100, (int) roundScore.getPoints().get(3));
	}

	/** */
	@Test
	public void testInspectOnly() {
		testRound.startRound();
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			assertTrue(view.tablePanel.clickEvent.listeners.isEmpty());
			view.playerPanel.endTurnEvent.emit();
			assertEquals(14, testRoundState.players.get(i).hand.getSize());
		}
		for (int i = 0; i < 4; i++) {
			view.startTurnEvent.emit();
			assertFalse(view.tablePanel.clickEvent.listeners.isEmpty());
			view.playerPanel.endTurnEvent.emit();
			view.acknowledgeInvalidEvent.emit();
			assertFalse(14 == testRoundState.players.get(i).hand.getSize());
		}
	}

	/** */
	@Test
	public void testRedeal() {
		testRound.getRestartRoundEvent().add(new IListener1<PlayerSettings>() {
			@Override
			public void handle(PlayerSettings player) {
				roundRestarted = true;
			}
		});
		testRound.startRound();
		view.startTurnEvent.emit();
		view.playerPanel.redealEvent.emit();
		assertTrue(roundRestarted);
	}

	/** */
	@Test
	public void testRedealDisallowed() {
		testRound.startRound();
		Hand hand = new Hand();
		hand.drop(new Stone(1, RED), new Position(0, 0));
		hand.drop(new Stone(1, BLACK), new Position(0, 0));
		hand.drop(new Stone(1, BLUE), new Position(0, 0));
		testRoundState.players.get(0).hand = hand;
		view.startTurnEvent.emit();
		assertEquals(TurnMode.INSPECT_ONLY, view.playerPanel.turnMode);
		for (int i = 0; i < 4; i++) {
			view.playerPanel.endTurnEvent.emit();
			view.startTurnEvent.emit();
		}
		assertEquals(TurnMode.NORMAL_TURN, view.playerPanel.turnMode);
	}

	/** */
	@Test
	public void testRedealAllowed() {
		testRound.startRound();
		Hand hand = new Hand();
		for (int i = 0; i < 6; i++) {
			hand.drop(new Stone(i / 2 + 1, RED), new Position(0, 0));
		}
		testRoundState.players.get(0).hand = hand;
		view.startTurnEvent.emit();
		assertEquals(TurnMode.MAY_REDEAL, view.playerPanel.turnMode);
		for (int i = 0; i < 4; i++) {
			view.playerPanel.endTurnEvent.emit();
			view.startTurnEvent.emit();
		}
		assertEquals(TurnMode.NORMAL_TURN, view.playerPanel.turnMode);
	}

	/** */
	@Test
	public void testPauseAndContinue() {
		testRound.startRound();
		testRound.startTurn();
		view.startTurnEvent.emit();
		view.pauseEvent.emit();
		assertTrue(view.pauseModeEnabled);
		view.endPauseEvent.emit();
		assertFalse(view.pauseModeEnabled);
	}
}
