package jrummikub.control.network;

import jrummikub.control.RoundControl;
import jrummikub.control.turn.ITurnControl;
import jrummikub.model.IRoundState;
import jrummikub.model.ITable;
import jrummikub.util.IListener;
import jrummikub.util.IListener1;
import jrummikub.view.IView;

/**
 * Round control for network games
 */
public class NetworkRoundControl extends RoundControl {
	private IConnectionControl connectionControl;
	private boolean currentlyActive = false;
	private boolean inited = false;

	/**
	 * Creates new network round control
	 * 
	 * @param roundState
	 *          current round state
	 * @param view
	 *          the view
	 * @param connectionControl
	 *          connection control for the current connection
	 * @param startActive
	 *          true for host
	 */
	public NetworkRoundControl(IRoundState roundState, IView view,
			final IConnectionControl connectionControl, final boolean startActive) {
		super(roundState, view, false);

		this.connectionControl = connectionControl;

		connections.add(connectionControl.getRoundStateUpdateEvent().add(
				new IListener1<IRoundState>() {
					@Override
					public void handle(IRoundState state) {
						setRoundState(state);
						currentlyActive = startActive;
						updateActive();
						inited = true;
						prepareTurn();
					}
				}));
		connections.add(connectionControl.getTurnStartEvent().add(new IListener() {
			@Override
			public void handle() {
				NetworkRoundControl.super.startTurn();
			}
		}));
		connections.add(connectionControl.getNextPlayerEvent().add(new IListener() {
			@Override
			public void handle() {
				NetworkRoundControl.super.nextPlayer();
			}
		}));
	}

	private void updateActive() {
		switch (roundState.getActivePlayer().getPlayerSettings().getType()) {
			case HUMAN:
				currentlyActive = true;
				break;
			case NETWORK:
				currentlyActive = false;
				break;
		}
	}

	@Override
	protected void addTurnControlListeners(ITurnControl turnControl) {
		turnControl.getTableUpdateEvent().add(new IListener1<ITable>() {
			@Override
			public void handle(ITable table) {
				connectionControl.updateTable(table);
			}
		});
	}

	@Override
	protected ITurnControl createTurnControl() {
		if (!currentlyActive) {
			return new NetworkTurnControl(connectionControl);
		}

		return super.createTurnControl();
	}

	@Override
	protected void prepareTurn() {
		if (!inited) {
			return;
		}

		updateActive();
		super.prepareTurn();
	}

	@Override
	protected void startTurn() {
		if (currentlyActive) {
			connectionControl.startTurn();
		}
	}

	@Override
	protected void deal() {
		super.deal();

		connectionControl.updateRoundState(roundState);
	}

	@Override
	protected void nextPlayer() {
		if (currentlyActive) {
			connectionControl.nextPlayer();
		}
	}

	@Override
	protected void endOfTurn(InvalidTurnInfo invalidTurnInfo) {
		if (currentlyActive) {
			connectionControl.endTurn(roundState, invalidTurnInfo);
		}

		super.endOfTurn(invalidTurnInfo);
	}

	@Override
	protected void redeal() {
		if (currentlyActive) {
			connectionControl.redeal();
		}

		super.redeal();
	}
}
