#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 results = new Array();
var count:int=0;

var param1:MySqlClient.MySqlParameter;
var param2:MySqlClient.MySqlParameter;
var param3:MySqlClient.MySqlParameter;
var param4:MySqlClient.MySqlParameter;
var param5:MySqlClient.MySqlParameter;
var param6:MySqlClient.MySqlParameter;
var param7:MySqlClient.MySqlParameter;

var myCamera:Camera;
var myMaterials:Material[];
var randomB:int;
var myPlayer:PlayerC;
var fontScore:GUISkin;
var fontLives:GUISkin;
var livePlus:AudioClip;
var chance:boolean;


var pineappleArray;
var appleArray;
var lemonArray;
var bombArray;
var pineappleQtySave:int;
var appleQtySave:int;
var lemonQtySave:int;
var bombQtySave:int;

//STATIC VARIABLES
static var score:int;
static var lives:int;

function changeBlade(){
	while(true){
		randomB = Random.Range(0,6);
		GetComponent(TrailRenderer).sharedMaterial = myMaterials[randomB];
		yield WaitForSeconds(3);
	}
}

function Start () {
	chance = true;
	myPlayer = new PlayerC("Clayton");
	yield changeBlade();
	getData();
}

function Update () {
	
	myPlayer.score = score;
	myPlayer.lives = lives;
	
	var mouse : Vector3 = myCamera.ScreenToWorldPoint(Input.mousePosition);
	if(Input.GetMouseButton(0) == true || Input.GetMouseButton(1) == true){
		mouse.z = 5;
	}else{
		mouse.z = 11;
	}
	transform.position = mouse;
	
	if(MainMenu2Controller.levelSelected == "Classic" && chance == true){
		if((myPlayer.score/100) == 1 && myPlayer.lives < 3){
			lives += 1;
			audio.PlayOneShot(livePlus);
			chance = false;
		}
	}
	
	if(MainMenu2Controller.levelSelected == "Classic" || MainMenu2Controller.levelSelected == "Arcade" || MainMenu2Controller.levelSelected == "Ninja" || MainMenu2Controller.levelSelected == "Slice")
	{
		if(Input.GetKeyDown(KeyCode.S)){
			getData();
			deleteInfo();
			try{
				connection = MySql.Data.MySqlClient.MySqlConnection(connectionString);
				connection.Open();
				var command:MySql.Data.MySqlClient.MySqlCommand;
				var sql:String;
		
		
		
				//pineappleQtySave = GameObject.FindGameObjectsWithTag("Pineapple").Length
				pineappleArray = GameObject.FindGameObjectsWithTag("Pineapple");
				for(var pineapple:GameObject in pineappleArray)
				{
					var pineappleX:double = pineapple.transform.position.x;
					var pineappleY:double = pineapple.transform.position.y;
					Debug.Log("Pineapple - X Coordinates: " + pineappleX + ", Y Coordinates:" + pineappleY);
					sql = "INSERT INTO state(username, game_type, score, live, time, object_xpos, object_ypos, object_type) VALUES(?username, ?gameType, ?score, ?lives, ?time, ?fruitX, ?fruitY, 1)";
					command = MySqlClient.MySqlCommand(sql, connection);
					
					param1 = command.Parameters.AddWithValue("?username", MainMenuController.myUsername);
					param2 = command.Parameters.AddWithValue("?gameType", MainMenu2Controller.levelSelected);
					param3 = command.Parameters.AddWithValue("?score", score);
					param4 = command.Parameters.AddWithValue("?lives", lives);
					param5 = command.Parameters.AddWithValue("?time", MainMenu2Controller.myTime);
					param6 = command.Parameters.AddWithValue("?fruitX", pineappleX);
					param7 = command.Parameters.AddWithValue("?fruitY", pineappleY);
					command.ExecuteNonQuery();
					
				
				}
				
				appleArray = GameObject.FindGameObjectsWithTag("Apple");
				for(var apple:GameObject in appleArray)
				{
					var appleX:double = apple.transform.position.x;
					var appleY:double = apple.transform.position.y;
					Debug.Log("Apple - X Coordinates: " + appleX + ", Y Coordinates:" + appleY);
					sql = "INSERT INTO state(username, game_type, score, live, time, object_xpos, object_ypos, object_type) VALUES(?username, ?gameType, ?score, ?lives, ?time, ?fruitX, ?fruitY, 2)";
					command = MySqlClient.MySqlCommand(sql, connection);
					
					param1 = command.Parameters.AddWithValue("?username", MainMenuController.myUsername);
					param2 = command.Parameters.AddWithValue("?gameType", MainMenu2Controller.levelSelected);
					param3 = command.Parameters.AddWithValue("?score", score);
					param4 = command.Parameters.AddWithValue("?lives", lives);
					param5 = command.Parameters.AddWithValue("?time", MainMenu2Controller.myTime);
					param6 = command.Parameters.AddWithValue("?fruitX", appleX);
					param7 = command.Parameters.AddWithValue("?fruitY", appleY);
					command.ExecuteNonQuery();
				}
				
				lemonArray = GameObject.FindGameObjectsWithTag("Lemon");
				for(var lemon:GameObject in lemonArray)
				{
					var lemonX:double = lemon.transform.position.x;
					var lemonY:double = lemon.transform.position.y;
					Debug.Log("Lemon - X Coordinates: " + lemonX + ", Y Coordinates:" + lemonY);
					sql = "INSERT INTO state(username, game_type, score, live, time, object_xpos, object_ypos, object_type) VALUES(?username, ?gameType, ?score, ?lives, ?time, ?fruitX, ?fruitY, 3)";
					command = MySqlClient.MySqlCommand(sql, connection);

					param1 = command.Parameters.AddWithValue("?username", MainMenuController.myUsername);
					param2 = command.Parameters.AddWithValue("?gameType", MainMenu2Controller.levelSelected);
					param3 = command.Parameters.AddWithValue("?score", score);
					param4 = command.Parameters.AddWithValue("?lives", lives);
					param5 = command.Parameters.AddWithValue("?time", MainMenu2Controller.myTime);
					param6 = command.Parameters.AddWithValue("?fruitX", lemonX);
					param7 = command.Parameters.AddWithValue("?fruitY", lemonY);
					command.ExecuteNonQuery();
				}
				
				bombArray = GameObject.FindGameObjectsWithTag("Bomb");
				for(var bomb:GameObject in bombArray)
				{
					var bombX:double = bomb.transform.position.x;
					var bombY:double = bomb.transform.position.y;
					Debug.Log("Bomb - X Coordinates: " + bombX + ", Y Coordinates:" + bombY);
					sql = "INSERT INTO state(username, game_type, score, live, time, object_xpos, object_ypos, object_type) VALUES(?username, ?gameType, ?score, ?lives, ?time, ?fruitX, ?fruitY, 4)";
					command = MySqlClient.MySqlCommand(sql, connection);

					param1 = command.Parameters.AddWithValue("?username", MainMenuController.myUsername);
					param2 = command.Parameters.AddWithValue("?gameType", MainMenu2Controller.levelSelected);
					param3 = command.Parameters.AddWithValue("?score", score);
					param4 = command.Parameters.AddWithValue("?lives", lives);
					param5 = command.Parameters.AddWithValue("?time", MainMenu2Controller.myTime);
					param6 = command.Parameters.AddWithValue("?fruitX", bombX);
					param7 = command.Parameters.AddWithValue("?fruitY", bombY);
					command.ExecuteNonQuery();
				}
				
				
			}catch(e){
				Debug.Log("Error: " + e);
			}
		}
	}
}

function OnGUI(){
	GUI.skin = fontScore;
	GUI.Label(Rect(20,20,150,20), "Score: " + myPlayer.score);
	
	if(MainMenu2Controller.levelSelected == "Classic"){
		GUI.skin = fontLives;
		GUI.Label(Rect(Screen.width-100,20,100,20), "Lives: " + myPlayer.lives);
	}
}

function incrementScore(){
	score++;
}
	
function incrementBonusScore(){
	score += 10;
}

function decreaseLive(){
	Debug.Log("Into Method");
	lives--;
	if(lives <= 0){
		Application.LoadLevel(3);
	}
}


//Database
function getData(){
	results = 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 * from state", connection);
		
		//read data from db
		mySqlDataReader = mySqlCommand.ExecuteReader();
		
		while(mySqlDataReader.Read()){
			//the data reader is an array for each row
			results.Add("Username: " + mySqlDataReader["username"].ToString() + " - Score: " + mySqlDataReader["classic_score"].ToString());
		}
		
		//close db connection
		connection.Close();
	}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);
	}
}