
DetailsAssistant = Class.create({

	certificate: {},
	
	initialize: function(item){
		Mojo.Log.info("cert details init " + Object.toJSON(item));
		this.certificate = item;
	},
	
	setup: function(item){
		Mojo.Log.info("cert details setup ");

        var appMenuModel = {
            visible: true,
            items: [Mojo.Menu.editItem, Mojo.Menu.helpItem]
        };
        this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: true}, appMenuModel);

		this.subjectModels = [];
                //this.subjectaltnameModels = [];
		this.issuerModels = [];
		this.certModels = [];
                this.subjectaltnameModels = {listTitle:$L("Subject Alt Name"),items:[]}; 
		this.certNameElement = this.controller.get('certName');

		Mojo.Log.info("Writing Cert Name", this.certificate.displayCommonName);
		this.certNameElement.innerHTML = this.certificate.displayCommonName;

		if(this.certNameElement.innerHTML==="undefined"){
                        var alternate_name="";
                        for(var j in this.certificate.subject.altname){
                                alternate_name += this.certificate.subject.altname[j] + ' ';
                        }
                        this.certNameElement.innerHTML=alternate_name;
                }

		
		Mojo.Log.info("Processing Cert List");
		for(var i in this.certificate) {
			Mojo.Log.info("certificate (top level)", i, this.certificate[i]);
			
			if (i == 'subject') {  dns_count=0;
				for (var j in this.certificate.subject) {
					Mojo.Log.info("certificate (subject) ", j, Object.toJSON(this.certificate.subject[j]));

                                        if(j=='altname'){
                                              for (var t in this.certificate.subject.altname){
                                               // this.subjectaltnameModels[t] = {
                                                //  disabled: true,
                                                 // value: this.certificate.subject[j][t]
                                                // };
                                                Mojo.Log.info("var t ",t, this.certificate.subject[j][t]);
                                                Mojo.Log.info("dns_count is:",dns_count);
                                                this.subjectaltnameModels.items[dns_count]= {value: this.certificate.subject[j][t]}; 
                                                Mojo.Log.info("this.subjectaltnameModels.items[dns_count].value is",this.subjectaltnameModels.items[dns_count].value);
                                                dns_count++;
                                               }
                                                //Mojo.Log.info("cert details init " + Object.toJSON(subjectaltnameModels)); 
                                                //this.controller.setupWidget('subjectaltname', {}, this.subjectaltnameModels[t]);
                                                this.controller.setupWidget('subjectaltname', 
                                                                            this.detailsAttr={
                                                                                               itemTemplate: "details/detailsRowTemplate",
                                                                                               listTemplate: "details/detailsListTemplate",
                                                                                             }, this.subjectaltnameModels 
                                                                                               );


                                        }
                                        else{
					   this.subjectModels[j] = {
						disabled: true,
						value: this.certificate.subject[j]
					   };
					   if(this.display_widget=this.controller.get('subject'+ j + 'view')){
						if(this.subjectModels[j].value){
							this.controller.get('subject'+ j + 'view').show();
						}
					   }
					   this.controller.setupWidget('subject' + j, {}, this.subjectModels[j]);
                                        }
				}
			} else if (i == 'issuer') {
				for (var k in this.certificate.issuer) {
					Mojo.Log.info("certificate (issuer) ", k, this.certificate.issuer[k]);
					this.issuerModels[k] = {
						disabled: true,
						value: this.certificate.issuer[k]
					};
					if(this.display_widget=this.controller.get('issuer'+ k + 'view')){
						if(this.issuerModels[k].value){
							this.controller.get('issuer'+ k + 'view').show();
						}
					   }

					this.controller.setupWidget('issuer' + k, {}, this.issuerModels[k]);
				}
			} else if (i == 'signature' || i == 'publicKey') {
				Mojo.Log.info("certificate (signature or publicKey)", i, this.certificate[i]);
				this.certModels[i] = {
					disabled: true,
					value: this.certificate[i].hexValue
				};
				this.controller.setupWidget(i, {}, this.certModels[i]);
				////------------  --------------////
				this.certModels[i+'Algorithm'] = {
					disabled: true,
					value: this.certificate[i].algorithm
				};
				this.controller.setupWidget(i+'Algorithm', {}, this.certModels[i+'Algorithm']);
			} else if (i == 'expiredate' || i == 'startdate') {
					Mojo.Log.info("certificate (expiredate or startdate)", i, this.certificate[i]);
					this.certModels[i] = {
					disabled: true,
					value: Mojo.Format.formatDate(new Date(this.certificate[i]), {date:'long', time:'long'})
				};
				this.controller.setupWidget(i, {}, this.certModels[i]);
			}
			else {
				Mojo.Log.info("certificate (misc)", i, this.certificate[i]);
				this.certModels[i] = {
					disabled: true,
					value: this.certificate[i]
				};
				if(i==='displayIssuerCommonName'){
					if(this.certModels[i].value)
					  this.controller.get('issued_by').show();
				
				}
				this.controller.setupWidget(i, {}, this.certModels[i]);
			}
		}
		
		
//		this.certNameElement.innerHTML = this.certificate.subject.commonname ? this.certificate.subject.commonname : this.certificate.subject.organization;
//		
//		var issuerName = this.certificate.issuer.commonname ? this.certificate.issuer.commonname : this.certificate.issuer.organization;
//		var issuerModel = {
//			disabled: true,
//			value: issuerName
//		};
//		this.controller.setupWidget('issuerName', {}, issuerModel);
		
		Mojo.Log.info("Setting up delete button");
		
		this.deleteButton = this.controller.get('deleteButton');
		if(this.certificate.certificateId == undefined) {
			this.deleteButton.style.display = 'none';
		}
		this.deleteButton.observe(Mojo.Event.tap, this.deleteCert.bindAsEventListener(this));

		Mojo.Log.info("Setting up done button");
		this.doneButton = this.controller.get('doneButton');
		this.doneButton.observe(Mojo.Event.tap, this.doneCert.bindAsEventListener(this));
		
		Mojo.Log.info("setup finished");
		
	},

	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();
		}
		
		
	},
	
	doneCert: function(event){
		this.controller.stageController.popScene('details');
	},
		
	deleteCert: function(event){
		Mojo.Log.info("cert delete ");
		var request = this.controller.serviceRequest('palm://com.palm.certificatemanager', {
			method: 'deletecertificate',
			parameters: {
				'certificateId': this.certificate.certificateId
			},
			onSuccess: function(response){
				Mojo.Log.info("delete cert response " + Object.toJSON(response));
			}.bind(this),
			onFailure: function(response){
				Mojo.Log.info("delete cert response " + Object.toJSON(response));
			}.bind(this)
		});
		
		this.controller.stageController.popScenesTo('list');
		
	},
	
	activate: function(event){
		Mojo.Log.info("cert details activate ");
	},
	
	deactivate: function(event){
		Mojo.Log.info("cert details deactivate ");
	},
	
	cleanup: function(event){
		Mojo.Log.info("cert details cleanup ");
	}
	
});
