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

package rec.house.speech;

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

public class NamerAgent extends TalkAgent
    implements Namer, ModuleListener
{
  Hashtable names;
  //  ModuleListener namerListener;
  RemoteModule namerModule;

  public NamerAgent( ) throws RemoteException
  {
    // get the sketch agent
    names = new Hashtable();
    //    namerListener = this;

    ModuleMaker mm = new ModuleMaker( "rec.house.speech.NameTrackerModule" );
    namerModule = sketch.installModule( mm, getAgentID() );
  }


  /**
   * Incoming speech goes through here.
   */
  public void acceptedTagsResult (Hashtable tags) throws RemoteException
  {
    System.err.println( "Namer activated." );
    if ( tags.containsKey( "namephrase" ) )
      {
	String pc = "it";
	
	String name = null;
	if ( tags.containsKey( "name" ) )
	  name =  (String) tags.get( "name" );
	else
	  name = asker.askFree( "What do you want to name " + pc + "?" );
	
	System.err.println( "Got the answer of " + name );
	name = fixName( name );
	
	if ( tags.containsKey( "region" ) )
	  {
	    System.err.println( "specific region name" );
	    Id id = findRegion( (String) tags.get( "region" ) );
	    if ( id != null )
	      namer( id, name );
	    else
	      System.err.println("Got null id back for " + tags.get("region"));
	  }
	else
	  namer( name );
      }
  }


  String fixName( String n )
  {
    if ( n != null )
      return n.trim();
    
    System.err.println( "fix name returning null" );

    return "";
  }

  public Id findRegion( String name )
  {
    try
      {
	Id id = (Id) namerModule.query( name );
    
	return id;
      }
    catch ( Exception e )
      {
	e.printStackTrace();
	System.err.println( "....continuing...." );
      }

    return null;
  }


  /**
   * @param what   null means everything, else it switches in its
   *               known classes, and it and all children are nuked.
   */
  public void namer( String what_name ) throws RemoteException
  {
    // get the object
    Id id = (Id)namerModule.query( null );
   
    namer( id, what_name );
  }

  public void namer( Id the_id, String what_name ) throws RemoteException
  {
    final Id id = the_id;
    final String name = what_name;

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

      public int tweak( Surface surf )
	{
	  Iterator e = surf.getWidgetsIterator();
	  boolean done = false;
	  while( !done && e.hasNext() )
	    {
	      Widget w = (Widget)e.next();
	      if ( id.equals( w.getId() ) )
		{
		  surf.beginEvent();
		  System.err.println( "Setting " + w + "'s name to " + name );
		  ( (Region)w ).setName( name );
		  done = true;
		}
	    }
	  
	  return done ? 1 : 0;
	}
    };

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


  public Object ring( Object o ) throws RemoteException
  {
    NameTracker n = (NameTracker)o;
    
    if ( n.added )
      {
	names.put( n.name, n.name );
      }
    else
      {
	names.remove( n.name );
      }
    System.err.println( "I know we have " + names.keys() + " names now!" );
    
    return null;
  }
  
  
}



