
var label_details_button = $L("Details");
var label_trust_button = $L("Trust certificate");
var label_dontTrust_button = $L("Don't trust certificate");

InstallAssistant = Class.create({
	
	passModel: {
		'value': ''
	},    
	
	initialize: function(target){
		Mojo.Log.info("install cert init " + Object.toJSON(target));	
		this.params = target;
		this.details = this.params;
	},
	
	setup: function() {
		Mojo.Log.info("install cert setup ");
		
		var appMenuModel = {
	        visible: true,
	        items: [Mojo.Menu.editItem, Mojo.Menu.helpItem]
	    };
	    this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: true}, appMenuModel);
	
		this.trustFileElement = this.controller.get('trustFile');               
		this.certNameElement = this.controller.get('certName');               
		this.certIssuerElement = this.controller.get('certIssuer');               
		this.certExpiresElement = this.controller.get('certExpires');               
		this.certValidityElement = this.controller.get('certValidity');               
	
		this.detailsButtonModel = {buttonLabel: label_details_button};
		this.controller.setupWidget('detailsButton', {}, this.detailsButtonModel);
		this.controller.listen('detailsButton','mojo-tap', this.detailCert.bind(this))
		
		this.trustButtonModel = {buttonLabel: label_trust_button};		
		this.controller.setupWidget('trustButton', {}, this.trustButtonModel);
		this.controller.listen('trustButton','mojo-tap', this.trustCert.bind(this))
		
		this.dontTrustButtonModel = {buttonLabel: label_dontTrust_button};		
		this.controller.setupWidget('dontTrustButton', {}, this.dontTrustButtonModel);
		this.controller.listen('dontTrustButton','mojo-tap', this.dontTrustCert.bind(this))
	
		var messageTemplate = new Template($L('The file #{fileName} is a security certificate.  Do you want to trust it?'));
		var msg = messageTemplate.evaluate({
			fileName: this.details.certificateFilename.split('/').last()
		});
	
		this.trustFileElement.innerHTML = msg;         
		this.certNameElement.innerHTML = this.details.displayCommonName = this.details.subject.commonname ? this.details.subject.commonname : this.details.subject.organization;
		if(this.certNameElement.innerHTML==="undefined"){
			var alternate_name="";
			for(var j in this.details.subject.altname){
				alternate_name += this.details.subject.altname[j] + ' ';
			}
			this.certNameElement.innerHTML=alternate_name;
		}
		Mojo.Log.info("% details " + Object.toJSON(this.details));
		this.certIssuerElement.innerHTML += this.details.displayIssuerCommonName = this.details.issuer.commonname ? this.details.issuer.commonname : this.details.issuer.organization;	
		this.certExpiresElement.innerHTML += Mojo.Format.formatDate(new Date(this.details.expiredate), {date:'long'});
	},
	
	handleCommand: function(event){
		if (event.type == Mojo.Event.command) {
			if (event.command == Mojo.Menu.helpCmd) {
				Event.stop(event);
				var openParams = {
					target: 'http://help.palm.com/device_info/index.html'
				};
				this.controller.serviceRequest('palm://com.palm.applicationManager', {
					method: 'open',
					parameters: {
						id: 'com.palm.app.help',
						params: openParams
					}
				});
			}
		} else if (event.type == Mojo.Event.commandEnable && 
		         (event.command == Mojo.Menu.helpCmd)) {
			event.stopPropagation();
		}
	},
	
	detailCert: function(event) {
		Mojo.Log.info("cert detail ");
		this.controller.stageController.pushScene('details', this.details);
	},
	
	trustCert: function(event) {
		Mojo.Log.info("cert install trust ", Object.toJSON(this.details));
		
		var args = {'certificateFilename': this.details.certificateFilename};
		
		if(this.details.passkey)
			args.passphrase = this.details.passkey;
		
		Mojo.Log.info("cert install trust ", Object.toJSON(args));
		var request = this.controller.serviceRequest('palm://com.palm.certificatemanager', {
			method: 'addcertificate',
			parameters: args,
			onSuccess: function(response){
				Mojo.Log.info("addcert response " + Object.toJSON(response));
				this.controller.stageController.popScenesTo('list');
			}.bind(this),
			onFailure: function(response){
				Mojo.Log.info("addcert response " + Object.toJSON(response));
				this.controller.stageController.popScenesTo('list');
			}.bind(this)
		});    
	},
	
	dontTrustCert: function(event) {
		Mojo.Log.info("cert dont trust ");
		this.controller.stageController.popScenesTo('list');
	},
	
	activate: function(event) {
		Mojo.Log.info("cert install activate ");
	},
	
	deactivate: function(event) {
		Mojo.Log.info("cert install deactivate ");
	},
	
	cleanup: function(event) {
		Mojo.Log.info("cert install cleanup ");
	},

});
