//
//  NewGameWindow.java
//  cribbage
//
//  Created by Nathan Haug on Thu Apr 22 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//
//
//  CribbageWindow.java
//  cribbage
//
//  Created by Nathan Haug on Thu Apr 22 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class NewGameWindow extends JDialog implements ActionListener, KeyListener, ItemListener
{
   JPanel mainPanel;
   JPanel optionPanel;
   JPanel buttonPanel;
   JComboBox cmbCombo0;
   JCheckBox cbGameMode;
   JButton btOkay;
   JButton btCancel;
   JLabel lbQuestion;
   JTextField tfPlayerName;
   JLabel lbName;
   
   int numPlayers;
   int gameMode = 1;
   String playerName;
   
   // Constructor
	public NewGameWindow(CribbageWindow parent) {
		// Construct the window
		super(parent,"New Game?",true); // (Frame parent, String title, boolean modalMode)
		
		mainPanel = new JPanel();
		BorderLayout mainLayout = new BorderLayout();
		mainPanel.setLayout(mainLayout);
		mainPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		mainPanel.setPreferredSize(new Dimension(250,150));
		mainPanel.setMaximumSize(new Dimension(250,150));
		
		optionPanel = new JPanel();
		BoxLayout optionLayout = new BoxLayout(optionPanel, BoxLayout.Y_AXIS);
		optionPanel.setLayout(optionLayout);
		
		buttonPanel = new JPanel();
		BoxLayout buttonLayout = new BoxLayout(buttonPanel,BoxLayout.X_AXIS);
		buttonPanel.setLayout(buttonLayout);
		

		// How many players label
		lbQuestion = new JLabel( "How many players?" );
		optionPanel.add( lbQuestion );
		
		// The Number of players drop-down menu
		String []sCombo0 = {"Three Player Game","Two Player Game"};
		cmbCombo0 = new JComboBox( sCombo0 );
		cmbCombo0.addKeyListener( this );
		optionPanel.add( cmbCombo0 );

		// AI mode checkbox
		cbGameMode = new JCheckBox( "AI versus Human" );
		cbGameMode.setSelected( true );
		cbGameMode.addKeyListener( this );
		cbGameMode.addItemListener(this);;
		optionPanel.add( cbGameMode );

		// Player Name label
		lbName = new JLabel( "Player Name:" );
		optionPanel.add( lbName );

		// Player Name Text Input
		tfPlayerName = new JTextField();
		tfPlayerName.addKeyListener( this );
		optionPanel.add( tfPlayerName );
		
		// Some horizontal space
		buttonPanel.add(Box.createHorizontalGlue());
		
		// Cancel Button
		btCancel = new JButton( "Cancel" );
		btCancel.addKeyListener( this );
		btCancel.addActionListener( this );
		//buttonPanel.add( btCancel );
		
		// Okay Button
		btOkay = new JButton( "Okay" );
		btOkay.addKeyListener( this );
		btOkay.addActionListener( this );
		buttonPanel.add( btOkay );

		tfPlayerName.setAlignmentX(Component.LEFT_ALIGNMENT);
		cbGameMode.setAlignmentX(Component.LEFT_ALIGNMENT);
		cmbCombo0.setAlignmentX(Component.LEFT_ALIGNMENT);
		lbQuestion.setAlignmentX(Component.LEFT_ALIGNMENT);
		lbName.setAlignmentX(Component.LEFT_ALIGNMENT);

		mainPanel.add(optionPanel,BorderLayout.NORTH);
		mainPanel.add(buttonPanel,BorderLayout.SOUTH);
		getContentPane().add(mainPanel);

		pack();
		
		// Place in the center of the parent window
		Dimension parentSize = parent.getSize();
		Dimension windowSize = this.getSize();
		setLocation(Math.max(0,(parentSize.width -windowSize.width)), 
		Math.max(0,(parentSize.height-windowSize.height)));
		
		this.setResizable(false);
		this.setVisible(true);
		this.addKeyListener( this );
		
		// Set the focus to the name text box
		addWindowListener( new WindowAdapter() {
		public void windowOpened( WindowEvent e ){
			tfPlayerName.requestFocus();
		}
		} );
   } 
   
   // Accessors
	public int getNumPlayers() {
		return numPlayers;
	}
	public int getGameMode() {
		return gameMode;
	}
	public String getPlayerName() {
		if (playerName.equals(""))
			return "Player 0";
		else
			return playerName;
	}

	public void actionPerformed( ActionEvent e ) {
		if ( e.getSource() == btOkay ) 
			okayAction();
		
		if ( e.getSource() == btCancel ) 
			cancelAction();
	}
   
	public void keyPressed(KeyEvent e) {
		int code = e.getKeyCode();
		if(code == KeyEvent.VK_ESCAPE)
			cancelAction();
		else if(code == KeyEvent.VK_ENTER)
			okayAction();
	}

	// The rest of the KeyListener bodies
	public void keyReleased(KeyEvent e) {}
	public void keyTyped(KeyEvent e) {}
	
	public void itemStateChanged(ItemEvent e) {
		if (e.getStateChange() == ItemEvent.SELECTED) {
			gameMode = 1; // Human vs AI
			tfPlayerName.setVisible(true); // Make sure the name field is enabled
			lbName.setVisible(true);
		}
		else {
			gameMode = 2; // AI vs AI
			tfPlayerName.setVisible(false); // Disable the name field
			lbName.setVisible(false);
		}
	}
	
	
	private void cancelAction() {
		this.setVisible(false);	}
	
	private void okayAction() {
		if (cmbCombo0.getSelectedIndex() == 0)
			numPlayers = 3;
		else if (cmbCombo0.getSelectedIndex() == 1)
			numPlayers = 2;
		playerName = tfPlayerName.getText();
		this.setVisible(false);		
	}


}





