#pragma strict

var db:DBAccess;
var infoList:ArrayList;
var saveList:ArrayList;

var hasSaved:boolean;

var enemy:Rigidbody;
var bouncyenemy:Rigidbody;
var rightmost:int;
var height:int;

var materials:Material[];

var lives:int;
var score:int;
var level:int;

var fontStyle:GUISkin;
var buttonStyle:GUISkin;

var texture1:Texture2D;
var texture2:Texture2D;
var texture3:Texture2D;

var pName:String;
var pSurname:String;
var pAge:String;
var pScore:String;

function Start ()
{	
	if (Application.loadedLevel == 1 || Application.loadedLevel == 2 || Application.loadedLevel == 3) {
		db = new DBAccess();
		db.OpenDB();
	
		try {
			db.CreateTable("Player", new Array("name", "surname", "age", "score"), new Array("text", "text", "text", "text"));		
			db.CreateTable("Save", new Array("lives", "score", "level"), new Array("int", "int", "int"));	
		}
		catch(e) {
			//Since tables already exist, don't do anything
		}	
	}

	if (Application.loadedLevel == 1) {
		saveList = db.GetSaveGame();
	
		lives = 10;
		score = 0;
		level = 1;	

		if (PlayerPrefs.GetInt("isLoading") == 1 && saveList.Count == 3) {
			lives = saveList[0];
			score = saveList[1];
			level = saveList[2];
			
			PlayerPrefs.SetInt("isLoading", 0);
		}

		materials = new Material[5];
		materials[0] = Resources.Load("Blue Material") as Material;
		materials[1] = Resources.Load("Red Material") as Material;
		materials[2] = Resources.Load("Cyan Material") as Material;
		materials[3] = Resources.Load("Yellow Material") as Material;
		materials[4] = Resources.Load("Magenta Material") as Material;
	
		rightmost = Camera.main.ScreenToWorldPoint(Vector3(Screen.width, 0, 0)).x;
				
		yield GenerateEnemies();
	}
	else if (Application.loadedLevel == 2) 
		infoList = db.GetTopHighScores();
	else if (Application.loadedLevel == 3) 
		hasSaved = false;
}

function Update ()
{
	if (Application.loadedLevel == 1) {
		height = Random.Range(0, 4);
		
		if (score >= 20)
			level = 3;
		else if (score >= 10)
			level = 2; 
		
		if (lives == 0) {				
			PlayerPrefs.SetInt("Player Score", score);
			db.CloseDB();
			Application.LoadLevel(3);
		}
		
		if (Input.GetKeyDown(KeyCode.S)) {
			db.DeleteLastSave();
			db.InsertInto("Save", new Array(lives, score, level));
			Application.LoadLevel(0);
		}
	}
}

