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

package rec.house;


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

/**
 * This subtracts out any regions underneath this region.  So if a new
 * region is overlapping another it cuts out the underdoggie.
 */
public class RegionSplitter extends Recognizer
{
  public static final double BREAK = 20;

  public RegionSplitter()
  {
  }

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

  /**
   * Recognizes on regions, not walls.
   */
  public boolean recognize( Surface s, Widget w )
  {
    Region new_r = (Region)w;

    Iterator e = s.getWidgetsIterator();

    while( e.hasNext() )
      {
	Object o = e.next();
	if (! ( o instanceof Region ) )
	  continue;
	Region r = (Region)o;

	// check to see if this is us
	if ( r.equals( new_r ) )
	  continue;

	// check to see if this is a mommy region
	if ( !r.contains( new_r, BREAK ) )
	  continue;

	// it must have been!  hack it, hack it now!
	Console.writeln( "Hacking " + r + " using " + new_r );
	Region rest = r.getRemainder( new_r, BREAK );
	if ( rest != null )
	  s.replaceWidget( r, rest, true );
	else
	  {
	    Console.writeln( "Strange- mother region remainder empty." );
	    s.removeWidget( r );
	  }
      }

    return false;
  }


  /*
  public boolean recognize_failed( Surface s, Widget w )
  {
    final WLine l = (WLine)w;

    Iterator e = s.getWidgetssIterator();

    GeoFunction gf = new GeoFunction()
      {
	public boolean check( Widget vw )
	  {
	    if ( vw instanceof Region )
	      {
		Region r = (Region)vw;
		if ( r.contains( l.getMidpoint(), BREAK ) )
		  return v;
	      }

	    return null;
	  }
      };
    GeometryCalculator gc = new GeometryCalculator( s );
    Region r = (Region)gc.findWidget( gf );

    if ( r != null )
      {
	s.removeWidget( r );
      }

    return false;
  }
  */

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

    //    v.addElement( "rec.core.WLine" );
    v.addElement( "rec.house.Region" );
    return( v.iterator() );
  }

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

    v.addElement( "rec.house.Region" );

    return( v.iterator() );
  }
}

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