#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 usernameResults = new Array();
var gameTypeResults = new Array();
var count:int=0;
var gt:String;
static var isLoad:boolean;


var menuSkin: GUISkin;
var menuSkin2: GUISkin;
static var levelSelected:String;
static var myTime:int;

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;
	
	if(GUI.Button(Rect(x,105,300,50), "Classic Mode")){
		Application.LoadLevel(2);
	}else if(GUI.Button(Rect(x,160,300,50), "Arcade Mode")){
		Application.LoadLevel(4);
	}else if(GUI.Button(Rect(x,215,300,50), "Ninja Mode")){
		Application.LoadLevel(5);
	}else if(GUI.Button(Rect(x,270,300,50), "Slice Mode")){
		Application.LoadLevel(6);
	}else if(GUI.Button(Rect(right,385,300,50), "High Scores")){
		Application.LoadLevel(7);
	}
	else if(GUI.Button(Rect(right,440,300,50), "Your Scores")){
		Application.LoadLevel(8);
	}
	else if(GUI.Button(Rect(right,bottom,300,50), "Back")){
		Application.LoadLevel(0);
	}
	
	
	if(gt == "Classic"){
		if(GUI.Button(Rect(right,330,300,50), "Load")){
			isLoad = true;
			Application.LoadLevel(2);
		}
	}else if(gt == "Arcade"){
		if(GUI.Button(Rect(right,330,300,50), "Load")){
			isLoad = true;
			Application.LoadLevel(4);
		}
	}else if(gt == "Ninja"){
		if(GUI.Button(Rect(right,330,300,50), "Load")){
			isLoad = true;
			Application.LoadLevel(5);
		}
	}else if(gt == "Slice"){
		if(GUI.Button(Rect(right,330,300,50), "Load")){
			isLoad = true;
			Application.LoadLevel(6);
		}
	}
}

function Start () {
	isLoad = false;
	MouseController.score = 0;
	MouseController.lives = 3;
	levelSelected = "none";
	getData();
	for(var uR in usernameResults){
		Debug.Log("My String: " + uR.ToString());
		if((MainMenuController.myUsername + " - " + "Classic").ToString() == uR.ToString()){
			gt = "Classic";
			break;
		}else if((MainMenuController.myUsername + " - " + "Arcade").ToString() == uR.ToString()){
			gt = "Arcade";
			break;
		}else if((MainMenuController.myUsername + " - " + "Ninja").ToString() == uR.ToString()){
			gt = "Ninja";
			break;
		}else if((MainMenuController.myUsername + " - " + "Slice").ToString() == uR.ToString()){
			gt = "Slice";
			break;
		}
		
		/*
		if(MainMenuController.myUsername == uR.ToString()){
			for(var gtR in gameTypeResults){
				if(gtR == "Classic"){
					gt = "Classic";
					break;
				}else if(gtR == "Arcade"){
					gt = "Arcade";
					break;
				}else if(gtR == "Ninja"){
					gt = "Ninja";
					break;
				}else if(gtR == "Slice"){
					gt = "Slice";
					break;
				}
			}
		}*/
	}
}

function Update () {

}

function getData(){
	usernameResults = new Array();
	gameTypeResults = 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
			usernameResults.Add(mySqlDataReader["username"].ToString() + " - " + mySqlDataReader["game_type"].ToString());
			//gameTypeResults.Add(mySqlDataReader["game_type"].ToString());
		}
		
		//close db connection
		connection.Close();
	}catch(e){
		Debug.Log("Error: " + e);
	}
}