// Filename GameOfLife.java
// Daniel Farinha
// January 1999

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class GameOfLife extends Applet
                        implements ActionListener
{
	
  private final static int RESET = 0;
  private final static int EDIT = 1;
  private final static int RUNNING = 2;

  private int theState;
  private CellInterface aCellInterface;
  private CellGrid aCellGrid;
		
  public void init()
  {
    aCellInterface = new CellInterface( this);      // create instance of Presentation (CellInterface)
    this.add( aCellInterface);                      // register as listener with Interface
    aCellInterface.setResetState();                 // reset grid in Interface
    theState = RESET;                               // set state to RESET
		
    aCellGrid = new CellGrid();                     // create instance of Application (CellGrid)
    try
    {
      aCellGrid.addActionListener( this);           // register as listener with CellGrid
    }
    catch ( java.util.TooManyListenersException exception)
    {
      // do nothing
    }
  } // End init.
	
	
  public static void main( String args[])
  {
    Frame frame = new Frame("Dan's Game of Life");
    GameOfLife aGameOfLife = new GameOfLife();
	
    aGameOfLife.init();
    frame.add( aGameOfLife, "Center");
      
    frame.pack();   // Resizes the window to its natural size.
    frame.setResizable(false);
    frame.show();
  } // End main.
	
  ////////////////////////////////////////////////////////
  public synchronized void actionPerformed( ActionEvent event)
  {
    String theCommand = event.getActionCommand();
		
    if( theCommand.equals( "start"))          // user clicked the start button
    {
      aCellInterface.setRunningState();       // notify interface to set buttons sensitivities
      theState = RUNNING;                     // change state
      aCellGrid.setRunning(true);             // notify CellGrid to start simulation
    }
    else if( theCommand.equals( "stop"))      // user clicked the stop button
    {
      aCellInterface.setEditState();          // notify interface to set buttons sensitivities 
      theState = EDIT;                        // change state
      aCellGrid.setRunning(false);            // notify CellGrid to stop simulation
    }
    else if( theCommand.equals( "reset"))     // request for reset from User/CellGrid
    {
      aCellInterface.setResetState();         // notify interface to set buttons sensitivities

      theState = RESET;                       // change state
      aCellGrid.setRunning(false);            // notify CellGrid to stop simulation
      aCellGrid.resetGrid();                  // reset CellGrid just in case it was user request
    }
    else if ( theCommand.equals( "getcellinfo"))              // CellGrid notifies cell change
    {
      LastCellInfo aCellInfo = aCellGrid.getLastCellInfo();   // Get cell info from CellGrid
      aCellInterface.modifyGUICell(aCellInfo.cellID, aCellInfo.cellState);  // Change GUI cell
    }
    else if ( theCommand.equals( "edit"))         // CellGrid notified edit
    {
      if( theState != EDIT)                       // change only needed if not edit already
      {
        aCellInterface.setEditState();            // notify interface to set buttons sensitivities
        theState = EDIT;                          // change state
      }
    }
    else if ( theCommand.equals( "newgeneration"))  // CellGrid notifies new generation ready
    {
      WorldArray aGeneration = aCellGrid.getCellGrid();   // get new generation from CellGrid
      aCellInterface.writeNewGeneration( aGeneration);    // update GUI with new generation
    }			
    else if (theState != RUNNING)   // cell was pressed. must only work when not running
    {
      int cellID = Integer.parseInt( theCommand, 10);   // get cell id
      aCellGrid.modifyCell(cellID);                     // notify CellGrid
    } // End else
			
  }

} // End class GameOfLife