#pragma strict
var Skin:GUISkin;
var ViewHighScores:boolean;
var LoadGame:boolean;
var HighScoresFetched: boolean;
var HighScores: Array;
var NewGame:boolean;
var UserName:String;
var Password: String;
var LoadGameFetched: boolean;
var UserDetails: Array;
var LoadDataDBOperations: boolean;
var result: Array;
var HighScoreUser: int;
var userExists:boolean;


function Start () {
	
	HighScoresFetched = false;
	LoadDataDBOperations = false;
	userExists = false;
	UserName = "test";
	Password = "test";
}	

function Update () {

}

function OnGUI()
{
	GUI.skin = Skin;
	
	GUI.color = Color.white;
	var width: int  = Screen.width;
	var height: int  = Screen.height;
	var db:dbAccess = GameObject.FindGameObjectWithTag("MainCamera").GetComponent(dbAccess);
	
	if(LoadGameFetched)
	{
		
		if(!LoadDataDBOperations)
		{
			result = db.GetSaveGames(UserName, Password);
			HighScoreUser = db.GetHighScore(MainSettings.GetPlayerName());
			LoadDataDBOperations = true;
		}
		
		GUI.color = Color.black;
		GUI.Label(Rect((width/2) - 100, ((height/4)-50) ,200,100), "Select your game: ");
		GUI.Label(Rect((width/2) - 100, ((height/4)) ,200,100), "High Score: "+ HighScoreUser) ;
		
		if (GUI.Button(Rect((width/2) - 100, ((height/4)-50) + (height/2) ,200,50),"Back"))
		{
			LoadGameFetched = false;
			LoadDataDBOperations = false;
		}
				
		MainSettings.SetPlayerId(parseInt(result[0].ToString()));
		
		if (GUI.Button(Rect((width/2) - 100, ((height/4)-50) + (height/4) + 0  ,200,100),result[1].ToString()))
			{
				db = GameObject.FindGameObjectWithTag("MainCamera").GetComponent(dbAccess);
				UserDetails = db.LoadUserDetails(parseInt(result[0].ToString()));
				MainSettings.SetToLoad(parseInt(result[0].ToString()));
				
				Application.LoadLevel(parseInt(UserDetails[7].ToString()));
				MainSettings.SetPlayerName(UserDetails[0].ToString());
				MainSettings.SetScore(parseInt(UserDetails[1].ToString()));
				MainSettings.SetLives(parseInt(UserDetails[2].ToString()));
				var IsInvincible = UserDetails[3].ToString() == 'true';
				MainSettings.SetInvincible(IsInvincible, false);
				MainSettings.SetFuel(parseInt(UserDetails[4].ToString()));
				MainSettings.SetXpos(parseInt(UserDetails[5].ToString())) ;
				MainSettings.SetInvincibilities(parseInt(UserDetails[6].ToString()));
				MainSettings.SetTimeLeft(parseInt(UserDetails[8].ToString()));				
			}
	}
	else if(NewGame)
	{
		GUI.color = Color.black;
	
		GUI.Label(Rect((width/2) - 100, ((height/4)-50) ,200,100), "Enter your name & password");
		
		if(userExists)
		{
			GUI.Label(Rect((width/2) - 100, ((height/4)) ,200,100), "User exists! ");
		}
		
		UserName = GUI.TextField (Rect((width/2) - 100, ((height/4)-50) + (height/4) ,200,50), UserName);
		
		Password = GUI.TextField (Rect((width/2) - 100, ((height/4)-50) + (height/8) + 150 ,200,50), Password);
		
		if (GUI.Button(Rect((width/2) - 100, ((height/4)-50) + (height/2)  ,200,100),"Play Game!"))
		{
			MainSettings.SetPlayerName(UserName);
		
			if(db.InsertUser(UserName, Password))
			{
				Application.LoadLevel(1);
				Debug.Log("true");
			}
			else
			{
				userExists = true;
			}
		}
	}
	else if(ViewHighScores)
	{
		GUI.color = Color.black;
		GUI.Label(Rect((width/2) - 100, (height/4)-50	,200,100), "High Scores");
		
		
		if(!HighScoresFetched)
		{
			HighScores = db.GetHighScores();
			HighScoresFetched = true;
		}
		
		var counter: int = 1;
		for (var HighScore: String in HighScores)
		{
			GUI.Label(Rect((width/2) - 200, ((height/8) +10) + (50*counter) ,400,100), HighScore);
			counter++;
		}		
		
		if (GUI.Button(Rect((width/2) - 100, ((height/4)) + (height/2) ,200,50),"Back"))
		{
			ViewHighScores = false;
			HighScoresFetched = false;
			LoadGame = false;
		}
	}
	else if(LoadGame)
	{
		GUI.color = Color.black;
		GUI.Label(Rect((width/2) - 100, ((height/4)-50) ,200,100), "Enter your name & password");
		
		UserName = GUI.TextField (Rect((width/2) - 100, ((height/4)-50) + (height/4) ,200,50), UserName);
		
		Password = GUI.TextField (Rect((width/2) - 100, ((height/4)-50) + (height/8) + 150 ,200,50), Password);
		
		if (GUI.Button(Rect((width/2) - 100, ((height/4)-50) + (height/2)   ,200,100),"Search Games"))
		{
			MainSettings.SetPlayerName(UserName);
			LoadGameFetched = true;
			LoadGame = false;
		}
	}
	else
	{
		if (GUI.Button(Rect((width/2) - 100, (height/4)-50	,200,100),"New Game"))
		{
			NewGame = true;
		}
		
		if (GUI.Button(Rect((width/2) - 100, ((height/4)-50) + (height/4) ,200,100),"Load Game"))
		{
			LoadGame = true;
		}
		
		if (GUI.Button(Rect((width/2) - 100, ((height/4)-50) + (height/2) ,200,100),"High Scores"))
		{
			ViewHighScores = true;
		}
	}
	
	
	GUI.color = Color.black;
	GUI.Label(Rect(0,0,300,100), "Autobahn to heaven");

}