import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Image;

public abstract class GamePanel extends JPanel
{
    protected JButton myButtons[][];
    protected GameModel myModel;
    protected Border myEmptyBorder;
    protected Border myShowBorder;    
    /**
     * Create a button panel for the model
     */
	
    GamePanel(GameModel model)
    {
	// construct superclass and make button array
	super(new GridLayout(model.getRows(),model.getCols(),0,0));
	myButtons = new JButton[model.getRows()][model.getCols()];
	myModel = model;
        myEmptyBorder = BorderFactory.createEmptyBorder();
        myShowBorder = BorderFactory.createMatteBorder(4,4,4,4,Color.BLUE); 
	initialize();
	makeButtons();
    }

    protected abstract void initialize();
    protected abstract JButton makeButton(int row, int col, int count);
    
    /**
     * Makes the buttons for this GUI/view. The number
     * of buttons is determined my the size of the array
     * myButtons. This helper function takes some of
     * the busy work out of the GamePanel constructor.
     */
    protected  void makeButtons()
    {
	int count = 0;
	for(int row = 0; row < myButtons.length; row++){
	    for(int col = 0; col < myButtons[0].length; col++) {
		myButtons[row][col] = makeButton(row,col,count);
		count++;
                myButtons[row][col].setBorder(myEmptyBorder);
		add(myButtons[row][col]);
	    }
	}
    }

    public abstract void doHighlight(GridPoint[] list, boolean show);

    public void showBorder(JComponent widget, boolean show)
    {
	if (show){
	    widget.setBorder(myShowBorder); 
	}
	else {
	    widget.setBorder(myEmptyBorder); 
	}
    }
    
    public void highlight(GridPoint[] list, boolean show)
    {
	doHighlight(list,show);
	repaint();
    }

    public abstract void doSetGrid(GridPoint[][] list);
    
    /**
     * Set all the buttons by re-displaying them all
     * in the right order. First we remove all the components
     * in this panel (that's the buttons). Then we add the
     * buttons to this panel in the right order based on what
     * the ordering of the buttons in the model is.
     * Finally, we revalidate so the buttons are shown (GUI
     * will redraw as a result of the revalidate).
     */
    public void setGrid(GridPoint[][] list)
    {
	doSetGrid(list);
	revalidate();
    }
}
