/*
 *	
 *  Filename: GuessPanel.java
 *  Java Application: GuessFrame: GUI for  Guessing Game (Lab 7)
 *  Author: Achim E Karger
 *  Course: CSCI 111 Java I
 *  Assignment: Lab 7
 *  Date:   April 27, 2008
 *  compiler JCreator LE
 *
 * 
 */

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * GuessPanel.java
 *
 * GuessPanel provides the GUI for the GuessModel
 * A board with hidden color tiles is set up for a guessing game where the user can flip one of the 
 * tiles and then tries to guess which tile on the board hides the same color. 
 * Buttons allow the user to restart, quit the game or reveal the solution.
 *
 *  
 * @author  Achim E Karger
 * @version 1.0 05/08/08
 * @see     java.lang.System
 */
class GuessPanel extends JPanel  {

    private GuessModel model;
    private static final ImageIcon tileIcon = new ImageIcon("tile.jpg");
    private static final ImageIcon[] icons = {
    	 new ImageIcon("image1.jpg"),
    	 new ImageIcon("image2.jpg"),
    	 new ImageIcon("image3.jpg"),
    	 new ImageIcon("image4.jpg"),
    	 new ImageIcon("image5.jpg"),
    	 new ImageIcon("image6.jpg"),
    	 new ImageIcon("image7.jpg"),
    	 new ImageIcon("image8.jpg")
       };    

   // the GUI elements
   private JPanel instructionPanel, boardPanel, buttonPanel;

   private JLabel instructionLabel; 
   protected JPanel[] myTiles;
   protected JLabel[] myLabels;
   
   protected JButton play, showall, quit;
   

    public GuessPanel(GuessModel model) {
    	this.model = model;
    	
    	int i;
    	
        // see L&L p. 354  the Frame is organized in a vertical BoxLayout,
        //  one box for each of 3 panels
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        
        // game instructions
        JLabel instructionLabel = new JLabel("Click on a tile to reveal the image");
    	instructionPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    	instructionPanel.add (instructionLabel);
    	
        // the board
        boardPanel = new JPanel(new GridLayout(4,4,2,2)); // create 2 pixel horizontal and 
        												  // vertical gaps between tiles           
        boardPanel.setPreferredSize (new Dimension(320, 320));
		boardPanel.setBackground(Color.white);
		
        myTiles = new JPanel[model.NUM_TILES];
        myLabels = new JLabel[model.NUM_TILES]; 
    
        for (i=0;i<model.NUM_TILES;i++){
        	myLabels[i] = new JLabel(tileIcon);
        	myTiles[i] = new JPanel();
            myTiles[i].setPreferredSize (new Dimension(75, 75)); // the icons are 75x75 px
        	myTiles[i].add(myLabels[i]);
		    boardPanel.add(myTiles[i]);
        }
       	hideAll(); // for now, all image tiles are hidden
  
        // the buttons:
        play    = new JButton("Play Again");
        showall = new JButton("Show All");
        quit    = new JButton("Quit");

        buttonPanel = new JPanel();  // default layout for buttonPanel: FlowLayout 
        buttonPanel.add (play);
        buttonPanel.add (showall);
        buttonPanel.add (quit);
    
        // add the label and panels to the GuessPanel ( vertical BoxLayout), 
        // note the order of addition from top to bottom    
        add(instructionPanel);
        add(boardPanel);
        add(buttonPanel);

      }

        // showTile maps the numbers in the theDeck array (model) to the icons array(view): each of the 8 different numbers  
        // (0-7) in the deck corresponds to one of the 8 available images the icons array 
    public void showTile(int i){
 	          myLabels[i].setIcon(icons[model.theDeck[i]]);
    }
 
    public void hideTile(int i){
        myLabels[i].setIcon(tileIcon);
    }

    public void hideAll(){
         for (int i=0;i<model.NUM_TILES;i++)
         	hideTile(i);
     }

     public void showAll(){
         for (int i=0;i<model.NUM_TILES;i++)
         	showTile(i);
     }

     public void addButtonListener(ActionListener bal) {
        play.addActionListener(bal);
        showall.addActionListener(bal);
        quit.addActionListener(bal);
    }

    // adds an indexed listener to the tile corresponding to the same index in the deck
    public  void addTileListener(MouseListener ml, int tileID) {
       	myTiles[tileID].addMouseListener(ml); 
    }
    
    
} // end of GUI