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

package rec.simulate.recognizers;

import rec.geo.*;
import java.util.*;
import java.awt.*;
import rec.util.*;
import rec.sys.*;
import rec.core.widgets.*;
import rec.meche.Estimator;
import rec.meche.WMSpring;
import rec.simulate.widgets.*;
import rec.simulate.*;

public class SimRotationalDamperRecognizer extends Recognizer
{
    public SimRotationalDamperRecognizer()
    {
    }

    public boolean recognize(Surface s, Widget w)
    {

	GeometryCalculator gc = new GeometryCalculator(s);
	Iterator it = gc.getVisibleWidgets().iterator();
	
	if (w instanceof WCircle){
	    
	    WCircle c = (WCircle) w;

	    while (it.hasNext()){
		
		VisibleWidget vw = (VisibleWidget) it.next();

		if (vw instanceof WCircle){
		    
		    WCircle testCircle = (WCircle) vw;
		    
		    if( !testCircle.equals(c) &&
			Estimator.connects( testCircle.getCenter(),
					    c.getCenter(), 20 ) &&
			c.getRadius() > testCircle.getRadius()){
			return false;
		    }

		}
		
		if (vw instanceof WSimDamper){

		    if(isRotationalDamper(c, (WSimDamper) vw)){
			replace(c, (WSimDamper) vw, s);
			return true;
		    }
		}
		
	    }
	}
	else if (w instanceof WSimDamper){

	    WCircle smallestCircle = null;
	    
	    while (it.hasNext()){
		
		VisibleWidget vw = (VisibleWidget) it.next();
		
		if (vw instanceof WCircle){

		    WCircle testCircle = (WCircle) vw;
		    
		    if(isRotationalDamper(testCircle, (WSimDamper) w)){

			if(smallestCircle == null 
			   && testCircle.getRadius() < 30 ){
			    smallestCircle = testCircle;
			    System.out.println("SMALLEST CIRCLE IS");
			    System.out.println(smallestCircle);
			    System.out.println("Radius = " 
					       + smallestCircle.getRadius());

			}
			else if (smallestCircle != null
				 && smallestCircle.getRadius() 
				 > testCircle.getRadius()){
			    smallestCircle = testCircle;
			    System.out.println("SMALLEST CIRCLE IS");
			    System.out.println(smallestCircle);
			    System.out.println("Radius = " 
					       + smallestCircle.getRadius());
			}

		    }
		}
	    }

	    if (smallestCircle != null){
		replace(smallestCircle, (WSimDamper) w, s);
		return true;
	    }

	}
	else{
	    return false;
	}

	return false;
	
    }

    private boolean isRotationalDamper(WCircle circle, WSimDamper damper){
	
	GPoint center = damper.getLocation().getCenter();
	
	if (circle.contains(center) ||
	    circle.includes(center, 10)){
	    return true;
	}
	
	return false;
    }
    
    private void replace(WCircle circle, WSimDamper damper, Surface s){
	
	SimulateSurface SimS = (SimulateSurface) s;
	
	WSimRotationalDamper rotationalDamper = 
	    SimS.makeRotationalDamper(circle.getCenter());
	
	HashSet set = new HashSet();
	
	set.add(circle);
	set.add(damper);
	
	s.replaceWidget(set, rotationalDamper, true);
    }
	    

    public Iterator getInputTypes()
    {
	Vector v = new Vector();
	
	v.addElement("rec.simulate.widgets.wm.WMSimDamper");
	v.addElement("rec.core.widgets.WCircle");

	return(v.iterator());
    }

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

	v.addElement("rec.simulate.widgets.WSimRotationalDamper");

	return(v.iterator());
    }
}
