import java.awt.*;
import primitives.frames.*;
import primitives.machines.NarmsMachine;
import primitives.geomtry.*;
public class NarmsAutoPilot extends Frames{
	final double dalpha = 0.05;
	double alpha = 0;
	double newAngle ;
	Coordinate oldPoint,newPoint;
	NarmsMachine machine ;
	int strechedLeg=-1;
	public void start(){
			run = true;

		machine = new NarmsMachine(frames[0].drawArea.getSize(),getParam("arms",5),1.3);
		frames[0].drawArea.setCurrentObject(machine);
		oldPoint = machine.center.toCoordinate();
		newAngle = Math.random()*Math.PI+Math.PI/2;
		newPoint = machine.getMaxPoint(newAngle);
 		startThread();
	}
	public void stop(){
		super.stop();
		machine = null;
		newPoint = null;
		oldPoint = null;
	}
	public void changeFrames(){
		
 		 Coordinate temp = Geomtry.getMidPoint(oldPoint,newPoint,1-alpha);
		 if(alpha<=1+dalpha){
			strechedLeg=machine.moveCenterEx(temp.x-machine.center.x,temp.y-machine.center.y);
			alpha = alpha+dalpha;
		 	frames[0].drawArea.repaint();
		}else{
		 	 alpha = 0;
			 if(strechedLeg>-1) machine.switchBend(strechedLeg);
			 oldPoint.move(temp.x,temp.y);
			 newAngle = Math.random()*Math.PI*3/4+Math.PI/4+newAngle;
			 newPoint = machine.getMaxPoint(newAngle);
		} 
	}

}