//
//  CribbagePanel.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.*;
import javax.swing.border.*;


public class CribbagePanel extends JPanel implements MouseListener {
	
	private JPanel			mainPanel;
	private JPanel			superPanel;
	private JPanel			northPanel;
	private JPanel			southPanel;
	private JPanel			eastPanel;
	private BoardPanel		boardPanel;
	private HandPanel		playerPanels[] = new HandPanel[3];
	private PlayPanel		playArea;
	private JLabel			cutCard;
	
	private Cribbage		gameManager;
	private GUIWaiter		waiter;



	// Contsructor
	public CribbagePanel(Cribbage gameManager, boolean humanMode) {
		super();
		
		this.gameManager = gameManager;
		int numPlayers = gameManager.getNumPlayers();
		Player players[] = gameManager.getPlayers();
		
		// Define the "super" panel to put the main panel in
		superPanel = new JPanel();
		BorderLayout superLayout = new BorderLayout(10,0);
		superPanel.setLayout(superLayout);
		this.add(superPanel);
		
		// Define the main panel
		mainPanel = new JPanel();
		BorderLayout mainLayout = new BorderLayout(0,10);
		mainPanel.setLayout(mainLayout);
		superPanel.add(mainPanel,"Center");
		
		// Define the west (Pegging) panel
		eastPanel = new JPanel();
		boardPanel = new BoardPanel(gameManager);
		eastPanel.add(boardPanel,"Center");
		superPanel.add(eastPanel,"East");
		
		// Define the north (Opponents) panel
		northPanel = new JPanel();
		GridLayout oppLayout = new GridLayout(1,0,10,5);
		northPanel.setLayout(oppLayout);
		mainPanel.add(northPanel,"North");
		
		// Add the Opponents
		for (int i = 1; i < numPlayers; i++)
			addOpponent(players[i].getName(),i); // name,playerID
		
		// Define the Play Panel
		playArea = new PlayPanel();
		mainPanel.add(playArea,"Center");
		
		// Define the south (Human Hand & Cut Card) panel
		southPanel = new JPanel();
		southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.X_AXIS));
			// Define Human Hand
			if (humanMode)
				playerPanels[0] = new HumanHandPanel(players[0].getName(),gameManager);
			else
				playerPanels[0] = new AIHandPanel(players[0].getName());
			southPanel.add(playerPanels[0]);
			// Some glue (spacer)
			southPanel.add(Box.createHorizontalGlue());

			// Add cutCard Label
			JPanel cutCardContainer = new JPanel();
			cutCardContainer.setBorder( BorderFactory.createTitledBorder( "Cut Card" ) );
			cutCard = new JLabel(new ImageIcon("./images/_cardback.jpg"));
			cutCard.setBorder(LineBorder.createBlackLineBorder());
			cutCardContainer.add(cutCard);
			southPanel.add(cutCardContainer);
			
			// Some glue (spacer)
			southPanel.add(Box.createHorizontalGlue());
		
		mainPanel.add(southPanel,"South");
	}
	
	// Accessors
	public void playCard(Card playCard) {
		playArea.putCard(playCard);
	}
	
	public void clearPlayArea () {
		playArea.clearPlay();
		for (int i = 0; i < gameManager.getNumPlayers(); i++)
			playerPanels[i].notify("");
	}
	
	public HandPanel getHand(int playerID) {
			return playerPanels[playerID];
	}
	
	public void setCutCard (Card theCutCard) {
		// Set the new card
		ImageIcon cardImage = CardGraphics.getCardGraphic(theCutCard);
		cutCard.setIcon(cardImage);
	}
	
	public void clearCutCard () {
		// Set the new card
		ImageIcon cardBack = new ImageIcon("./images/_cardback.jpg");
		cutCard.setIcon(cardBack);
	}
	
	public void notify (int playerID,String message) {
		playerPanels[playerID].notify(message);
	}
	
	public void setPlayText(String text) {
		playArea.setPlayText(text);
	}
	
	public void addScore (int playerID, int score) {
		// Sets the score both in the players info area and on the board
		playerPanels[playerID].addScore(score);
		boardPanel.addScore(playerID,score);
	} 
	
	public void putCard (int playerID, Card theCard) {
		playerPanels[playerID].putCard(theCard);
	}
	
	public void deleteCard (int playerID, int cardNum) {
		playerPanels[playerID].deleteCard(cardNum);
	}
	
	public void show (int playerID, Hand showHand) {
		playerPanels[playerID].show(showHand);
	}
	
	public void disableCards() {
		playerPanels[0].disableCards();
	}
	
	public void enableCards() {
		playerPanels[0].enableCards();
	}
	
	public void clickWait () {
		this.disableCards();
		mainPanel.addMouseListener(this);
		
		waiter = new GUIWaiter();
		waiter.start();
		try {waiter.join();} catch (InterruptedException e) {}
	}
	
	public void cleanUp () {
		for (int i=0; i<gameManager.getNumPlayers();i++)
			playerPanels[i].clearHand();
		clearPlayArea();
		clearCutCard();
	}
	
	// Private Methods
	private HandPanel addOpponent (String name, int OppNum) {
		playerPanels[OppNum] = new AIHandPanel(name);
		northPanel.add(playerPanels[OppNum]);
		
		return playerPanels[OppNum];
	}
	
	// Mouse Listeners
	public void mouseClicked (MouseEvent e) {
		mainPanel.removeMouseListener(this);
		this.enableCards();
		waiter.setDone();
	}
	
	public void mouseEntered (MouseEvent e) {}
	public void mouseExited (MouseEvent e) {}
	public void mousePressed (MouseEvent e) {}
	public void mouseReleased (MouseEvent e) {}

}



