//
//  CribbageWindow.java
//  cribbage
//
//  Created by Nathan Haug on Wed Jun 09 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;


public class CribbageWindow extends JFrame implements ActionListener{
	private CribbagePanel gamePanel;
	
	private JMenuBar		menuBar;
	private JMenu			fileMenu;
	private JMenuItem		menuItem;
	
	private Cribbage myGame = null;
	private EndOfGameDialog endOfGameDialog;
	private NewGameWindow   newGameWindow;
	

	CribbageWindow () {
	super("Cribbage");
	
	// Define the Menu Bar
	menuBar = new JMenuBar();
	setJMenuBar(menuBar);
		
	// Define the File Menu
	fileMenu = new JMenu("File");
	fileMenu.setMnemonic(KeyEvent.VK_F);
	
	int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
	
	menuItem = new JMenuItem("New Game...");
	menuItem.setActionCommand("New Game");
	menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,mask));
	menuItem.addActionListener(this);
	fileMenu.add(menuItem);
	
	fileMenu.addSeparator();
	
	menuItem = new JMenuItem("Quit");
	menuItem.setActionCommand("Quit");
	menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,mask));
	menuItem.addActionListener(this);
	fileMenu.add(menuItem);
	
	menuBar.add(fileMenu);

	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
	setSize(600,400);

	// Place in the center of the screen
	Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
	Dimension windowSize = this.getSize();
	setLocation(Math.max(0,(screenSize.width -windowSize.width)/2), 
	Math.max(0,(screenSize.height-windowSize.height)/2));
	
	setVisible(true);
	
	}
	
	// Action Listeners
	public void actionPerformed(ActionEvent e) {
		
		if (e.getActionCommand().equals("New Game")) {
			newGame();
		}
		
		if (e.getActionCommand().equals("Quit")) {
			System.exit(0);
		}
	}
	
	public void setCribbagePanel(CribbagePanel theGamePanel) {
		getContentPane().removeAll();
		getContentPane().add(theGamePanel);
		pack();
		
		// Place in the center of the screen
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension windowSize = this.getSize();
		setLocation(Math.max(0,(screenSize.width -windowSize.width)/2), 
		Math.max(0,(screenSize.height-windowSize.height)/2));
		
		// Turn it on
		setVisible(true);
		
	}
	
	public void newGame() {
		int numOfPlayers;
		int mode;
		String playerName = "0";
		
		// Get the number of players, the game mode, and the player's name
		newGameWindow = new NewGameWindow(this);
		numOfPlayers = newGameWindow.getNumPlayers();
		mode = newGameWindow.getGameMode();
		
		// Create the Game
		if (mode == 1) {
			playerName = newGameWindow.getPlayerName();
			myGame = new Cribbage(numOfPlayers,playerName);
			setCribbagePanel(myGame.getGameWindow());
		}
		else if (mode==2)
			myGame = new Cribbage(numOfPlayers);
			setCribbagePanel(myGame.getGameWindow());
	}
	
	public void initialize() {
		boolean gameOn = true;;
		
		while (gameOn) {
			System.out.println("Game On!");
			newGame();
			
			try {
				myGame.playGame();
			}
			catch (GameOverException gameOverEx) {
				endOfGameDialog = new EndOfGameDialog(this,myGame,gameOverEx.getWinner());
				gameOn = endOfGameDialog.gameOn();
			}
		}	
	}
	
	// Testing main function
	public static void main(String[] str) {
		CribbageWindow gameWindow = new CribbageWindow();
		
		gameWindow.initialize();
		System.exit(0);
	}
}
