#pragma strict
var ScrollSpeed:float;
var FuelImage:Texture2D;
var Skin: GUISkin;
var SavedScores:boolean;
var ToShowOnScreen:String;
var db:dbAccess;
var HighScore:int;

function Start () {
	MainSettings.SetScrollSpeed(ScrollSpeed);
	yield SetScore();
	MainSettings.ResetTime();
	SavedScores = false;
}

function Update () {
	if(MainSettings.OutOfFuel() || MainSettings.GetLives() == 0)
	{
		Time.timeScale = 0;
	}
	
}

function OnGUI()
{

	if(MainSettings.GetTimeLeft(false) < 0)
		{
			if(Application.loadedLevel < 3)
			{		
				Application.LoadLevel(Application.loadedLevel+1);
				MainSettings.SoftReset();
			}
			else
			{		
						if(!SavedScores)
						{					
							Time.timeScale = 0;
							SavedScores = true;
							db = GameObject.FindGameObjectWithTag("MainCamera").GetComponent(dbAccess);	
							HighScore = db.SaveScore(MainSettings.GetScore(), MainSettings.GetPlayerName());
							
							if(HighScore == -1)
							{
								//This is his high score
								ToShowOnScreen = "Congratulations, you just finished the game! Final score: "+ MainSettings.GetScore() + ". This is your highest score!";
							}
							else
							{
								ToShowOnScreen = "Congratulations, you just finished the game! Final score: "+ MainSettings.GetScore() + ". This is not your highest score; your highest score is "+ HighScore;
							}
							
						}
						

						GUI.Label(Rect((Screen.width/2) - 150 ,(Screen.height/4) - 50,300,100) , ToShowOnScreen);
							
						if (GUI.Button(Rect((Screen.width/2) - 150 ,(Screen.height/2) - 50,300,100),"Play again!"))
						{				
							MainSettings.Reset();
							Time.timeScale = 1;
							Application.LoadLevel(1);
							SavedScores = false;			
						}
			}
		}


	if(Time.timeScale == 0)
	{

		if(!SavedScores)
		{					
			SavedScores = true;
			db = GameObject.FindGameObjectWithTag("MainCamera").GetComponent(dbAccess);	
			HighScore = db.SaveScore(MainSettings.GetScore(), MainSettings.GetPlayerName());
			
			if(HighScore == -1)
			{
				//This is his high score
				ToShowOnScreen = "Game Over! Final score: "+ MainSettings.GetScore() + ". This is your highest score!";
			}
			else
			{
				ToShowOnScreen = "Game Over! Final score: "+ MainSettings.GetScore() + ". This is not your highest score; your highest score is "+ HighScore;
			}
			
		}
		

		GUI.Label(Rect((Screen.width/2) - 150 ,(Screen.height/4) - 50,300,100) , ToShowOnScreen);
			
		if (GUI.Button(Rect((Screen.width/2) - 150 ,(Screen.height/2) - 50,300,100),"Play again!"))
		{				
			MainSettings.Reset();
			Time.timeScale = 1;
			Application.LoadLevel(1);
			SavedScores = false;			
		}
	}
	else
	{
		GUI.skin = Skin;
		if(Application.loadedLevel == 2)
		{
			GUI.color = Color.white;
		}
		else
		{
			GUI.color = Color.black;
		}
		
		GUI.Label(Rect(0,0,100,100), "Score: "+MainSettings.GetScore());
		GUI.Label(Rect(0,20,100,100), "Lives: "+MainSettings.GetLives());
		GUI.Label(Rect(0,40,40,100), "Fuel: ");	
		GUI.color = Color.red;
		GUI.DrawTexture(Rect(40,45,(MainSettings.GetFuel()/100),17),FuelImage,ScaleMode.StretchToFill);
		if(Application.loadedLevel == 2)
		{
			GUI.color = Color.white;
		}
		else
		{
			GUI.color = Color.black;
		}
		GUI.Label(Rect(0,60,150,150), "Invincible: "+MainSettings.IsInvincible());
		GUI.Label(Rect(0,80,150,100), "Invincibities: "+MainSettings.GetInvincibilities());
		GUI.Label(Rect(0,100,150,150), "Time to Survive: "+MainSettings.GetTimeLeft(false));	
		GUI.Label(Rect(0,120,150,150), "Level: "+Application.loadedLevel);
	}
	
}


function SetScore()
{
	while(true){
		MainSettings.SetScore();
		MainSettings.GetTimeLeft(true);
		yield WaitForSeconds(1);
	}
}