import java.awt.*;
import java.util.Date;
public class TW extends Panel
{
	public int ready = 0;
	int Ospeed = 100;
	int  Wspeed = 100;
	long  timedelay = 500;
	int Pulsetime = 100;
	int ScrnSize = 200; //move to adjustable variable depending on resize event
	int ScrnLen = 1000; //
	int cycle = 0;		//number of loops preformed..
	int scale = 1000;	//Number of meters per screen	
	int position = 0;   //current Position of object in pixels
	int I = 0;
	int waves[][] = new int[12][2];
	int MpC = 0;
	DopplerWave Page1;
	Controls controls;
	DopplerScale scale1;
	long timestamp = 0;
	long timenext = 0;
	//------------------
	
	public TW(String title,int Ospeed,int Wspeed, int Pulsetime)
	{
		
		this.Ospeed = Ospeed;
		this.Wspeed = Wspeed;
		this.Pulsetime = Pulsetime;
	}
	public void start()
	{
		this.setLayout(new BorderLayout());
		DopplerWave Page1 = new DopplerWave(0,0,ScrnSize,waves,Wspeed,scale,0);
		DopplerScale scale1 = new DopplerScale(1000);
		Controls controls = new Controls();
//------System.out.println("Objects inited");
		Page1.resize(1000,200);
		scale1.resize(1000,100);
		controls.resize(1000,100);
		this.resize(1000,400);
		Page1.repaint(1);
		scale1.show();
		controls.show();
		this.add("North",Page1);
		this.add("Center",scale1);
		this.add("South",controls);		
		this.show();
		
//Auto scale function-----------------------------------------
		if (Ospeed >= 100)
			scale = 1000;
		if (Ospeed >= 1000)
			scale = 10000;
		if (Ospeed >= 10000)
			scale = 100000;
		if (Ospeed >= 100000)
			scale = 1000000;
		Page1.scale = scale;
		scale1.Scale = scale;

//------------------------------------------------------
// MpC is meters per cycle
		int MpC = scale / 10;
		Page1.MpC = MpC;
		
//Pass varibles back up--------------------------------
		this.Page1 = Page1;
		this.controls = controls;
		this.scale1 = scale1;
		this.MpC = MpC;
	}


//-----------------------------------------------------------------	
//this is the loop that is passed to the new thread "threadloop"
	public void loop()
	{
		DopplerWave Page1 = this.Page1;
		Controls controls = this.controls;
		DopplerScale scale1 = this.scale1;
		long timenext = this.timenext;
		long timestamp = this.timestamp;
		int position = this.position;
		int cycle = this.cycle;
		int Ospeed = this.Ospeed;
		int Wspeed = this.Wspeed;
		int ready = this.ready;
		
		
	

		for (this.position=0;this.position<this.ScrnLen;this.position = this.position)
		{

//------------------timer usage---------------------------------------------
			Date timer = new Date();
			timestamp = timer.getTime();
			int painted = 0;
			while ( timenext - timestamp <= controls.DelayTime || controls.DelayTime >= 495)
			{
				if (controls.ready == 0)
				{
					VarInit SpeedSet = new VarInit(this.Ospeed,this.Wspeed,this.Pulsetime);
					controls.hide();
					SpeedSet.resize(1000,100);
					this.remove(controls);
					this.add("South",SpeedSet);
					SpeedSet.show();
					this.layout();
					while (SpeedSet.busy != 1);
					this.Ospeed = SpeedSet.Ospeed;
					this.Wspeed = SpeedSet.Wspeed;
					Page1.WaveSpeed = SpeedSet.Wspeed;
					this.Pulsetime = SpeedSet.PulseTime;
					SpeedSet.hide();
					this.remove(SpeedSet);
					this.add("South",controls);
					controls.show();
					controls.DelayTime = 500;
					int scale = 0;
					if (this.Ospeed >= 100)
						scale = 1000;
					if (this.Ospeed >= 1000)
						scale = 10000;
					if (this.Ospeed >= 10000)
						scale = 100000;
					if (this.Ospeed >= 100000)
						scale = 1000000;
					Page1.scale = scale;
					scale1.Scale = scale;
					scale1.repaint(0);
					this.MpC = scale / 10;
					Page1.MpC = this.MpC;
					this.cycle = 0;
					this.position = 0;
					this.waves[0][0] = 0;
					this.layout();
					controls.ready = 1;
					controls.repaint(0);
					this.repaint(0);
					
				}



				Date timer2 = new Date();
				timenext = timer2.getTime();
//--OBSLOC-HITS-------------------------------------------------------------
				int offtime = this.cycle - this.waves[this.waves[0][0]][0];
				
				if (controls.ObsTyp != scale1.ObsTyp)
				{
					scale1.ObsTyp = controls.ObsTyp;
					scale1.repaint(0);
				}
				if (controls.ObsLoc != scale1.ObsLoc)
				{
					scale1.ObsLoc = controls.ObsLoc;
					scale1.repaint(0);
				}	
				if (this.position >= scale1.ObsLoc && scale1.ObsTyp == 0)
				{
					controls.DelayTime = 500;
					if (painted == 0)
					{
						controls.repaint(0);
						painted = 1;
					}
				}
				else if ( (((this.waves[this.waves[0][0]][1] + offtime*(this.Wspeed/this.MpC)) >= scale1.ObsLoc) || ((this.waves[1][0] + (this.cycle - this.waves[1][0])*(this.Wspeed/this.MpC)) >= scale1.ObsLoc)) && scale1.ObsTyp == 1)
				{
					controls.DelayTime = 500;
					if (painted == 0)
					{
						controls.repaint(0);
						painted = 1;
					}
				}
//--DEBUG-------System.out.println("FirstWave:" + (this.waves[1][1] + (this.cycle - this.waves[1][0])*(this.Wspeed/this.MpC)) + " LastWave:" + (this.waves[this.waves[0][0]][1] + offtime*(this.Wspeed/this.MpC)) + " Loc:" +scale1.ObsLoc); 
			//----------------------------------------------------------------------
			
			}
//-----------------------------------------------------------------------------	
// on 100,000 scale 1 cycle = 10 ms 
			if (this.cycle % (this.Pulsetime / 10) == 0 && this.waves[0][0] < 11)
			{
//--DEBUG-------System.out.println("Cycle:" + cycle + " Pulsetime:" + Pulsetime + " mod:" + (cycle % (Pulsetime/10)));
				this.waves[0][0] = this.waves[0][0] + 1;
				this.waves[this.waves[0][0]][0] = this.cycle;
				this.waves[this.waves[0][0]][1] = this.position;
				Page1.position = this.position;
				Page1.time = this.cycle;
				Page1.waves = this.waves;
			} 
			else
			{
				Page1.position = this.position;
				Page1.time = this.cycle;
			}
			this.cycle++;
			this.position = this.position + (this.Ospeed / this.MpC);
			Page1.repaint(0);

		}
		this.cycle = 0;
		this.position = 0;
		this.waves[0][0] = 0;
	} 
}