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

package rec.house;


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

public class  WallTBreaker extends WallBreaker
{
  public WallTBreaker()
  {
  }
  
  public boolean recognize( Surface s, Widget w )
  {
    WLine line = (WLine)w;
    GPoint head = line.getHead();
    GPoint tail = line.getTail();
    boolean did_stuff = false;

    //    Console.writeln( "T-snappie!" );

    // find lines with points on our line and if they are there,
    // snap ourselves or vice-versa
    Iterator e = s.getWidgetsIterator();
    while( e.hasNext() )
      {
	Object o = e.next();
	if ( o instanceof WLine )
	  {
	    WLine line2 = (WLine)o;

	    if ( line2.equals( line ) )
	      continue;

	    if ( snapOn( s, line, line2.getHead() ) )
	      return true;

	    if ( snapOn( s, line, line2.getTail() ) )
	      return true;

	    // now let the new line snap other older lines.
	    if ( !snapOn( s, line2, line.getHead() ) )
	      {
		// if not cut by head, try tail.
		if ( snapOn( s, line2, line.getTail() ) )
		  did_stuff = true;
	      }
	    else
	      did_stuff = true;
	  }
      }

    // if we changed things, lets go round again, just in case.
    if ( did_stuff )
      {
	s.rerecognizeWidget( w );
	return true;
      }
    else
      return false;
  } 



  /**
   * if the point p is on the segment, but not near endpoints,
   * then snap like a twig.   
   */
  boolean snapOn( Surface s, WLine line, GPoint p )
  {
    if ( line.includes( p, BREAK ) 
	 && ( !line.getHead().includes( p, BREAK )
	      && !line.getTail().includes( p, BREAK ) ) )
      {
	split( s, line, p );
	
	return true;
      }

    return false;
  }


  
}
