import DBase;
import Level;

public var header:GUISkin;	//Used for total title
public var content:GUISkin;	//Used for total title
private var error:String;

private var password:String; // the code
function Start () {
	password = "Enter Password...";
	error = "";
}

function Update () {
}

function OnGUI(){
	GUI.skin = header;	//Setting up the GUI Skin.
	//Title
	GUI.Label(Rect(100,70,950,100),"Load Game");
	GUI.skin = content;
	
	password = GUI.TextField(Rect(100,160,280,40),password,75);
    GUI.Label(Rect(320,160,280,40),""+error);
    //Clicking this button the game will check whehter the code is linked
    //with a particular save and if it is it will load the game
    //else it will inform the user about his mistake
    if(GUI.Button(Rect(100,210,270,30),"Load")){
		var db = new DBase();
		
		try{
			var mySave: Save;
			mySave = db.getSaveByPassword(int.Parse(password.ToString()));
			if(mySave != null){
				//First level attributes are set
				PlayerPrefs.SetInt("Level",mySave.getLevel());
				PlayerPrefs.SetInt("PlayerScore",mySave.getTScore());
				PlayerPrefs.SetInt("PlayerLife",mySave.getLife());
				PlayerPrefs.SetInt("PlayerGameScore",mySave.getScore());
				PlayerPrefs.SetInt("GameTime",mySave.getTime());
				Application.LoadLevel(1);	//Game is loaded
			}else{
				error = "Invalid Code";
			}
		}catch(e){
			error = "Invalid Format";
		}
    }
    
    //Clicking this button will redirect the user to the main menu.
    if(GUI.Button(Rect(100,260,270,30),"Back")){
    	Application.LoadLevel(0);
    }
}