import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.Image;

public class ClickomaniaPanel extends GamePanel
{
    private ActionListener myMoveMaker;
    private MouseAdapter myMoveTryer;
    private Image myBackImage;
    private IconFactory myIconFactory;
    
    /**
     * Create a button panel for the model
     */
	
    ClickomaniaPanel(GameModel model)
    {
	super(model);
    }


    protected void initialize()
    {
        myBackImage = ImageFactory.getImage(this, "ola.jpg");
        myIconFactory = new IconFactory("clickimages");
	
	myMoveMaker = new ActionListener(){
		public void actionPerformed(ActionEvent e)
		{
                    int value = Integer.parseInt(e.getActionCommand());
                    final int row = value/myModel.getCols();
                    final int col = value%myModel.getCols();
		    myModel.makeMove(new Move(row,col));
		}
	    };

        myMoveTryer = new MouseAdapter(){
                public void mouseEntered(MouseEvent e)
                {
                    BackImageButton button = (BackImageButton) e.getSource();
                    int value = Integer.parseInt(button.getActionCommand());
                    final int row = value/myModel.getCols(); 
                    final int col = value%myModel.getCols();
                    myModel.tryMove(new Move(row,col));
                }
            };
    }
    
    protected JButton makeButton(int row, int col, int count)
    {
	String label = ""+count;
	// create button with label reflecting count
        
        Icon icon = new DividedImageIcon(myBackImage,myModel);
	BackImageButton button = new BackImageButton(icon);
	button.setActionCommand(label);
	button.addActionListener(myMoveMaker);
	button.addMouseListener(myMoveTryer);
	return button;
    }

    public void doHighlight(GridPoint[] list, boolean show)
    {
	for(int j=0; j < list.length; j++){
	    GridPoint p = list[j];
            if (show){
		showBorder(myButtons[p.getRow()][p.getCol()],true);
            }
            else {
		showBorder(myButtons[p.getRow()][p.getCol()],false); 
            }
	}
    }
        
    /**
     * 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 doSetGrid(GridPoint[][] list)
    {
	for(int j=0; j < list.length; j++){
	    for(int k=0; k < list[0].length; k++) {
		BackImageButton button =
		    (BackImageButton) myButtons[j][k];
		
                button.setBorder(myEmptyBorder);
		if (list[j][k].getValue() != GameModel.INVALID){
                    button.changeIcon(
			myIconFactory.getIcon(list[j][k].getValue()));
                }
		else {
		    button.resetBackground();
		}
	    }
	}
    }
}
