//
//  EndOfGameDialog.java
//  cribbage
//
//  Created by Nathan Haug on Wed Apr 28 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

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

public class EndOfGameDialog extends JDialog implements ActionListener, KeyListener {

	private JLabel winnerLabel;
	private JLabel scoresLabel;
	private JPanel buttonPanel;
	private JPanel scoresPanel;
	private JPanel mainPanel;
	private Player players[];
	private JButton btNewGame;
	private JButton btQuit;
	
	private GUIWaiter waiter;
	private Cribbage gameManager;
	private CribbageWindow gameWindow;
	
	private boolean gameOn;

	public EndOfGameDialog (CribbageWindow gameWindow, Cribbage gameManager, int winner) {
		super(gameWindow, "End of Game", true); // Parent, title, modal
		
		this.gameManager = gameManager;
		this.gameWindow = gameWindow;
		
		mainPanel = new JPanel();
		BorderLayout mainLayout = new BorderLayout();
		mainPanel.setLayout(mainLayout);
		mainPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
		
		scoresPanel = new JPanel();
		BoxLayout scoresLayout = new BoxLayout(scoresPanel, BoxLayout.Y_AXIS);
		scoresPanel.setLayout(scoresLayout);
		
		buttonPanel = new JPanel();
		BoxLayout buttonLayout = new BoxLayout(buttonPanel,BoxLayout.X_AXIS);
		buttonPanel.setLayout(buttonLayout);
		
		// Add the Winner Label
		players = gameManager.getPlayers();
		String winnerText = players[winner].getName() + " wins the game with " +
							players[winner].getScore() + " points!";
		winnerLabel = new JLabel (winnerText);
		scoresPanel.add(winnerLabel);
		
		// Show the final scores
		String scoresText = "Final Scores:";
		scoresLabel = new JLabel (scoresText);
		scoresPanel.add(scoresLabel);
		for (int i=0; i < gameManager.getNumPlayers(); i++) {
			scoresText = "      " + players[i].getName() + " with " + players[i].getScore();
			if (players[i].getScore() < 91)
				scoresText += " (ouch, skunked!)\n";
			else if (players[i].getScore() < 61)
				scoresText += " (get outta town, double skunked!!)\n";

			scoresLabel = new JLabel (scoresText);
			scoresPanel.add(scoresLabel);
		}
		
		// Define our buttons
		buttonPanel.add(Box.createHorizontalGlue());
		// Okay Button
		btNewGame = new JButton( "New Game" );
		btNewGame.addKeyListener( this );
		btNewGame.addActionListener( this );
		buttonPanel.add( btNewGame );
		
		// Cancel Button
		btQuit = new JButton( "Quit" );
		btQuit.addKeyListener( this );
		btQuit.addActionListener( this );
		buttonPanel.add( btQuit );
		
		mainPanel.add(scoresPanel,BorderLayout.CENTER);
		mainPanel.add(buttonPanel,BorderLayout.SOUTH);
		this.getContentPane().add(mainPanel);
		
		pack();
		
		setLocationRelativeTo(gameWindow);
		
		setResizable(false);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addKeyListener( this );
	}
	
	public void actionPerformed( ActionEvent e ) {
		if ( e.getSource() == btNewGame ) 
			newGame();
		
		if ( e.getSource() == btQuit ) 
			quit();
	}
   
	public void keyPressed(KeyEvent e) {
		int code = e.getKeyCode();
		if(code == KeyEvent.VK_ESCAPE)
			quit();
		else if(code == KeyEvent.VK_ENTER)
			newGame();
	}

	// The rest of the KeyListener bodies
	public void keyReleased(KeyEvent e) {}
	public void keyTyped(KeyEvent e) {}
	
	public boolean gameOn() {
		return gameOn;
	}
	
	private void quit() {
		gameOn = false;
		this.setVisible(false);
	}
	
	private void newGame() {
		gameOn = true;
		this.setVisible(false);
	}
	
}
