package threearms;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.* ;
import primitives.frames.*;
import primitives.machines.MachineException;
public class Receiver extends Frames implements FramesListener  {
	MachineSpace currentMachine = null;
	MachineSpace currentSpace= null;
	int frameNumber=0;
	int sign;
	int spaceSign=1;
	int times = 0;
	Point[] anchors;

	public void changeFrame(int  frame){
			run = true;
		try{
			if(t!=null)t.stop();
		}catch(SecurityException e){}
		    t = null;
			frames[0].drawArea.setCurrentObject(null);
			frames[1].drawArea.setCurrentObject(null);
			currentMachine = null;
			currentSpace = null;
		frameNumber = frame;
		switch(frame){
		case 0:{
			labels[1].setText("Configuration Space ?");
			currentMachine = new constraint3ArmsMachine(frames[0].drawArea,null);
			frames[0].drawArea.setCurrentObject(currentMachine);
		/*	if (firstFrame==null){
				firstFrame = new Thread(this,"FirstFrame");
				firstFrame.start();
			}	  */
			frames[1].drawArea.repaint();
			};break;
		case 1:{
			labels[1].setText("Configuration Space");
			
			currentMachine = new constraint3ArmsMachineUp(frames[0].drawArea,null);
			testRun();
			currentSpace = new ConstraintedSpaceUp(frames[1].drawArea,currentMachine,true);
			frames[0].drawArea.setCurrentObject(currentMachine);
			frames[1].drawArea.setCurrentObject(currentSpace);
		/*	if (firstFrame==null){
				firstFrame = new Thread(this,"FirstFrame");
				firstFrame.start();
			}	*/
			};break;
		case 2:{
			labels[1].setText("Configuration Space");
			
			currentMachine = new constraint3ArmsMachineUp(frames[0].drawArea,null);
			testRun();
			currentSpace = new ConstraintedSpaceUp(frames[1].drawArea,currentMachine,false);
			frames[0].drawArea.setCurrentObject(currentMachine);
			frames[1].drawArea.setCurrentObject(currentSpace);
			/*if (firstFrame==null){
				firstFrame = new Thread(this,"FirstFrame");
				firstFrame.start();
			} */
			};break;
		case 3:{
			currentSpace = new Demo(frames[1].drawArea,currentMachine);
			frames[1].drawArea.setCurrentObject(currentSpace);
			/*if (firstFrame==null){
				firstFrame = new Thread(this,"FirstFrame");
				firstFrame.start();
			} */
			 };break;
		case 4:{
			labels[1].setText("Configuration Space");
			currentMachine = new constraint3ArmsMachine(frames[0].drawArea,null);
			testRun();
			currentSpace = new ConstraintSpaceDemo(frames[1].drawArea,currentMachine);
			frames[0].drawArea.setCurrentObject(currentMachine);
			frames[1].drawArea.setCurrentObject(currentSpace);
			/*if (firstFrame==null){
				firstFrame = new Thread(this,"FirstFrame");
				firstFrame.start();
			} */
			};break;

  		}
		if (frameNumber!=3){	
			sign = 1;
			anchors = currentMachine.machine.anchors;
			if (frameNumber==2)	currentMachine.mouseClicked(anchors[1].x,anchors[1].y);
			spaceSign=1;
			times = 0;
		}
		startThread(); 
	}
	public void start(){
				 changeFrame(0);
	}
	private void testRun(){
		int rounds=0;
		int sign = 1;
		while(rounds<2){
			try{
				currentMachine.keyTyped(sign);
			}catch(MachineException e){
				 sign = sign*(-1);
				 rounds++;
			}
		}
	}
	public void stop(){
		super.stop();
		currentMachine = null;
		currentSpace = null;
		anchors = null;
	}

	public void changeFrames(){
		 if (frameNumber==3)
			frames[1].drawArea.repaint();
		 else{
			try{
				currentMachine.keyTyped(sign);
				frames[0].drawArea.repaint();
				if (currentSpace!=null){
					currentSpace.keyTyped(spaceSign);
					frames[1].drawArea.repaint();
				}
			}catch(MachineException e){
			     switch (sign){
					case 1:	currentMachine.mouseClicked(anchors[0].x,anchors[0].y);break;
					case -1:{
						if ((frameNumber!=2)&&(frameNumber!=1)) currentMachine.mouseClicked(anchors[1].x,anchors[1].y);
						else spaceSign = spaceSign*(-1);
							}break;
				 }
				 sign = sign*(-1);
				 if (times==0) times++;
				 else times=0;
			}
		}
	}
}
