/*
 *	
 *  Filename: GuessFrame.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.*;

/**
 * GuessFrame.java
 *
 * GuessFrame 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.
 *  Note that methods addButtonListener and addTileListener are used to enhance the GUI components
 *  with interactive capabilities by adding listeners defined in the GuessController  
 *  
 * @author  Achim E Karger
 * @version 1.0 04/26/08
 * @see     java.lang.System
 */
class GuessFrame extends Frame  {

    private GuessModel model;
    private static final Color[] Colors = {
    Color.blue, Color.orange, Color.red,  Color.yellow, 
    Color.cyan, Color.green,  Color.pink, Color.magenta
    };
      

   // the GUI elements
   private JLabel instructionLabel; 
   private JPanel boardPanel, buttonPanel;
   
   protected JPanel[] myTiles;
   protected JButton play, showall, quit;
   

    public GuessFrame(GuessModel model) {
    	this.model = model;
    	
    	int i;
    	
        // see L&L p. 354  the Frame is organized in a vertical BoxLayout, instructions in the top box
        //  one box for each of 2 panels
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        setTitle("Guessing Game");
        
        // game instructions
        JLabel instructionLabel = new JLabel("Click on a tile to reveal it's color");
        
        // the board
        boardPanel = new JPanel(new GridLayout(4,4,2,2)); // create 2 pixel horizontal and 
        												  // vertical gaps between tiles           
        boardPanel.setSize (new Dimension(200, 200));
		boardPanel.setBackground(Color.white);
		
        myTiles = new JPanel[model.NUM_TILES]; 
        for (i=0;i<model.NUM_TILES;i++){
        	myTiles[i] = new JPanel();
        	myTiles[i].setBorder(BorderFactory.createRaisedBevelBorder());      

        	myTiles[i].setSize (new Dimension(40, 40));
		    boardPanel.add(myTiles[i]);
        }
       	hideAll(); // for now, all color 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 lable and panels to the Frame ( vertical BoxLayout), 
        // note the order of addition from top to bottom    
        add(instructionLabel);
        add(boardPanel);
        add(buttonPanel);

        addWindowListener(new MyAdapter(this));
        pack();
        setVisible(true);

    }

        // showTile maps the numbers in the theDeck array (model) to the Colors array(view): each of the 8 different numbers  
        // (0-7) in the deck corresponds to one of the 8 available colors the Colors array 
    public void showTile(int i){
 	  myTiles[i].setBackground(Colors[model.theDeck[i]]);
    }
 
    public void hideTile(int i){
      myTiles[i].setBackground(Color.black);
    }

    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
