package primitives.machines;
import java.awt.*;
import primitives.geomtry.*;
/**
*A class encapsulating the behavior of a Small machine used by <a href="SmallMachineProof.html">
* SmallMachineProof</a> applet.
*@author Dori Eldar.
*/
public class SmallMachine extends Machine{
	/**
	*Switching state of the 2 joints.
	*Only values of -1 and a are allowed [should be private]
	*/
	public int[] state ;//only valus of +1,-1  !
	/**
	*the angle of the current state of the machine.
	*[should be private]
	*/
	public double alpha;
	/**
	*Specifies the angle limits of motion.
	*/
	public double[] alphaRange;
	/**
	*@param d the size of the rectangle to draw the machine in.
	*/
	public SmallMachine(Dimension d){
		super(2,2,d);
		alpha = 0.0;
		state = new int[2];
		alphaRange = new double[2];
		barLength=(Math.min(d.width,d.height))/3;
		for (int angle=0;angle<2;angle++){
			state[angle]=-1;
			anchors[angle]=new ExtPoint(d.width/6+angle*(int)(barLength*1.5),d.height/2);
		}
		dJoints[0] = Geomtry.getPointByVector(anchors[0].toCoordinate(),barLength,0);
		dJoints[1] = Geomtry.getTriPointEx(anchors[1].toCoordinate(),barLength,dJoints[0],barLength,new Coordinate(0,0),null);
//		relative = new Coordinate((anchors[0].x+anchors[1].x)/2,0);
	}
	private Coordinate t1 = new Coordinate();
	private Coordinate t2 = new Coordinate();
	/**
	*rotates the right joint with respect to the left joint.
	*@param alpha the angle to rotate the joint to.
	*@exception MachineException if the rotation requsted is not valid.
	*/
	public void rotateJoint(double alpha)throws MachineException{
			savePoints();
			dJoints[0] = Geomtry.getPointByVector(anchors[0].toCoordinate(),barLength,alpha);
			try{
				t1.move(anchors[1].x,anchors[1].y);
				t2.move(dJoints[1].x,(1+state[1])*d.height/2);
				dJoints[1] = Geomtry.getTriPointEx(dJoints[0],barLength,
					t1,barLength,dJoints[1],t2);
			}catch(ArithmeticException e){
				restorePoints();
				if (alpha>0) alphaRange[1] = alpha ;
				else alphaRange[0] = alpha;
				throw new MachineException("illegal move");
			}
	}
	/**
	* streches the right leg.
	*/
	public void reachEnd(){
		dJoints[1] = Geomtry.getMidPoint(anchors[1].toCoordinate(),dJoints[0],0.5);
	}
	/**
	* Redraws the machine.
	*@param g the graphic context to draw to.
	*/

	public void redraw(Graphics g){
		g.setColor(Color.black);
		updatePoints(dJoints,joints);
		for(int i=0;i<2;i++){
			drawLine(g,anchors[i],joints[i]);
		}
		drawLine(g,joints[0],joints[1]);
		super.redraw(g);
	}
	/**
    *Switches the bending state of the right joint.
	*@see Machine#switchBend
	*/
	public void switchBend(){
	/*	Coordinate[] temp = Geomtry.getTriPoint(anchors[1].toCoordinate(),barLength,dJoints[0],barLength);
		if (Geomtry.distance(temp[0],dJoints[1])<Geomtry.distance(temp[1],dJoints[1]))
			dJoints[1] = temp[1];
		else dJoints[1] = temp[0];
		*/
		
		state[1] = state[1]*(-1);
	}
	/**
	*Sets object field to null.
	*@exception java.lang.Throwable .
	*/
	public void finalize() throws Throwable{
		state = null;
		alphaRange = null;
		super.finalize();
	}
}