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

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

public class BoardPanel extends JPanel{

	private JPanel boardCenter;
	private JLabel boardLeft;
	private JLabel boardRight;
	private JLabel boardTop;
	private JLabel boardBottom;
	private JLabel namesLabel;
	private JPanel mainPanel;
	private PeggingPanel peggingColumns[];
	private Player players[];
	private Cribbage gameManager;
	
	private ImageIcon boardTopIcon = new ImageIcon("./images/boardTop.gif");
	private ImageIcon boardSideIcon = new ImageIcon("./images/boardSide.gif");
	private ImageIcon boardBottomIcon = new ImageIcon("./images/boardBottom.gif");
	
	public BoardPanel (Cribbage gameManager) {
		super();
		
		this.players = gameManager.getPlayers();
		this.gameManager = gameManager;
		
		mainPanel = new JPanel();
		boardCenter = new JPanel();
		boardRight = new JLabel(boardSideIcon);
		boardLeft = new JLabel(boardSideIcon);
		boardTop = new JLabel(boardTopIcon);
		boardBottom = new JLabel(boardBottomIcon);
		peggingColumns = new PeggingPanel[3];
		namesLabel = new JLabel();
		
		// this layout
		this.setLayout(new BorderLayout());
		
		// Our board container
		mainPanel.setLayout(new BorderLayout());
		boardCenter.setLayout(new BoxLayout(boardCenter,BoxLayout.X_AXIS));
		
		for (int i=0; i < 3; i++) {
			peggingColumns[i] = new PeggingPanel(i,this);
			boardCenter.add(peggingColumns[i]);
		}
		
		mainPanel.add(boardTop,"North");
		mainPanel.add(boardBottom,"South");
		mainPanel.add(boardLeft,"West");
		mainPanel.add(boardRight,"East");
		mainPanel.add(boardCenter,"Center");
		
		this.add(mainPanel,"Center");
		//Add the names to the names label
		String humanName = players[0].getName();
		if (!humanName.equals("Player 0") & !humanName.equals("")) {
			humanName = humanName.substring(0,1);
			namesLabel.setText(" "+humanName+" "+1+" "+2);
			namesLabel.setFont(new Font("Helvetica",Font.PLAIN,10));
		}
		else {
			namesLabel.setText(" "+0+" "+1+" "+2);
			namesLabel.setFont(new Font("Helvetica",Font.PLAIN,10));
		}
		this.add(namesLabel,"South");
		
	}
	
	public void addScore(int playerID,int points) {
		peggingColumns[playerID].addScore(points);
	}
	
	public void setWinPeg(int playerID) {
		boardTopIcon = new ImageIcon("./images/boardTop"+playerID+".gif");
		boardTop.setIcon(boardTopIcon);
	}
	
}
