package jrummikub.control.network;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import jrummikub.model.PlayerSettings;
import jrummikub.model.PlayerSettings.Type;
import jrummikub.util.Connection;
import jrummikub.util.Event;
import jrummikub.util.GameData;
import jrummikub.util.IEvent;
import jrummikub.util.IListener;
import jrummikub.util.IListener2;
import jrummikub.view.ISettingsPanel;
import jrummikub.view.ISettingsPanel.SettingsMode;
import jrummikub.view.IView;

/**
 * Abstract class for network game controls in between choosing and starting a
 * game
 */
public abstract class AbstractGameBeginControl {
	protected List<Connection> connections = new ArrayList<Connection>();
	protected GameData gameData;
	protected IConnectionControl connectionControl;
	protected IView view;
	protected Event backEvent = new Event();

	/**
	 * Create a new game begin control
	 * 
	 * @param connection
	 *          connection control for messages and events
	 * @param view
	 *          the view
	 * @param gameData
	 *          game data of chosen game
	 * @param settingsMode
	 *          mode of settings panel
	 */
	public AbstractGameBeginControl(IConnectionControl connection, IView view,
			final GameData gameData, SettingsMode settingsMode) {
		this.connectionControl = connection;
		this.view = view;
		this.gameData = gameData;

		view.getSettingsPanel().setSettingsMode(settingsMode);
		view.getSettingsPanel().enableAddPlayerButton(false);
		updateSettingsPanel();

		addViewListeners(view, gameData);

		connections.add(connectionControl.getChangeColorEvent().add(
				new IListener2<String, Color>() {
					@Override
					public void handle(String sender, Color color) {
						List<PlayerSettings> players = gameData.getGameSettings()
								.getPlayerList();
						for (PlayerSettings s : players) {
							if (s.getName().equals(sender) && s.getType() == Type.NETWORK) {
								s.setColor(color);
								break;
							}
						}
						updateSettingsPanel();
					}
				}));
	}

	private void addViewListeners(IView view, final GameData gameData) {
		connections.add(view.getSettingsPanel().getBackEvent().add(new IListener() {
			@Override
			public void handle() {
				goBack();
			}
		}));
		connections.add(view.getSettingsPanel().getChangePlayerColorEvent()
				.add(new IListener2<Integer, Color>() {
					@Override
					public void handle(Integer i, Color color) {
						for (PlayerSettings player : gameData.getGameSettings()
								.getPlayerList()) {
							if (player.getColor() == color) {
								return;
							}
						}
						PlayerSettings player = gameData.getGameSettings().getPlayerList()
								.get(i);
						if (player.getType() != Type.HUMAN) {
							return;
						}
						player.setColor(color);
						connectionControl.changeColor(color);
						updateSettingsPanel();
					}
				}));
	}

	protected abstract void goBack();

	/**
	 * The back event is emitted when the player wants to go back to the previous
	 * control and panel
	 * 
	 * @return the event
	 */
	public IEvent getBackEvent() {
		return backEvent;
	}

	protected void abort() {
		view.showSettingsPanel(false);
		for (Connection c : connections) {
			c.remove();
		}
	}

	protected void updateSettingsPanel() {
		view.getSettingsPanel().setGameSettings(gameData.getGameSettings());

		Set<Color> colors = new HashSet<Color>(
				Arrays.asList(ISettingsPanel.PLAYER_COLORS));

		for (PlayerSettings player : gameData.getGameSettings().getPlayerList()) {
			colors.remove(player.getColor());
		}

		view.getSettingsPanel().setPlayerColors(colors);
	}
}
