//Importing Libraries
import DBase;
import Level;

public var skin: GUISkin;
public var cSkin: GUISkin;

public var ball:Rigidbody;
private static var ballStatic:Rigidbody;

public var life:Rigidbody;
private static var lifeStatic:Rigidbody;

//Used for the backgrounds
public var texture1:Texture;
public var texture2:Texture;
public var texture3:Texture;
public var texture4:Texture;

public static var wldTop: float;	//Top Most Coordinate
public static var wldBot: float;	//Bottom Most Coordinate
public static var wldLef: float;	//Left Most Coordinate
public static var wldRig: float;	//Right Most Coordainte

private static var level:Level;		//Level Number

private static var gameScore:int;	//Game Score
private static var score:int;		//Total Score - All the levels
private static var timeLeft:int;	//The time
private static var comment:String;	//Comments of the commenter

private static var password:int;	//Used to show th password to load the game

function Start () {
	//Setup the coordinates
	setBoundaries();
	
	password = 0;	//Init Password
	
	//Initial Comment
	pushComment("Here we go!");
	
	//Showing Lifes
	var xAxis = -1.0;
	lifeStatic = life;
	
	lifeNo = 5;
	
	if(PlayerPrefs.HasKey("PlayerLife")){
		lifeNo = PlayerPrefs.GetInt("PlayerLife");
	}
	
	for(var i = 0 ; i < lifeNo; i++){
		var obj = Instantiate(lifeStatic, Vector3(xAxis,-1.3,2.8),Quaternion.identity);
		xAxis += 0.5;
	}
	
	//Instantiate Ball
	ballStatic = ball;
	generateBall();
	
	score = PlayerPrefs.GetInt("PlayerScore");
	
	//Setting Attributes
	if(PlayerPrefs.HasKey("PlayerGameScore")){
		gameScore = PlayerPrefs.GetInt("PlayerGameScore");
		score -= PlayerPrefs.GetInt("PlayerGameScore");
	}else{
		gameScore = 0;
	}
	
	//Setting up background
	var background = GameObject.FindGameObjectWithTag('background');
	background.GetComponent(GUITexture).texture = texture1;
	
	//Loading Level
	var db = new DBase();	//Initialzing Database
	level = db.getLevelById(PlayerPrefs.GetInt("Level"));	//Get Current Level
	
	//Check whether the time has been given (Game Load)
	if(PlayerPrefs.HasKey("GameTime")){
		timeLeft = PlayerPrefs.GetInt("GameTime");
	}else{
		timeLeft = level.getDuration();
	}
	
	//Used for the game timer
	yield countdown();
}

function Update(){
	save();
}

//This method is used to save the game. CTRL + K
public static function save(){
	if(Input.GetKey(KeyCode.K) && Input.GetKey(KeyCode.LeftControl)){
		var db = new DBase();
		Debug.Log(score);
		Debug.Log(gameScore);
		password = db.addSave(this.getLife(),timeLeft,gameScore,PlayerPrefs.GetInt("PlayerScore")+gameScore,PlayerPrefs.GetInt("Level"));
	}
}

//This method we will be able to get the boundaries of the camera coordinates as World Coordinates
private function setBoundaries(){
	wldLef = Camera.mainCamera.ScreenToWorldPoint(Vector3(0,0,0)).x;
	wldRig = Camera.mainCamera.ScreenToWorldPoint(Vector3(Screen.width,0,0)).x;
	wldTop = Camera.mainCamera.ScreenToWorldPoint(Vector3(0,0,0)).y;
	wldBot = Camera.mainCamera.ScreenToWorldPoint(Vector3(0,Screen.height,0)).y;
}

function OnGUI(){
	GUI.skin = cSkin;	//Assigning Style
	GUI.skin.label.fontSize = 20;
	GUI.Label(Rect(0,0,100,100),"Level "+level.getLevelID());	//Showing Level Number7
	GUI.skin = skin;	//Assigning Style
	GUI.color = Color.yellow;
	GUI.Label(Rect((Screen.width/2)-50,0,100,100),""+timeLeft);	//Showing Time Left
	if(password != 0){
		GUI.Label(Rect((Screen.width/2)-50,20,100,100),"Game Saved! Password: "+password);	//Showing Time Left
	}
	GUI.skin = cSkin;	//Assigning Style
	GUI.color = Color.white;
	GUI.Label(Rect(Screen.width - 50,0,100,100),""+gameScore+"/"+level.getScore());	//Showing Score
	
	GUI.Label(Rect(0,Screen.height-50,100,100),""+comment);	//Comments
	GUI.Label(Rect(Screen.width - 50,Screen.height-50,100,100),""+(score+gameScore));	//Total Score
	GUI.skin.label.fontSize = 100;
}

//This method will add scores
public static function addScore(){
	gameScore+=2;	//Add score by 2
	generateBall();	//Generate a new ball
	pushComment("Gooooooooaal!!");	//Add a commentary comment
	
	//If score is greater then the limit of the level, advice user
	if(score == level.getScore()){
		pushComment("#CHAMPION!");
	}
	
	//If limit score has been reached and the user keeps scoring, congratz user.
	if((score-1) > level.getScore()){
		pushComment("He's Still Going!!");
	}
}

//This method will keep track of the time limit of the user
private function countdown(){
	//If time left is greater then 0
	while(timeLeft > 0){
		//Subtract the time by 1
		timeLeft -= 1;
		//if time is equals to 20, advice the user that time is almost up
		if(timeLeft == 20){
			pushComment("Time is almost up!");
		}
		yield WaitForSeconds(1);	//Repeat this method every 1 second
	}
	
	//If the time has passed
	if(timeLeft <= 0){
		if(gameScore >= level.getScore()){
			score += gameScore;	//add total score with the game score
			PlayerPrefs.SetInt("PlayerScore",score);	//Save it in the PlayerScore playerprefs
			PlayerPrefs.SetInt("PlayerGameScore",gameScore);
			PlayerPrefs.SetInt("Level",level.getLevelID()+1);	//Increment the level
			Application.LoadLevel(2);	//Load same scene
		}else{
			Application.LoadLevel(3);	//if score hasn't been reached, show lost scene.
		}
	}
}

//This method will create a new basketball
public static function generateBall(){
	Instantiate(ballStatic,Vector3(0,0,4),Quaternion.identity);
}

//This method will create a new comment
public static function pushComment(com:String){
	comment = com;
}

private static function getLife(){
	var lifeArray = GameObject.FindGameObjectsWithTag('life');	//Getting number of lifes
	return lifeArray.Length;
}

//This method will add a life
public static function addLife(){
	var lifeArray = GameObject.FindGameObjectsWithTag('life');	//Getting number of lifes
	if(lifeArray.Length < 5){
		//If the user has less then 5 lifes, a new life is added
		Instantiate(lifeStatic, Vector3((lifeArray.Length * 0.5) - 1,-1.3,2.8),Quaternion.identity);
	}
}

//This method will subtract a life
public static function subLife(){
	var lifeArray = GameObject.FindGameObjectsWithTag('life');	//Getting number of lifes
	if(lifeArray.Length > 1){
		//If life is greater then 1, subtract a life
		Destroy(lifeArray[lifeArray.Length-1]);
	}else if(lifeArray.Length == 1){
		Application.LoadLevel(3);
	}
}