// (c) MIT 2003.  All rights reserved.

package rec.demo;

import java.rmi.*;
import java.util.*;
import speech.*;
import speech.tools.*;
import metaglue.*;
import rec.core.*;
import rec.sys.*;
import rec.fsm.*;
import rec.util.*;
import rec.agent.*;
import demo.*;

public class StartAgent extends TalkAgent
    implements Start
{

  // so startup load doesn't do extra work.
  boolean first_run = true;

  String current_state;

  Guide guide;

  public StartAgent( ) throws RemoteException
  {
    guide = (Guide)reliesOn( "rec.demo.Guide" );
    guide.setDrawing( "freely without any assistance" );

    current_state = "simple";

    // get the simple mode up
    loadSimple();

    alert( "You are now drawing with little help.  What do you want to start up?  Say help for help." );

    first_run = false;
  }


  /**
   * Incoming speech goes through here.
   */
  public void acceptedTagsResult (Hashtable tags) throws RemoteException
  {
    log( "Just got into atr" );

    if ( tags.containsKey( "fsm" ) )
      {
	killHelp( current_state );
	killModules();
	startSystem( "fsm.ini" );
	guide.setDrawing( "finite state machines" );
	current_state = "fsm";
	raiseHelp( current_state );
      }
    else if ( tags.containsKey( "house" ) )
      {
	killHelp( current_state );
	startSystem( "house.ini" );
	guide.setDrawing( "house floor plans" );
	current_state = "house";
	raiseHelp( current_state );
      }
    else if ( tags.containsKey( "sim" ) ) 
      {
	killHelp( current_state );
	startSystem( "sim.ini" );
	guide.setDrawing( "mechanical simulations" );
	current_state = "sim";
	raiseHelp( current_state );
      }
    else if ( tags.containsKey( "revert" ) )
      {
	loadSimple();
      }
    else
      alert( "I do not understand you, I have a bad grammar in start agent." );
  }

  void loadSimple()
       throws RemoteException
  {
    if ( !first_run )
      {
	killHelp( current_state );
	killModules();
      }
    startSystem( "simple.ini" );
	
    guide.setDrawing( "freely" ); // hack--we know what guide says
    current_state = "simple";

    raiseHelp( current_state );
  }


  void killHelp( String help ) throws RemoteException
  {
    //Help h = (Help)reliesOn( "rec.demo.Help",
    //		   help );

  //h.turnOff();
  }

  void raiseHelp( String help ) throws RemoteException
  {
    //    Help h = (Help)reliesOn( "rec.demo.Help",
    //help );

  //h.turnOn();
  }

  void startSystem( String file )
  {
    try
      {
	alert( "please wait while I load up the program" );
	log( "loading base: " + file );
	sketch.loadBase( file );
	
	// now load grammar agents 
	String dname = file.toString();
	int i = dname.lastIndexOf( "." );
	dname = dname.substring( 0, i );
	
	log( "Going to do relies on for group" );
	Group ag = (Group)reliesOn( "demo.Group", dname );
	ag.ensureUp();

	log( "Done with the relieson call" );

	alert( "Thank you for your patience.  Happy drawing!" );
      }
    catch ( Throwable e )
      {
	e.printStackTrace();
	System.err.println( "....continuing...." );
      }
  }


  public void killModules( )
  {
    Gremlin g = new Gremlin() {
      public int tweak( Surface s )
	{
	  Assert.error( "Not implemented" );
	  return 0;
	  
	  // remove all modules
//  	  int n = s.numRecognizerModules();
//  	  System.err.println( "There are " + n + " modules to remove." );
//  	  for ( int i = 0; i < n; i++ )
//  	    s.removeRecognizerModule();

//  	  return n;
	}

    };

    try 
      {
	int res = sketch.takeGremlin( g );
	log( "Removed " + res + " modules." );
      }
    catch ( RemoteException e )
      {
	e.printStackTrace();
	log( "....continuing...." );
      }
  } // end killModules
  
}



