// (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.core.*;
import rec.util.*;
import rec.agent.*;
import rec.house.*;

public class RegionNamerAgent extends NamerAgent
    implements RegionNamer, ModuleListener
{

  public RegionNamerAgent() throws RemoteException
  {
    //AppGrammar g = loadGrammar( "rec.house.speech.Region" );
    //    makeAlwaysActive( g );
  }


  /**
   * Incoming speech goes through here.
   */
  public void acceptedTagsResult (Hashtable tags) throws RemoteException
  {
    try {
    System.err.println( "RegionNamer activated." );
    if ( tags.containsKey( "declare" ) )
      {
	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 );
      }
    }
    catch (Exception e )
      {
	e.printStackTrace();
	System.err.println( "....continue...." );
      }
  }

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

    return "";
  }


  /**
   * @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
  {
    final String name = what_name;

    Gremlin g = null;

    try
      {
	// get the object
	final Id id = (Id)namerModule.query( null );
   
	// make the gremlin which will name it
	g = new Gremlin( ) {

      public int tweak( Surface surf )
	{
	  System.err.println( "* in tweak of RNA" );
	  try {
	    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;
	  }
	  catch ( Exception e ) 
	    {
	      e.printStackTrace();
	      System.err.println( "....continuing(RNA-tweak)...." );
	    }
	  return 0;
	}
    };

      }
    catch ( Exception e )
      {
	e.printStackTrace();
	System.err.println("....continuing(2)...." );
      }

    // 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;
  }
  
  
}



