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

var menuSkin:GUISkin;
var menuSkin2:GUISkin;
var bomb:AudioClip;
var gong2:AudioClip;

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;

function OnGUI(){
	GUI.skin = menuSkin;
	var x = Screen.width/2-150;
	var right = Screen.width-320;
	var bottom = Screen.height-70;
	
	GUI.Label(Rect(x,20,300,50), "Fruit Slice");
	GUI.skin = menuSkin2;
	GUI.Label(Rect(x,70,120,30), "Game Over");
	GUI.Label(Rect(x,105,300,30), "Your Score: " + MouseController.score);
	if(GUI.Button(Rect(x,140,300,50), "Back to Game Menu")){
		Application.LoadLevel(1);
	}
	
	count=0;
	for(var r in results){
		count++;
		GUI.Label(Rect(x-40, 200+(count*15),380, 30), r.ToString());
	}
}

function Start () {
	if(MainMenu2Controller.levelSelected == "Classic"){
		if(MouseController.lives > 0){
			audio.PlayOneShot(bomb);
		}
	}else if(MainMenu2Controller.levelSelected == "Arcade" || MainMenu2Controller.levelSelected == "Ninja" || MainMenu2Controller.levelSelected == "Slice"){
		audio.PlayOneShot(gong2);
	}
	getData();
	insertInfo(MainMenuController.myUsername,MouseController.score);
}

function Update () {

}


function insertInfo(username:String, score:int){
//Debug.Log(username+score);
	try{
		connection = MySql.Data.MySqlClient.MySqlConnection(connectionString);
		connection.Open();
		var command:MySql.Data.MySqlClient.MySqlCommand;
		var sql:String;
		if(MainMenu2Controller.levelSelected == "Classic"){
			sql = "INSERT INTO players(username, classic_score, arcade_score, ninja_score, slice_score) VALUES(?username, ?score, 0, 0, 0)";
		}else if(MainMenu2Controller.levelSelected == "Arcade"){
			sql = "INSERT INTO players(username, classic_score, arcade_score, ninja_score, slice_score) VALUES(?username, 0, ?score, 0, 0)";
		}else if(MainMenu2Controller.levelSelected == "Ninja"){
			sql = "INSERT INTO players(username, classic_score, arcade_score, ninja_score, slice_score) VALUES(?username, 0, 0, ?score, 0)";
		}else if(MainMenu2Controller.levelSelected == "Slice"){
			sql = "INSERT INTO players(username, classic_score, arcade_score, ninja_score, slice_score) VALUES(?username, 0, 0, 0, ?score)";
		}
		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);
	}
}

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 players", 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);
	}
}
