/*
 *	
 *  Filename: GuessController.java
 *  Java Application GuessGameWindow: 
 *  Controller class for GuessModel(model) and GuessFrame(GUI view)(Lab 7)
 *  Author: Achim E Karger
 *  Course: CSCI 111 Java I
 *  Assignment: Lab 7
 *  Date:   April 26, 2008
 *  compiler JCreator LE
 *
 *  
 * 
 */
//   GuessController.java - Controller
//    Handles user interaction with listeners.
//    Calls View and Model as needed.
//
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * GuessController.java
 *
 *  GuessController provides interactive functionality for the GuessFrame GUI.  
 *  Listeners for the GUI elements (ButtonListener , TileListener)are implemented as inner classes.
 *  The  TimerListener class is used to hide the tiles 800 ms after the user has triggered a timer by guessing 
 *  tiles that don't match.   
 *
 * @author  Achim E Karger
 * @version 1.0 04/28/08
 * @see     java.lang.System
 */
public class GuessController {
    //...... the Controller needs to interact with both the Model and View.
    private GuessModel  model;
    private GuessFrame  view;
   
    private static int  tile1, tile2;  // Index of first and second tile flipped by user, 
                                       // set to -1 if has not been flipped.
    private Timer timer;               // Create a  timer to be used to flipback tiles that didn't match 
    public final int DELAY = 800;      // after DELAY, ms.
 
    
    //========================================================== constructor
    /** Constructor */
    public GuessController(GuessModel model, GuessFrame view) {
        this.model = model;
        this.view  = view;
        tile1 = tile2 =-1;
         
        //... Add listeners to the view buttons and the color tiles.
        view.addButtonListener(new ButtonListener());
        for (int i=0;i<model.NUM_TILES;i++)
                view.addTileListener(new TileListener(i),i);
                
       // set for 1 timer event only, no repeats
        timer = new Timer(DELAY, new TimerListener() );
        timer.setRepeats(false);  
    }    // end constructor
    
    
    ///////////////////////////////////////////// inner class ButtonListener
    /** 
     * Determines which button was pressed and sets the game board accordingly.
     */
   private class ButtonListener implements ActionListener
   {
      //--------------------------------------------------------------
      //  Determines which button was pressed and sets the game board accordingly.
      //--------------------------------------------------------------
      public void actionPerformed (ActionEvent event)
      {
       if (event.getSource() == view.play){
           tile1 = tile2 = -1;
           model.shuffleDeck();
           view.hideAll();
       } 
       if (event.getSource() == view.showall) 
           view.showAll();
        
       if (event.getSource() == view.quit)
         System.exit(0);
      

      }

    }        //end ButtonListener
      
        
    //////////////////////////////////////////// inner class TileListener
    /**  
     *
     */    
      private class TileListener extends MouseAdapter
   {
   //--------------------------------------------------------------
      //  Listener is identified by it's tileID
      //--------------------------------------------------------------
      private int tileID;
      
      // constructor
      public TileListener(int tileID){
      	this.tileID = tileID;
      	}
                
      public void mouseClicked (MouseEvent event)
      {
      // show the tile that was clicked using TileID member of the  TileListener	
      	view.showTile(tileID);
 
 
        if(tile1 == -1) {   // this is the first tile clicked
     		tile1 = tileID;
   		}
        else {    // this was the 2. tile => need to check tiles for match
		  tile2 =tileID;     
     	  if( model.theDeck[tile1] != model.theDeck[tile2]){    // tiles don't match
      			timer.start(); // start Timer for FlipBack aka hide both tiles
      	  } else {                                        // found a match!
      		tile1 = tile2 = -1;
          }
  		} 
      }       
      }  //end TileListener

    ///////////////////////////////////////////// inner class TimerListener
    /** When a mulitplication is requested.
     *  1. Identify view button using getSource.
     *  2. Call the model to mulitply by this number.
     *  3. Get the result from the Model.
     *  4. Tell the View to display the result.
     * If there was an error, tell the View to display it.lip
     */
   private class TimerListener implements ActionListener
   {

    // timer listener flips back both tiles if no match
	public void actionPerformed (ActionEvent event){
	  view.hideTile(tile1);
	  view.hideTile(tile2);
      tile1 = tile2 = -1;  
      }      

    }        //end TimerListener





}















