package jrummikub.control;

import jrummikub.model.GameState;
import jrummikub.util.IListener;
import jrummikub.view.IView;

/**
 * Controls a Game, at some point including all Rounds, starts new Rounds
 */
public class GameControl {
	final static int HAND_HEIGHT = 2;
	final static int HAND_WIDTH = 14;
	private IView view;
	private RoundControl roundControl;

	/**
	 * Constructor
	 * 
	 * @param view the view
	 */
	public GameControl(IView view) {
		this.view = view;
		view.getPlayerPanel().getHandPanel().setHandHeight(HAND_HEIGHT);
		view.getPlayerPanel().getHandPanel().setHandWidth(HAND_WIDTH);

		view.getNewGameEvent().add(new IListener() {
			@Override
			public void handle() {
				startRound();
			}
		});
		view.getQuitEvent().add(new IListener() {
			@Override
			public void handle() {
				quitProgram();
			}
		});
	}

	/**
	 * Game gets started by initializing the first Round
	 */
	public void startGame() {
		startRound();
	}

	private void startRound() {
		if (roundControl != null) {
			return;
		}

		GameState gameState = new GameState();

		roundControl = new RoundControl(gameState, view);
		roundControl.getEndRoundEvent().add(new IListener() {

			@Override
			public void handle() {
				roundControl = null;
			}
		});

		roundControl.startRound();
	}

	private void quitProgram() {
		System.exit(0);
	}

}