function OnGUI()
{
	if (Application.loadedLevel == 1) {
		GUI.color = Color.red;
		GUI.skin = fontStyle;	
		GUI.Label(Rect(20, 20, 300, 50), "Lives: " + lives);
		GUI.color = Color.blue;
		GUI.Label(Rect(20, 40, 300, 50), "Score: " + score);	
		GUI.Label(Rect(20, 60, 300, 50), "Level: " + level);	
		GUI.Label(Rect(20, 80, 300, 50), "Press S to Save");	
	
		if (level == 1)
			GameObject.FindGameObjectWithTag("Background").guiTexture.texture = texture1;
		else if (level == 2)
			GameObject.FindGameObjectWithTag("Background").guiTexture.texture = texture2;
		else if (level == 3)
			GameObject.FindGameObjectWithTag("Background").guiTexture.texture = texture3;
	}
	else {
		GUI.skin = buttonStyle;
	}

	if (Application.loadedLevel == 0) {
		if(GUI.Button(Rect((Screen.width/2)-80, (Screen.height/2)-20, 200, 40), "Play"))
			Application.LoadLevel(1);
		else if (GUI.Button(Rect((Screen.width/2)-80, (Screen.height/2)+30, 200, 40), "Load Saved Game")) {
			PlayerPrefs.SetInt("isLoading", 1);
			Application.LoadLevel(1);
		}
		else if (GUI.Button(Rect((Screen.width/2)-80, (Screen.height/2)+80, 200, 40), "High Scores"))
			Application.LoadLevel(2);		
	}	
	else if (Application.loadedLevel == 2) {
		if (GUI.Button(Rect((Screen.width/2)-750, (Screen.height/2)-150, 200, 50), "Main Menu")) {
			db.CloseDB();
 			Application.LoadLevel(0);
		}
	
		GUI.color = Color.yellow;
		GUI.Label(Rect((Screen.width/2)-450, (Screen.height/2)-150, 300, 40), "***Top Sonic Jumper High Scores***");
		
		var inc:int = 5;
		
		for (var col:ArrayList in infoList) {
			GUI.Label(Rect((Screen.width/2)-450, (Screen.height/2)-(inc*20), 700, 40), "Name: " + col[0] + " Surname: " + col[1] + " Age: " + col[2] + " Score: " + col[3]);
			inc--;
		}
		
		GUI.color = Color.yellow;
		GUI.Label(Rect((Screen.width/2)-450, (Screen.height/2)+50, 300, 40), "Enter your Name:" );
		pName = GUI.TextField(Rect((Screen.width)/2-330, ((Screen.height)/2)+50, 200, 20), pName, 200);
			
		if (GUI.Button(Rect((Screen.width/2)-450, (Screen.height/2)+80, 200, 20), "Load Your Highest Score"))
				pScore = db.GetPlayerTopScore(pName);
						
		if (pScore != null && pScore != "")
			GUI.Label(Rect((Screen.width/2)-450, (Screen.height/2)+110, 300, 40), "Your Highest Score is: " + pScore);
	}
	else if(Application.loadedLevel == 3) {
		for(go in GameObject.FindGameObjectsWithTag("Bouncy"))
			Destroy(go);
			
		for(go in GameObject.FindGameObjectsWithTag("Square"))
			Destroy(go);
	
		if (GUI.Button(Rect((Screen.width/2)-300, (Screen.height/2)-300, 300, 50), "Main Menu"))
			Application.LoadLevel(0);
 		
 		GUI.Label(Rect((Screen.width/2)-300, (Screen.height/2)-240, 300, 40), "Name: ");
 		pName = GUI.TextField(Rect((Screen.width/2)-200,((Screen.height)/2)-240,200,20), pName, 200);
 		
 		GUI.Label(Rect((Screen.width/2)-300, (Screen.height/2)-220, 300, 40), "Surname: ");
 		pSurname = GUI.TextField(Rect((Screen.width/2)-200,((Screen.height)/2)-220,200,20), pSurname, 200);
 		
 		GUI.Label(Rect((Screen.width/2)-300, (Screen.height/2)-200, 300, 40), "Age: "); 		
 		pAge = GUI.TextField(Rect((Screen.width/2)-200,((Screen.height)/2)-200,200,20), pAge, 200);
 	
 		if (GUI.Button(Rect((Screen.width/2)-200, (Screen.height/2)-170, 150, 30), "Save High Score")) {
 			if (pName != "" && pName != null && pSurname != "" && pSurname != null && pAge != "" && pAge != null)
 				SaveScore(pName, pSurname, pAge, PlayerPrefs.GetInt("Player Score").ToString());	 			
		}		
	}
}

function GenerateEnemies() {
	while(lives > 0) {
		if (Application.loadedLevel != 1)
			break;
	
		yield WaitForSeconds(2);
		
		if(Random.Range(1,4) != 3) {
			Instantiate(enemy, Vector3(rightmost + 0.5, -4.5 + height, 0), Quaternion.identity);
		}
		else {
			Instantiate(bouncyenemy, Vector3(rightmost + (height*3) + 0.5, -1, 0), Quaternion.identity);
			
			if(level >= 2)
				Instantiate(bouncyenemy, Vector3(rightmost + (height*4) + 1.5, -1, 0), Quaternion.identity);
			if(level == 3)
				Instantiate(bouncyenemy, Vector3(rightmost + (height*4) + 2.5, -1, 0), Quaternion.identity);
		}
	}		
}

function SaveScore(name, surname, age, score) {
	if (hasSaved)
		return;

	try	{
		db.InsertInto("Player", new Array("'"+name+"'", "'"+surname+"'", "'"+age+"'", "'"+score+"'"));
		hasSaved = true;
	}
	catch(e) {
		
	}
}