package jrummikub.control;

import static jrummikub.model.StoneColor.BLACK;
import static jrummikub.model.StoneColor.BLUE;
import static jrummikub.model.StoneColor.RED;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import jrummikub.model.MockGameState;
import jrummikub.model.MockTable;
import jrummikub.model.Position;
import jrummikub.model.Stone;
import jrummikub.model.StoneSet;
import jrummikub.util.Pair;
import jrummikub.view.MockView;

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

/**
 * Tests for {@link RoundControl}
 */
public class RoundControlTest {
	private MockView view;
	private MockGameState testGameState;
	private RoundControl testRound;
	private MockTable newTable;

	/**
	 * For each test create a round control initialized by a mock model and view
	 */
	@Before
	public void setup() {
		view = new MockView();
		testGameState = new MockGameState();
		testRound = new RoundControl(testGameState, view);
		Stone stone = testGameState.getGameHeap().drawStone();
		testGameState.table.drop(new StoneSet(stone), new Position(5, 0));
		newTable = new MockTable();
		newTable.sets.add(testGameState.table.sets.get(0));
		testGameState.table.clonedTable = newTable;
	}

	private void checkCorrectlyDealed() {
		assertEquals(106 - testGameState.getPlayerCount() * 14
				- testGameState.table.getSize(), testGameState.getGameHeap()
				.getSize());
		for (int i = 0; i < testGameState.getPlayerCount(); i++) {
			assertEquals(14, testGameState.getNthNextPlayer(i).getHand()
					.getSize());
		}
	}

	private void checkTurnStartSetUp() {
		assertNotNull(view.currentPlayerName);
		assertNotNull(view.getTablePanel().leftPlayerName);
		assertNotNull(view.getTablePanel().topPlayerName);
		assertNotNull(view.getTablePanel().rightPlayerName);
		assertTrue(view.displayStartTurnPanel);
		assertFalse(view.startTurnEvent.listeners.isEmpty());
		assertFalse(view.displayWinPanel);
		checkTableDisplay();
	}

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

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

	private void resetTurnStart() {
		view.currentPlayerName = null;
		view.getTablePanel().leftPlayerName = null;
		view.getTablePanel().topPlayerName = null;
		view.getTablePanel().rightPlayerName = null;
		view.displayStartTurnPanel = false;
	}

	/** */
	@Test
	public void testDealStone() {
		testRound.deal();
		checkCorrectlyDealed();
		for (int i = 0; i < 28 - 14; i++) {
			testRound.dealStone();
		}
		assertEquals(28, testGameState.getActivePlayer().getHand().getSize());
	}

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

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

		checkTurnStartSetUp();
	}

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

	/** */
	@Test
	public void testTableValidHandChanged() {
		testRound.startRound();
		MockTable oldTable = testGameState.table;
		newTable.valid = true;
		oldTable.clonedTable = newTable;

		view.startTurnEvent.emit();
		assertFalse(view.displayStartTurnPanel);
		testGameState.players.get(0).hand.stones.remove(0);
		resetTurnStart();
		view.getPlayerPanel().endTurnEvent.emit();

		assertSame(newTable, testGameState.setTable);
		assertEquals(1, testGameState.activePlayer);

		checkTurnStartSetUp();
	}

	/** */
	@Test
	public void testTableInvalidHandChanged() {
		testRound.startRound();
		MockTable oldTable = testGameState.table;
		newTable.valid = false;
		oldTable.clonedTable = newTable;

		view.startTurnEvent.emit();
		assertFalse(view.displayStartTurnPanel);
		Stone stone = testGameState.players.get(0).hand.stones.remove(0)
				.getFirst();
		newTable.drop(new StoneSet(stone), new Position(0, 0));
		resetTurnStart();
		view.getPlayerPanel().endTurnEvent.emit();

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

	/** */
	@Test
	public void testTableValidHandUnchanged() {
		testRound.startRound();
		MockTable oldTable = testGameState.table;
		newTable.valid = true;
		oldTable.clonedTable = newTable;

		view.startTurnEvent.emit();
		assertFalse(view.displayStartTurnPanel);
		resetTurnStart();
		view.getPlayerPanel().endTurnEvent.emit();

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

		checkTurnStartSetUp();
	}

	/** */
	@Test
	public void testTableInvalidHandUnchanged() {
		testRound.startRound();
		MockTable oldTable = testGameState.table;
		newTable.valid = false;
		oldTable.clonedTable = newTable;

		view.startTurnEvent.emit();
		assertFalse(view.displayStartTurnPanel);
		resetTurnStart();
		view.getPlayerPanel().endTurnEvent.emit();

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

		checkTurnStartSetUp();
	}

	/** */
	@Test
	public void testWinning() {
		testRound.startRound();
		MockTable oldTable = testGameState.table;
		newTable.valid = true;
		oldTable.clonedTable = newTable;

		view.startTurnEvent.emit();
		assertFalse(view.displayStartTurnPanel);
		Stone stone = testGameState.players.get(0).hand.stones.remove(0)
				.getFirst();
		newTable.drop(new StoneSet(stone), new Position(0, 0));
		testGameState.players.get(0).hand.stones.clear();
		resetTurnStart();
		view.getPlayerPanel().endTurnEvent.emit();

		assertTrue(view.displayWinPanel);
	}

	/** */
	@Test
	public void testTableDifference() {
		MockTable oldTable = new MockTable();
		MockTable newTable = new MockTable();
		Stone blueOne = new Stone(1, BLUE);
		Stone redOne = new Stone(1, RED);
		Stone blackOne = new Stone(1, BLACK);
		Stone blueTwo = new Stone(2, BLUE);
		Stone blueThree = new Stone(3, BLUE);
		Stone blueFour = new Stone(4, BLUE);
		StoneSet oldSet1 = new StoneSet(
				Arrays.asList(blueOne, redOne, blackOne));
		StoneSet oldSet2 = new StoneSet(blueTwo);
		oldTable.drop(oldSet1, new Position(0, 0));
		oldTable.drop(oldSet2, new Position(0, 0));
		StoneSet newSet1 = new StoneSet(Arrays.asList(blueOne, blueTwo,
				blueFour));
		StoneSet newSet2 = new StoneSet(Arrays.asList(redOne, blackOne,
				blueThree));
		newTable.drop(newSet1, new Position(0, 0));
		newTable.drop(newSet2, new Position(0, 0));

		Set<Stone> expectedStones = new HashSet<Stone>();
		expectedStones.add(blueThree);
		expectedStones.add(blueFour);

		Set<Stone> stones = RoundControl.tableDifference(oldTable, newTable);

		assertTrue(expectedStones.containsAll(stones));
		assertTrue(stones.containsAll(expectedStones));
	}
}