package jrummikub.control;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import jrummikub.util.LoginData;
import jrummikub.view.IQuitWarningPanel.QuitMode;
import jrummikub.view.MockView;

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

/**
 * Tests for the testable parts of the application control
 */
public class ApplicationControlTest {
	ApplicationControl testAppControl;
	MockView view;

	/** */
	@Before
	public void setup() {
		view = new MockView();
	}

	/** */
	@Test
	public void enableSaving() {
		assertFalse(view.savingEnabled);
		testAppControl = new ApplicationControl(view);
		testAppControl.startApplication();
		assertTrue(view.isSettingsPanelVisible);
		view.settingsPanel.startGameEvent.emit();
		assertFalse(view.isSettingsPanelVisible);
		assertTrue(view.savingEnabled);
	}

	/** */
	@Test
	public void pauseTest() {
		testAppControl = new ApplicationControl(view);
		testAppControl.startApplication();
		view.settingsPanel.startGameEvent.emit();
		view.getPlayerPanel().pauseEvent.emit();
		// assertTrue(view.isPaused);
	}

	/** */
	@Test
	public void networkTest() {
		testAppControl = new ApplicationControl(view);
		testAppControl.startApplication();

		view.networkGameEvent.emit();
		assertTrue(view.isLoginPanelVisible);
	}

	/** */
	@Test
	public void networkFromRunningGameTest() {
		testAppControl = new ApplicationControl(view);
		testAppControl.startApplication();
		view.settingsPanel.startGameEvent.emit();

		view.networkGameEvent.emit();
		assertTrue(view.isQuitWarningPanelVisible);
		assertSame(QuitMode.QUIT_GAME, view.getQuitWarningPanel().getQuitMode());
		view.getQuitWarningPanel().quitEvent.emit();
		assertTrue(view.isLoginPanelVisible);
	}

	/** */
	@Test
	public void newGameFromNetworkTest() {
		testAppControl = new ApplicationControl(view);
		testAppControl.startApplication();

		view.networkGameEvent.emit();
		assertTrue(view.isLoginPanelVisible);

		LoginData data = new LoginData("Horst", "server", "horstspw", "channel");
		view.loginPanel.loginEvent.emit(data);
		assertTrue(view.isConnectPanelVisible);
		view.menuNewGameEvent.emit();
		assertTrue(view.isQuitWarningPanelVisible);
		assertSame(QuitMode.QUIT_GAME, view.getQuitWarningPanel().getQuitMode());
		view.getQuitWarningPanel().quitEvent.emit();

		assertFalse(view.isConnectPanelVisible);
		assertTrue(view.isSettingsPanelVisible);
	}

	/** */
	@Test
	public void loadTest() {
		testAppControl = new ApplicationControl(view);
		testAppControl.startApplication();

		view.loadEvent.emit();
		assertFalse(view.isQuitWarningPanelVisible);
	}

	/** */
	@Test
	public void loadFromRunningGameTest() {
		testAppControl = new ApplicationControl(view);
		testAppControl.startApplication();
		view.settingsPanel.startGameEvent.emit();

		view.loadEvent.emit();
		assertTrue(view.isQuitWarningPanelVisible);
		assertSame(QuitMode.QUIT_GAME, view.getQuitWarningPanel().getQuitMode());
		view.getQuitWarningPanel().quitEvent.emit();
		// TODO asserte was
	}

	/** */
	@Test
	public void newGameFromRunningGameTest() {
		testAppControl = new ApplicationControl(view);
		testAppControl.startApplication();
		view.settingsPanel.startGameEvent.emit();

		view.menuNewGameEvent.emit();
		assertTrue(view.isQuitWarningPanelVisible);
		assertSame(QuitMode.QUIT_GAME, view.getQuitWarningPanel().getQuitMode());
		view.getQuitWarningPanel().cancelEvent.emit();
		assertFalse(view.isQuitWarningPanelVisible);

		view.menuNewGameEvent.emit();
		assertTrue(view.isQuitWarningPanelVisible);
		assertSame(QuitMode.QUIT_GAME, view.getQuitWarningPanel().getQuitMode());
		view.getQuitWarningPanel().quitEvent.emit();
		assertTrue(view.isSettingsPanelVisible);
	}
}
