// (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  WallMerger extends WallBreaker
{
  public WallMerger()
  {
  }
  
  public boolean recognize( Surface s, Widget w )
  {
    WLine line = (WLine)w;
    GPoint head = line.getHead();
    GPoint tail = line.getTail();

    // go through, find lines which overlap our line, and if we find one
    // remove ourselves from the pool.
    Iterator e = s.getWidgetsIterator();
    while( e.hasNext() )
      {
	Object o = e.next();
	if ( o instanceof WLine )
	  {
	    int cut_count = 0;

	    WLine line2 = (WLine)o;

	    if ( line2.equals( line ) )
	      continue;

	    GPoint head2 = line2.getHead();
	    GPoint tail2 = line2.getTail();

	    if ( head.includes( head2, BREAK ) 
		 || head.includes( tail2, BREAK ) )
	      {
		if ( tail.includes( head2, BREAK )
		     || tail.includes( tail2, BREAK ) )
		  {
		    // we can nuke ourselves!
		    s.removeWidget( line );

		    return true;
		  }
	      }
	  }
      }

    return false;
  } // end rec2





  public boolean ignore_recognize( Surface s, Widget w )
  {
    WLine line = (WLine)w;

    // go through, find lines which overlap our line, and if it does
    // snap our line like a twig and return.
    Iterator e = s.getWidgetsIterator();
    while( e.hasNext() )
      {
	Object o = e.next();
	if ( o instanceof WLine )
	  {
	    int cut_count = 0;

	    WLine line2 = (WLine)o;
	    GPoint head = line2.getHead();
	    GPoint tail = line2.getTail();
	    
	    GSegment g1 = line.getLine();
	    GSegment g2 = line2.getLine();

	    // if angle of line from x-axis within XX radians
	    if ( ( Math.abs( g1.getAngle() - g2.getAngle() ) ) < 0.1 )
	      {
		if ( line.includes( head, BREAK ) )
		  {
		    if ( head.includes( line.getHead(), BREAK )
			 || tail.includes( line.getTail(), BREAK ) )
		      cut_count++;
		    else
		      {
			// do the split of the line
			split( s, line, head );

			return true;
		      }
		  }

		if ( line.includes( tail, BREAK ) )
		  {
		    if ( tail.includes( line.getHead(), BREAK )
			 || tail.includes( line.getTail(), BREAK ) )
		      cut_count++;
		    else
		      {
			// do the split of the line
			split( s, line, tail );

			return true;
		      }
		  }

		// check to see if we have 2 overlapping lines
		if ( cut_count == 2 )
		  {
		    // we can nuke ourselves!
		    s.removeWidget( line );
		  }
	      }
	  }
      }

    return false;
  } // end rec2

}
