var PasswordDialogAssistant = Class.create({
	
	initialize: function(sceneAssistant, params) {
		Mojo.Log.info("dialog initialize", Object.toJSON(params));
		this.sceneAssistant = sceneAssistant;
		this.controller = sceneAssistant.controller;
		this.params = params;	
	},
	
	setup : function(widget) {
		Mojo.Log.info("dialog setup");
		this.widget = widget;
		this.passwordAttr = {
			focus: true,
			hintText: $L('enter password...'),
			changeOnKeyPress: true,
			requiresEnterKey: true,
		};
		this.passwordModel = {
			value :''
		};

		this.controller.setupWidget('certificatePasswordWidget', this.passwordAttr, this.passwordModel);
		this.controller.get('certificatePasswordWidget').observe(Mojo.Event.propertyChange, this.propertyChanged.bind(this));

		this.controller.get('submitPassword').addEventListener(Mojo.Event.tap, this.doneDialog.bindAsEventListener(this));
		this.controller.get('cancelPassword').addEventListener(Mojo.Event.tap, this.closeDialog.bind(this));
		this.controller.get('passwordIncorrectView').hide();
	},
	
	activate: function() {
		Mojo.Log.info("dialog activate");
	},
	
	closeDialog: function() {
		this.widget.mojo.close();
	},
	
	propertyChanged: function(event){
		if (event && Mojo.Char.isEnterKey(event.originalEvent.keyCode)) {
			Mojo.Log.info("enter key");
			this.doneDialog(event);
		} else {
			Mojo.Log.info("no enter key");
		}
    },
	
	doneDialog: function(event) {
		event.stop();
		this.params.passphrase = this.passwordModel.value;
		Mojo.Log.info("dialog donedialog", Object.toJSON(this.params));
		this.controller.serviceRequest('palm://com.palm.certificatemanager', {
				method: 'getcertificatedetails',
				parameters: this.params,
				onSuccess: this.dialogSuccess.bind(this, this.params),
				onFailure: this.dialogFailure.bind(this, this.params),
			});
	},
	
	dialogSuccess: function(params, response){
		Mojo.Log.info("dialog success");
		response.certificateFilename = params.certificateFilename;
		response.passkey = this.passwordModel.value;
		this.controller.stageController.pushScene('install', response);		
	},
	
	dialogFailure: function(params, response){
		Mojo.Log.info("dialog failure ", Object.toJSON(response));
		this.controller.get('passwordIncorrectView').show();
	}
	
});
