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

package rec.house.speech;

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

public class RegionDiscusserAgent extends SpeechAgent
    implements RegionDiscusser, ModuleListener
{
  protected Sketch sketch;
  protected AppGrammar grammar;
  protected Asker asker;

  Hashtable names;
  //  ModuleListener regionWatcherListener;
  RemoteModule regionWatcherModule;

  public RegionDiscusserAgent( ) throws RemoteException
  {
    grammar = loadGrammar();
    makeAlwaysActive( grammar );
    
    // get the sketch agent
    sketch = (Sketch)reliesOn( "rec.agent.Sketch" );

    asker = (Asker)reliesOn( "speech.tools.Asker" );

    // regionWatcherListener = this;

    ModuleMaker mm = new ModuleMaker( "rec.house.speech.RegionWatcherModule" );
    regionWatcherModule = sketch.installModule( mm,
						getAgentID() );
    // regionWatcherListener );
  }


  /**
   * Incoming speech goes through here.
   */
  public void acceptedTagsResult (Hashtable tags) throws RemoteException
  {
    System.err.println( "RegionWatcher activated. " + tags );
  }


  public void fixer( WatcherMessage m ) throws RemoteException
  {
    final WatcherMessage msg = m;

    // make the gremlin which will do it
    Gremlin g = new Gremlin( ) {

      public int tweak( Surface surf )
	{
	  System.err.println( "trying to undo: " + msg );
	  surf.undo();

	  return 0;
	}
    };

    // name the object
    sketch.takeGremlin( g );
  }


  public Object ring( Object o ) throws RemoteException
  {
    WatcherMessage n = (WatcherMessage)o;
    
    System.err.println( "got " + n );

    if ( asker.ask( n.msg + "  Do you want to undo this?" ) 
	 == Asker.YES )
      {
	fixer( n );
      }
    else
      alert( "nothing done." );

    return null;
  }
  
  
}



