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

var menuSkin:GUISkin;
var menuSkin2:GUISkin;

var connectionString;
var mySqlCommand:MySql.Data.MySqlClient.MySqlCommand;
var connection:MySql.Data.MySqlClient.MySqlConnection;
var mySqlDataReader:MySql.Data.MySqlClient.MySqlDataReader;
var id:long;

var resultsClassic = new Array();
var resultsArcade = new Array();
var resultsNinja = new Array();
var resultsSlice = new Array();
var count:int=0;

function Start () {
	getDataClassic();
	getDataArcade();
	getDataNinja();
	getDataSlice();
}

function Update () {

}

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,200,30), "High Scores");
	
	count=1;
	if(resultsClassic.length != 0){
		GUI.Label(Rect(x-40, 100+(count*15),380, 30), "Classic Mode");
		for(var rC in resultsClassic){
			count++;
			GUI.Label(Rect(x-40, 100+(count*15),380, 30), rC.ToString());
		}
	}
	if(resultsArcade.length != 0){
		count+=2;
		GUI.Label(Rect(x-40, 100+(count*15),380, 30), "Arcade Mode");
		for(var rA in resultsArcade){
			count++;
			GUI.Label(Rect(x-40, 100+(count*15),380, 30), rA.ToString());
		}
	}
	if(resultsNinja.length != 0){
		count+=2;
		GUI.Label(Rect(x-40, 100+(count*15),380, 30), "Ninja Mode");
		for(var rN in resultsNinja){
			count++;
			GUI.Label(Rect(x-40, 100+(count*15),380, 30), rN.ToString());
		}
	}
	if(resultsSlice.length != 0){
		count+=2;
		GUI.Label(Rect(x-40, 100+(count*15),380, 30), "Slice Mode");
		for(var rS in resultsSlice){
			count++;
			GUI.Label(Rect(x-40, 100+(count*15),380, 30), rS.ToString());
		}
	}
	
	if(GUI.Button(Rect(right,bottom,300,50), "Back")){
		Application.LoadLevel(1);
	}
}

function getDataClassic(){
	resultsClassic = 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 WHERE NOT (classic_score = 0) ORDER BY classic_score DESC LIMIT 5", connection);
		
		//read data from db
		mySqlDataReader = mySqlCommand.ExecuteReader();
		
		while(mySqlDataReader.Read()){
			//the data reader is an array for each row
			resultsClassic.Add("Username: " + mySqlDataReader["username"].ToString() + " - Score: " + mySqlDataReader["classic_score"].ToString());
		}
		
		//close db connection
		connection.Close();
	}catch(e){
		Debug.Log("Error: " + e);
	}
}

function getDataArcade(){
	resultsArcade = 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 WHERE NOT (arcade_score = 0) ORDER BY arcade_score DESC LIMIT 5", connection);
		
		//read data from db
		mySqlDataReader = mySqlCommand.ExecuteReader();
		
		while(mySqlDataReader.Read()){
			//the data reader is an array for each row
			resultsArcade.Add("Username: " + mySqlDataReader["username"].ToString() + " - Score: " + mySqlDataReader["arcade_score"].ToString());
		}
		
		//close db connection
		connection.Close();
	}catch(e){
		Debug.Log("Error: " + e);
	}
}

function getDataNinja(){
	resultsNinja = 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 WHERE NOT (ninja_score = 0) ORDER BY ninja_score DESC LIMIT 5", connection);
		
		//read data from db
		mySqlDataReader = mySqlCommand.ExecuteReader();
		
		while(mySqlDataReader.Read()){
			//the data reader is an array for each row
			resultsNinja.Add("Username: " + mySqlDataReader["username"].ToString() + " - Score: " + mySqlDataReader["ninja_score"].ToString());
		}
		
		//close db connection
		connection.Close();
	}catch(e){
		Debug.Log("Error: " + e);
	}
}

function getDataSlice(){
	resultsSlice = 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 WHERE NOT (slice_score = 0) ORDER BY slice_score DESC LIMIT 5", connection);
		
		//read data from db
		mySqlDataReader = mySqlCommand.ExecuteReader();
		
		while(mySqlDataReader.Read()){
			//the data reader is an array for each row
			resultsSlice.Add("Username: " + mySqlDataReader["username"].ToString() + " - Score: " + mySqlDataReader["slice_score"].ToString());
		}
		
		//close db connection
		connection.Close();
	}catch(e){
		Debug.Log("Error: " + e);
	}
}
