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

package rec.house;

import java.util.*;
import java.awt.*;
import rec.util.*;
import rec.geo.*;
import rec.sys.*;
import rec.core.*;

public class DoorRecognizer extends Recognizer 
{

  public DoorRecognizer()
  {
  }


  public boolean recognize( Surface s, Widget w )
  {
    Rectangle b = ((VisibleWidget)w).getBounds();
    GPoint center = ((VisibleWidget)w).getLocation().getCenter();

    // check for size
    if ( ( b.width < 60 ) && ( b.height < 60 ) 
	 && ( b.width > 10 ) && ( b.height > 10 ) )
      {
	// see if we are near Edge midpoint
	GeoFunction gf = new GeoFunction()
	  {
	    public boolean check( VisibleWidget vw )
	      {
		if ( vw instanceof Wall )
		  return true;
		else
		  return false;
	      }
	  };
	GeometryCalculator gc = new GeometryCalculator( s );
	Wall ed = (Wall)gc.findClosestWithCheck( center, gf );

	if ( ed != null ) 
	  {
	    // we have Door (yeah, as _if_ this is working.)
	    Door dr = new Door( s, ed, 0.5 );

	    dr.where = Geometry.distance( center, ed.getHead() )
	      / ed.getLength();

	    s.replaceWidget( w, dr );

	    s.shuffleToTop( dr );

	    return true;
	  }
	else
	  Console.writeln( "No place for possible door found." );
      }
    else
      Console.writeln( "Widget too big to be a door." );

    return false;
  }

  public Iterator getInputTypes()
  {
    Vector v = new Vector();

    v.addElement( "rec.sys.Squiggle" );
    v.addElement( "rec.core.WCircle" );

    return( v.iterator() );
  }

  public Iterator getOutputTypes()
  {
    Vector v = new Vector();

    return( v.iterator() );
  }


  
}

////////////////////////////////////////////////////////////////////////////////



