#pragma strict
import System.Data;
import MySql.Data;

var connectionString;
var mySqlCommand:MySql.Data.MySqlClient.MySqlCommand;
var connection:MySql.Data.MySqlClient.MySqlConnection;
var mySqlDataReader:MySql.Data.MySqlClient.MySqlDataReader;
var id:long;
var	scoreResult = new Array();
var	timeResult = new Array();
var	objectXPosResults = new Array();
var	objectYPosResults = new Array();
var	objectTypesResults = new Array();

var pineapple:GameObject;
var apple:GameObject;
var lemon:GameObject;
var bomb:GameObject;
var upVector:Vector3;
private var myTime:float;
private var tempTime:float;
private var tempObj:GameObject;
var counter:int;
var xPos:int;
var gong:AudioClip;
var timer:boolean;
var endTime:int;
var timeLeft:int;
var startTime;
var timeSound:AudioClip;
var playSound:boolean;

function throwFruit(){
	//Infiniate While Loop
	while(true){
		//Declaration of the Random Seconds variable
		var randSeconds:int;
		xPos = Random.Range(-7,7);
		//randSeconds = Random.Range(1,3);
		
		if(counter <= 10){
			//Increment Counter
			counter++;
			
			//Random Seconds from 1-3 seconds
			randSeconds = Random.Range(1,4);
			
			//Random Fruit (example 1=Pineapple, 2=Apple, etc)
			var randFruit:int;
			randFruit = Random.Range(1,4);
			
			//If and Else statements depending on the random fruit to be shown
			if(randFruit == 1){
				//If randFruit == 1, Instantiate the Pineapple object
				tempObj = GameObject.Instantiate(pineapple,Vector3(xPos,-12,5), Quaternion(0,0,0,0));
			}else if(randFruit == 2){
				//If randFruit == 2, Instantiate the Apple object
				tempObj = GameObject.Instantiate(apple,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
			}else if(randFruit == 3){
				//If randFruit == 3, Instantiate the Lemon object
				tempObj = GameObject.Instantiate(lemon,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
			}else if(randFruit == 4){
				//If randFruit == 4, Instantiate the Bomb object
				tempObj = GameObject.Instantiate(bomb,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
				
			}			
			//Disable Kinematic of the Object
			tempObj.rigidbody.isKinematic = false;
			
			//Set a Random of the UpVector.x between -250 to 250 <- this is used to throw the fruit in a random x axis
			upVector.x = Random.Range(-250,250);
			
			//Set a Random of the UpVector.y between -250 to 250 <- this is used to throw the fruit in a random y axis (force of throw)
			upVector.y = Random.Range(700,900);
			
			//Add the Force to throw in the Given randoms of the upVector
			tempObj.rigidbody.AddForce(upVector);
			
			//If it is a pineapple or a bomb add a torquie in order to be thrown in a reamianing z axis
			if(randFruit == 1 || randFruit == 4){
				tempObj.rigidbody.AddTorque(Random.Range(-100,100),Random.Range(-100,100),Random.Range(-100,100));
			}
		}else if(counter > 10 && counter <= 20){
			//Increment Counter
			counter++;
			
			//Random Seconds from 1-3 seconds
			randSeconds = Random.Range(1,4);
			
			//Random Fruit (example 1=Pineapple, 2=Apple, etc)
			var randFruit2:int;
			randFruit2 = Random.Range(1,5);
			
			//If and Else statements depending on the random fruit to be shown
			if(randFruit2 == 1){
				//If randFruit2 == 1, Instantiate the Pineapple object
				tempObj = GameObject.Instantiate(pineapple,Vector3(xPos,-12,5), Quaternion(0,0,0,0));
			}else if(randFruit2 == 2){
				//If randFruit2 == 2, Instantiate the Apple object
				tempObj = GameObject.Instantiate(apple,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
			}else if(randFruit2 == 3){
				//If randFruit2 == 3, Instantiate the Lemon object
				tempObj = GameObject.Instantiate(lemon,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
			}else if(randFruit2 == 4){
				//If randFruit2 == 4, Instantiate the Bomb object
				tempObj = GameObject.Instantiate(bomb,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
				
			}
			
			//Disable Kinematic of the Object
			tempObj.rigidbody.isKinematic = false;
			
			//Set a Random of the UpVector.x between -250 to 250 <- this is used to throw the fruit in a random x axis
			upVector.x = Random.Range(-250,250);
			
			//Set a Random of the UpVector.y between -250 to 250 <- this is used to throw the fruit in a random y axis (force of throw)
			upVector.y = Random.Range(700,900);
			
			//Add the Force to throw in the Given randoms of the upVector
			tempObj.rigidbody.AddForce(upVector);
			
			//If it is a pineapple or a bomb add a torquie in order to be thrown in a reamianing z axis
			if(randFruit2 == 1 || randFruit2 == 4){
				tempObj.rigidbody.AddTorque(Random.Range(-100,100),Random.Range(-100,100),Random.Range(-100,100));
			}
		
		}else if(counter > 20 && counter <= 30){
			//Increment Counter
			counter++;
			
			//Random Seconds from 1-2 seconds
			randSeconds = Random.Range(1,3);
			
			//Random Fruit (example 1=Pineapple, 2=Apple, etc)
			var randFruit3:int;
			randFruit3 = Random.Range(1,8);
			Debug.Log(randFruit);
			//If and Else statements depending on the random fruit to be shown
			if(randFruit3 == 1){
				//If randFruit == 1, Instantiate the Pineapple object
				tempObj = GameObject.Instantiate(pineapple,Vector3(xPos,-12,5), Quaternion(0,0,0,0));
			}else if(randFruit3 == 2){
				//If randFruit == 2, Instantiate the Apple object
				tempObj = GameObject.Instantiate(apple,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
			}else if(randFruit3 == 3){
				//If randFruit == 3, Instantiate the Lemon object
				tempObj = GameObject.Instantiate(lemon,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
			}else if(randFruit3 == 4){
				//If randFruit == 4, Instantiate the Bomb object
				tempObj = GameObject.Instantiate(bomb,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
				
			}
			else if(randFruit3 >= 5){
				var qtyRand = Random.Range(2,4);
				Debug.Log(qtyRand);
				for(var i=0;i<qtyRand;i++){
					var togetherRand = Random.Range(1,4);
					if(togetherRand == 1){
						//If randFruit == 1, Instantiate the Pineapple object
						tempObj = GameObject.Instantiate(pineapple,Vector3(xPos,-12,5), Quaternion(0,0,0,0));
					}else if(togetherRand == 2){
						//If randFruit == 2, Instantiate the Apple object
						tempObj = GameObject.Instantiate(apple,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
					}else if(togetherRand == 3){
						//If randFruit == 3, Instantiate the Lemon object
						tempObj = GameObject.Instantiate(lemon,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
					}
					//Disable Kinematic of the Object
					tempObj.rigidbody.isKinematic = false;
					
					//Set a Random of the UpVector.x between -250 to 250 <- this is used to throw the fruit in a random x axis
					upVector.x = Random.Range(-250,250);
					
					//Set a Random of the UpVector.y between -250 to 250 <- this is used to throw the fruit in a random y axis (force of throw)
					upVector.y = Random.Range(700,900);
					
					//Add the Force to throw in the Given randoms of the upVector
					tempObj.rigidbody.AddForce(upVector);
				}
			}
			
			
			if(randFruit3 < 5){
				//Disable Kinematic of the Object
				tempObj.rigidbody.isKinematic = false;
				
				//Set a Random of the UpVector.x between -250 to 250 <- this is used to throw the fruit in a random x axis
				upVector.x = Random.Range(-250,250);
				
				//Set a Random of the UpVector.y between -250 to 250 <- this is used to throw the fruit in a random y axis (force of throw)
				upVector.y = Random.Range(700,900);
				
				//Add the Force to throw in the Given randoms of the upVector
				tempObj.rigidbody.AddForce(upVector);
				
				//If it is a pineapple or a bomb add a torquie in order to be thrown in a reamianing z axis
				if(randFruit3 == 1 || randFruit3 == 4){
					tempObj.rigidbody.AddTorque(Random.Range(-100,100),Random.Range(-100,100),Random.Range(-100,100));
				}
			}
		}else if(counter > 30 && counter <= 40){
			//Increment Counter
			counter++;
			Debug.Log("****************************** OK ENTERED *************************************");
			//Random Seconds from 1-2 seconds
			randSeconds = Random.Range(1,3);
			
			//Random Fruit (example 1=Pineapple, 2=Apple, etc)
			var randFruit4:int;
			randFruit4 = Random.Range(1,10);
			
			//If and Else statements depending on the random fruit to be shown
			if(randFruit4 == 1){
				//If randFruit4 == 1, Instantiate the Pineapple object
				tempObj = GameObject.Instantiate(pineapple,Vector3(xPos,-12,5), Quaternion(0,0,0,0));
			}else if(randFruit4 == 2){
				//If randFruit4 == 2, Instantiate the Apple object
				tempObj = GameObject.Instantiate(apple,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
			}else if(randFruit4 == 3){
				//If randFruit4 == 3, Instantiate the Lemon object
				tempObj = GameObject.Instantiate(lemon,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
			}else if(randFruit4 == 4){
				//If randFruit4 == 4, Instantiate the Bomb object
				tempObj = GameObject.Instantiate(bomb,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
			}
			else if(randFruit4 >= 5){
				var qtyRand4 = Random.Range(2,5);
				Debug.Log(qtyRand4);
				for(var j=0;j<qtyRand4;j++){
					var togetherRand4 = Random.Range(1,4);
					if(togetherRand4 == 1){
						//If randFruit == 1, Instantiate the Pineapple object
						tempObj = GameObject.Instantiate(pineapple,Vector3(xPos,-12,5), Quaternion(0,0,0,0));
					}else if(togetherRand4 == 2){
						//If randFruit == 2, Instantiate the Apple object
						tempObj = GameObject.Instantiate(apple,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
					}else if(togetherRand4 == 3){
						//If randFruit == 3, Instantiate the Lemon object
						tempObj = GameObject.Instantiate(lemon,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
					}
					//Disable Kinematic of the Object
					tempObj.rigidbody.isKinematic = false;
					
					//Set a Random of the UpVector.x between -250 to 250 <- this is used to throw the fruit in a random x axis
					upVector.x = Random.Range(-250,250);
					
					//Set a Random of the UpVector.y between -250 to 250 <- this is used to throw the fruit in a random y axis (force of throw)
					upVector.y = Random.Range(700,900);
					
					//Add the Force to throw in the Given randoms of the upVector
					tempObj.rigidbody.AddForce(upVector);
				}
			}
			
			
			if(randFruit4 < 5){
				//Disable Kinematic of the Object
				tempObj.rigidbody.isKinematic = false;
				
				//Set a Random of the UpVector.x between -250 to 250 <- this is used to throw the fruit in a random x axis
				upVector.x = Random.Range(-250,250);
				
				//Set a Random of the UpVector.y between -250 to 250 <- this is used to throw the fruit in a random y axis (force of throw)
				upVector.y = Random.Range(700,900);
				
				//Add the Force to throw in the Given randoms of the upVector
				tempObj.rigidbody.AddForce(upVector);
				
				//If it is a pineapple or a bomb add a torquie in order to be thrown in a reamianing z axis
				if(randFruit4 == 1 || randFruit4 == 4){
					tempObj.rigidbody.AddTorque(Random.Range(-100,100),Random.Range(-100,100),Random.Range(-100,100));
				}
			}
		}else{
			//Increment Counter
			counter++;
			
			//Random Seconds from 1-2 seconds
			randSeconds = Random.Range(1,3);
			
			//Random Fruit (example 1=Pineapple, 2=Apple, etc)
			var randFruit5:int;
			randFruit5 = Random.Range(1,12);
			
			//If and Else statements depending on the random fruit to be shown
			if(randFruit5 == 1){
				//If randFruit5 == 1, Instantiate the Pineapple object
				tempObj = GameObject.Instantiate(pineapple,Vector3(xPos,-12,5), Quaternion(0,0,0,0));
			}else if(randFruit5 == 2){
				//If randFruit5 == 2, Instantiate the Apple object
				tempObj = GameObject.Instantiate(apple,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
			}else if(randFruit5 == 3){
				//If randFruit5 == 3, Instantiate the Lemon object
				tempObj = GameObject.Instantiate(lemon,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
			}else if(randFruit5 == 4 || randFruit5 == 5 || randFruit5 == 6){
				//If randFruit5 == 4, Instantiate the Bomb object
				tempObj = GameObject.Instantiate(bomb,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
			}else if(randFruit5 >= 7){
				var qtyRand5 = Random.Range(2,6);
				Debug.Log(qtyRand5);
				for(var k=0;k<qtyRand5;k++){
					var togetherRand5 = Random.Range(1,5);
					if(togetherRand5 == 1){
						//If randFruit == 1, Instantiate the Pineapple object
						tempObj = GameObject.Instantiate(pineapple,Vector3(xPos,-12,5), Quaternion(0,0,0,0));
					}else if(togetherRand5 == 2){
						//If randFruit == 2, Instantiate the Apple object
						tempObj = GameObject.Instantiate(apple,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
					}else if(togetherRand5 == 3){
						//If randFruit == 3, Instantiate the Lemon object
						tempObj = GameObject.Instantiate(lemon,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
					}
					else if(togetherRand5 == 4){
						//If randFruit == 3, Instantiate the Lemon object
						tempObj = GameObject.Instantiate(bomb,Vector3(xPos,-10,5), Quaternion(0,0,0,0));
					}
					//Disable Kinematic of the Object
					tempObj.rigidbody.isKinematic = false;
					
					//Set a Random of the UpVector.x between -250 to 250 <- this is used to throw the fruit in a random x axis
					upVector.x = Random.Range(-250,250);
					
					//Set a Random of the UpVector.y between -250 to 250 <- this is used to throw the fruit in a random y axis (force of throw)
					upVector.y = Random.Range(700,900);
					
					//Add the Force to throw in the Given randoms of the upVector
					tempObj.rigidbody.AddForce(upVector);
				}
			}
			
			
			if(randFruit5 < 7){			
				//Disable Kinematic of the Object
				tempObj.rigidbody.isKinematic = false;
				
				//Set a Random of the UpVector.x between -250 to 250 <- this is used to throw the fruit in a random x axis
				upVector.x = Random.Range(-250,250);
				
				//Set a Random of the UpVector.y between -250 to 250 <- this is used to throw the fruit in a random y axis (force of throw)
				upVector.y = Random.Range(700,900);
				
				//Add the Force to throw in the Given randoms of the upVector
				tempObj.rigidbody.AddForce(upVector);
				
				//If it is a pineapple or a bomb add a torquie in order to be thrown in a reamianing z axis
				if(randFruit5 == 1 || randFruit5 == 4){
					tempObj.rigidbody.AddTorque(Random.Range(-100,100),Random.Range(-100,100),Random.Range(-100,100));
				}
			}
		}
		
		yield WaitForSeconds(randSeconds);
	}
}

function Start () {
	if(MainMenu2Controller.isLoad == true){
		getData();
		
		var i:int;
		i = 0;
		
		var checkObject:String;
		var checkX:int;
		var checkY:int;
		
		Debug.Log("Start");
		for(var t=0; t<objectTypesResults.length; t++){
			Debug.Log("Testing Here: " + objectTypesResults[t]);
			checkObject = objectTypesResults[t];
			checkX = int.Parse(objectXPosResults[t]);
			checkY = int.Parse(objectYPosResults[t]);
			if(checkObject == "1"){
				GameObject.Instantiate(pineapple,Vector3(checkX,checkY,5), Quaternion(0,0,0,0));
			}else if(checkObject == "2"){
				GameObject.Instantiate(apple,Vector3(checkX,checkY,5), Quaternion(0,0,0,0));
			}else if(checkObject == "3"){
				GameObject.Instantiate(lemon,Vector3(checkX,checkY,5), Quaternion(0,0,0,0));
			}else if(checkObject == "4"){
				GameObject.Instantiate(bomb,Vector3(checkX,checkY,5), Quaternion(0,0,0,0));
			}
		}
		
		for(var time in timeResult){
			endTime = Time.time + int.Parse(time);
			Debug.Log("Ti: " + int.Parse(time));
			break;
		}
		for(var score in scoreResult){
			MouseController.score = int.Parse(score);
			Debug.Log("Score: " + score);
			break;
		}
		
		
		counter = 0;
		timer = false;
		playSound = true;
		
		MainMenu2Controller.levelSelected = "Arcade";
		timer = true;
		yield WaitForSeconds(1);
		deleteInfo();
	}
	else{
		counter = 0;
		timer = false;
		endTime = Time.time+185;
		playSound = true;
		
		MainMenu2Controller.levelSelected = "Arcade";
		Debug.Log(MainMenu2Controller.levelSelected);
		//Display 3, 2, 1, SLICE!!! and Wait for 1 second between each countdown
		guiText.text = "3";
		yield WaitForSeconds(1);
		guiText.text = "2";
		yield WaitForSeconds(1);
		guiText.text = "1";
		yield WaitForSeconds(1);
		guiText.text = "SLICE!!!";
		audio.PlayOneShot(gong);
		yield WaitForSeconds(1);
		guiText.text = " ";
		
		
		timer = true;
	}
	
	//Call the Method Throw Fruit
	yield throwFruit();
}

function Update () {
	//if(timer == true){
		MainMenu2Controller.myTime = timeLeft;
		timeLeft=endTime-Time.time;
		Debug.Log("Time Left: " + timeLeft);
		if(timeLeft <= 0){
			Application.LoadLevel(3);
		}
	//}
	
	if(timer == true){
		guiText.anchor = TextAnchor.UpperRight;
		guiText.pixelOffset = Vector2(930,710);
		guiText.text = timeLeft+" sec";
		if(timeLeft <= 5){
			if(playSound == true){
				PlaySound();
				playSound = false;
				
			}
		}
	}
}

function PlaySound(){
	audio.PlayOneShot(timeSound);
}

function getData(){
	scoreResult = new Array();
	timeResult = new Array();
	objectXPosResults = new Array();
	objectYPosResults = new Array();
	
	try{
		connectionString = "Server=localhost;Database=fruit_slice;UserID=root;Password=;Pooling=true";
		connection = MySql.Data.MySqlClient.MySqlConnection(connectionString);
		
		connection.Open();
		
		//create the sql command
		mySqlCommand = MySql.Data.MySqlClient.MySqlCommand("SELECT score, time, object_xpos, object_ypos, object_type FROM state WHERE username = '" + MainMenuController.myUsername + "'", connection);
		
		//read data from db
		mySqlDataReader = mySqlCommand.ExecuteReader();
		
		while(mySqlDataReader.Read()){
			//the data reader is an array for each row
			scoreResult.Add(mySqlDataReader["score"].ToString());
			timeResult.Add(mySqlDataReader["time"].ToString());
			objectXPosResults.Add(mySqlDataReader["object_xpos"].ToString());
			objectYPosResults.Add(mySqlDataReader["object_ypos"].ToString());
			objectTypesResults.Add(mySqlDataReader["object_type"].ToString());
			//usernameResults.Add(mySqlDataReader["username"].ToString() + " - " + mySqlDataReader["game_type"].ToString());
			//gameTypeResults.Add(mySqlDataReader["game_type"].ToString());
		}
		
		//close db connection
		connection.Close();
		Debug.Log("Connection Closed");
	}catch(e){
		Debug.Log("Error: " + e);
	}
}

function deleteInfo(){
//Debug.Log(username+score);
	try{
		connection = MySql.Data.MySqlClient.MySqlConnection(connectionString);
		connection.Open();
		var command:MySql.Data.MySqlClient.MySqlCommand;
		var sql:String;
		sql = "DELETE FROM state WHERE username = '"+MainMenuController.myUsername+"'";
		command = MySqlClient.MySqlCommand(sql, connection);
		/*
		var param1:MySqlClient.MySqlParameter;
		var param2:MySqlClient.MySqlParameter;
		
		param1 = command.Parameters.AddWithValue("?username", username);
		param2 = command.Parameters.AddWithValue("?score", score);
		*/
		command.ExecuteNonQuery();
//		id = command.LastInsertedId();
	}catch(e){
		Debug.Log("Error: " + e);
	}
}