#pragma strict
static var PC:PlaneController;
static var start:boolean;
static var i:int;
static var gameover:boolean;
static var gamewon:boolean;
static var score:int;
static var initialTargets:int;
static var remainingTargets:int;

var rocketPrefab:GameObject;
var explosion:GameObject;
var fuel:float;
var FireTip:Transform;
var targets:GUIText;
var fuelText:GUIText;
var gameStatus:GUIText;
var gameScore:GUIText;
var level:int;
static var countdown:String[];
static var firstgo:boolean=true;
function Start () {
 	countdown=new String[4];
	remainingTargets=initialTargets = TargetGenerator.targetNumber;
	targets.text= "Targets: "+remainingTargets+"/"+initialTargets;
	fuelText.text="Fuel: "+fuel+"%";
	gameStatus.text="";
	gameScore.text="";
	start=false;
	i=0;
	gameover=false;
	gamewon=false;
}
function Awake()
{
	if(PC!=null)
	{
		GameObject.Destroy(PC);
	}
	PC=this;
}
function Update () {
	
	if (Input.GetKeyDown ("e"))
	{	
			
			if(gameover!=true)
			{	
				if(firstgo)DoCountdown();
				start=!start;
				Pause();
			}
	}
	if(start==true)
	{
	
		targets.text= "Targets: "+remainingTargets+"/"+initialTargets;
		UseFuel();
		var h = Input.GetAxis("Horizontal") * Time.deltaTime * 5;
		var v = Input.GetAxis("Vertical") * Time.deltaTime * 5;
		transform.Translate(0, h*1, -v);
		transform.Rotate(v*10, h*5, 0);
		transform.Translate(h*5,-i*Time.deltaTime*3,0);
		if(i!=10)
			i++;

		if(Input.GetKey("z"))
		{
			transform.Rotate(0,0,-Time.deltaTime*10);
		}
		if(Input.GetKey("x"))
		{
			//transform.Translate(Time.deltaTime*10,0,0);
			//transform.RotateAround(0, Time.deltaTime*10, 0);
			transform.Rotate(0,0,Time.deltaTime*10);
		}	
	}
	if(Input.GetKeyDown(KeyCode.Space))
	{
		Shooting();
		
	}
	if(remainingTargets==0)
	{
		Win();
	}
}
function Shooting()
{
	var rocket:GameObject=GameObject.Instantiate(rocketPrefab,FireTip.position,FireTip.rotation);

}
function UseFuel()
{
	if(start==true)
	{
		if(fuel>0)
		{
			fuel-=Time.deltaTime;
			fuelText.text="Fuel: "+fuel+"%";
		}
		else
		{
			GameOver("You wasted all your fuel!");
		}
		if(fuel<10)
		{
			fuelText.color=Color.red;
		}
	}
}
function GameOver(Message:String)
{	
	gameover=true;
	start=false;
	targets.text="";
	fuelText.text="";
	gameStatus.text="You Lost!";
	gameScore.text=Message;
	
}
function Pause()
{	
	if(start==false)
	{
		gameStatus.text="Game Paused";
		gameScore.text="Press e to continue";
	}
	else
	{
		gameStatus.text="";
		gameScore.text="";
	}
}
function Win()
{
	gameover=true;
	gamewon=true;
	start=false;
	targets.text="";
	fuelText.text="";
	gameStatus.text="You Won!";
	score=(fuel)*initialTargets;
	gameScore.text="Your final score is "+score;
}
function DoCountdown()
{	if(firstgo==true)
	{
	countdown[0]="";
	countdown[1]="Ready";
	countdown[2]="Set";
	countdown[3]="Go!";
	for(var c in countdown)
	{
		gameStatus.text=c;
		yield WaitForSeconds(0.5);
	}
	gameStatus.text="";
	firstgo=false;
	start=true;
	}
}
function OnCollisionEnter(col:Collision)
{
	if(start!=false)
	{
	
	Instantiate(explosion,transform.position,transform.rotation);
	GameOver("You crashed!");
	}
}

function OnGUI()
{
	if(gameover==true)
	{
		if(gamewon==true)
		{
			if(level==2)
			{
				if(GUI.Button(Rect(480,500,120,50),"Next level"))
				{
					Application.LoadLevel(3);
				}
			}
			if(level==3)
			{
			
			}
		}
		else
		{
			if(GUI.Button(Rect(480,500,120,50),"Restart"))
			{
				Application.LoadLevel(Application.loadedLevel);
			}
		}
		if(GUI.Button(Rect(650,500,120,50),"Go to Menu"))
		{
			Application.LoadLevel (1); 
		}	
	}
}