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

package rec.house;

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

public class RegionRecognizer extends Recognizer
{
  public final static double BREAK = 20.0;

  public RegionRecognizer()
  {
  }

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

  public boolean recognize( Surface s, Widget w )
  {
    WLine l = (WLine)w;

    // first check to make sure _new_ line is not already bordering a
    // region.  If it is--then no go!
    if ( ( l instanceof Wall ) && ( ((Wall)l).isBorder() ) )
      return false;

    // build up list of all possible lines.
    Iterator e = s.getWidgetsIterator();
    HashSet line_pool = new HashSet();
    while( e.hasNext() )
      {
	Object o = e.next();
	if ( o instanceof WLine )
	{
	  line_pool.add( o );
	}
      }


    PolygonInfo pi = null;
    if( (pi = PolygonParser.parse( line_pool, l )) != null ) {

      Console.writeln( "Region online" );

      Region q = new Region( pi );

      s.addWidget( q );

      // make lines walls
      int size = pi.getNumberOfSides();
      for ( int i = 0; i < size; i++ )
	{
	  WLine wl = pi.getLineAt( i );
	  
	  if ( wl instanceof Wall )
	    {
	      Console.writeln( "Adding region to wall." );
	      ((Wall)wl).addRegion( q );
	      q.addWall( (Wall)wl );
	    }
	  else
	    {
	      Console.writeln( "Making a wall" );
	      Wall new_w = Wall.changeToWall( s, wl );
	      new_w.addRegion( q );
	      q.addWall( new_w );
	    }
	} 

      return( true );
    }

    return( false );
  }

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

    v.addElement( "rec.core.WLine" );

    return( v.iterator() );
  }

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

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

    return( v.iterator() );
  }
}

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