package jrummikub.control.network;

import jrummikub.control.GameControl;
import jrummikub.control.RoundControl;
import jrummikub.control.SaveControl;
import jrummikub.model.GameSettings;
import jrummikub.model.IRoundState;
import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.Event;
import jrummikub.util.IEvent;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.view.IView;
import jrummikub.view.IView.BottomPanelType;

/**
 * Class controlling network games
 */
public class NetworkGameControl extends GameControl {
	private IConnectionControl connectionControl;
	private boolean host;
	private Event backEvent = new Event();

	/**
	 * Creates new network game control
	 * 
	 * @param gameSettings
	 *          current game settings
	 * @param saveControl
	 *          if there should ever be saving in network mode
	 * @param view
	 *          the view
	 * @param connectionControl
	 *          the current connection
	 * @param host
	 *          of the current game
	 */
	public NetworkGameControl(GameSettings gameSettings, SaveControl saveControl,
			final IView view, IConnectionControl connectionControl, boolean host) {
		super(gameSettings, saveControl, view);

		this.connectionControl = connectionControl;
		this.host = host;

		connections.add(connectionControl.getParticipantLeftEvent().add(
				new IListener1<String>() {
					@Override
					public void handle(String nickname) {
						if (NetworkGameControl.this.gameSettings == null) {
							return;
						}

						for (PlayerSettings s : NetworkGameControl.this.gameSettings
								.getPlayerList()) {
							if (s.getName().equals(nickname) && s.getType() == Type.NETWORK) {
								connectionLost();
								return;
							}
						}
					}
				}));
	}

	/**
	 * The back event is emitted when the network game has ended and the player
	 * wants to return to the game list
	 * 
	 * @return the event
	 */
	public IEvent getBackEvent() {
		return backEvent;
	}

	@Override
	protected void startRound() {
		connections.add(connectionControl.getRoundStartEvent().add(new IListener() {
			@Override
			public void handle() {
				NetworkGameControl.super.startRound();
			}
		}));

		if (host) {
			connectionControl.startRound();
		}
	}

	@Override
	protected IRoundState createRoundState() {
		return host ? super.createRoundState() : null;
	}

	@Override
	protected RoundControl createRoundControl(IRoundState roundState) {
		return new NetworkRoundControl(roundState, view, connectionControl, host);
	}

	@Override
	protected void showWinPanel() {
		view.setBottomPanel(host ? BottomPanelType.WIN_PANEL
				: BottomPanelType.NETWORK_WIN_PANEL);
	}

	private void connectionLost() {
		abortGame();

		connections.add(view.getNewGameEvent().add(new IListener() {
			@Override
			public void handle() {
				abortGame();
				backEvent.emit();
			}
		}));
		connections.add(view.getEndProgramEvent().add(new IListener() {
			@Override
			public void handle() {
				System.exit(0);
			}
		}));

		view.setBottomPanel(BottomPanelType.NETWORK_CONNECTION_LOST_PANEL);
	}
}
