/* Copyright 2009 Palm, Inc.  All rights reserved. */

var PoundMslAssistant = Class.create({
	
	initialize: function() {
		TelephonyCommands.getActivationInfo(this.displayActivationInfo.bind(this));
	},
	
	setup: function() {
		this.controller.document.body.addClassName("no-wallpaper");
		this.textAttributes = {
			enterSubmits: true,
			focus: true,
			multiline: false,       
			modifierState: Mojo.Widget.numLock,
			focusMode: Mojo.Widget.focusSelectMode,
			maxLength: 40,
			modelProperty: "value",
			disabledProperty: 'DISABLED'			
		};
		
		this.cmdMenuModel = { visible: true, items: [{},{},{},{label: $L('Edit'), command:'edit'}]};
					
		this.controller.setupWidget(Mojo.Menu.commandMenu, undefined, this.cmdMenuModel);
		
		this.mdnModel = {
			'value':'',
			'DISABLED': true
		},
		this.msidModel = {
			'value':'',
			'DISABLED': true
		},
		this.controller.setupWidget('msid', this.textAttributes, this.msidModel);
		this.controller.setupWidget('mdn', this.textAttributes, this.mdnModel);	
		
		this.mdnWidget = this.controller.get('mdn');
		this.msidWidget = this.controller.get('msid');			
		
		Mojo.Event.listen(this.controller.get('saveButton'),'mojo-tap', this.handleSave.bindAsEventListener(this));
		this.controller.get('saveButton').hide();
	},
	
	cleanup: function() {
		this.controller.document.body.removeClassName("no-wallpaper");
		TelephonyCommands.exitProgramMode(null);
	},
	
	handleCommand: function(event) {
		if (event.type == Mojo.Event.command) {		
			if (event.command == 'edit') {
				this.changeView('edit');
				var view = [{},{},{},{label: $L('View'), command:'view'}];
				this.cmdMenuModel.items = view;
				this.controller.modelChanged(this.cmdMenuModel);				
			}
			else if (event.command == 'view') {
				this.changeView('view');
				var edit = [{},{},{},{label: $L('Edit'), command:'edit'}];
				this.cmdMenuModel.items = edit;
				this.controller.modelChanged(this.cmdMenuModel);
				
			}
		}
	},
	
	changeView: function(viewName) {
		
		if(viewName === 'edit') {
			this.msidModel.value = '0000000000';
			this.msidModel.DISABLED = false;
			this.controller.modelChanged(this.msidModel,this);
			
			this.mdnModel.DISABLED = false;
			this.controller.modelChanged(this.mdnModel,this);
			
			this.controller.get('saveButton').show();
			this.mdnWidget.mojo.focus();			
		}
		else {
			this.msidModel.value = this.msid;
			this.msidModel.DISABLED = true;
			this.controller.modelChanged(this.msidModel,this);
			
			this.mdnModel.value = this.mdn;
			this.mdnModel.DISABLED = true;
			this.controller.modelChanged(this.mdnModel,this);
			
			this.controller.get('saveButton').hide();
			this.mdnWidget.mojo.blur();
			this.msidWidget.mojo.blur();
		}
	},
	
	// callback setting activation info
	displayActivationInfo: function(success, status, mdn, msid) {
		QDLogger.log("PoundMSL Display ", success, mdn, msid);
		if (success) {
			this.mdn = mdn;
			this.mdnModel.value = mdn;			
			this.controller.modelChanged(this.mdnModel,this);
			this.msid = msid;
			this.msidModel.value = msid;
			this.controller.modelChanged(this.msidModel,this);			
		} else {
			this.controller.showAlertDialog({
		    onChoose: function(value){},
		    message: status,
		    preventCancel:false,
		    choices:[
	        {label: $L('Done'), value:true, type:'dismiss'}    
		    ]
		  	});	  
		}
	},
	
	handleSave: function() {
		
		this.controller.get('validationerror').hide();
		
		if(!this.controller.get('msid').mojo.getValue() || !this.controller.get('mdn').mojo.getValue())
			return;
		
		if(this.controller.get('msid').mojo.getValue().length < 10 || this.controller.get('mdn').mojo.getValue().length < 10) {
			this.controller.get('validationerror').show();
			return;
		}
		this.msid = this.controller.get('msid').mojo.getValue();
		this.mdn = this.controller.get('mdn').mojo.getValue();
				
		TelephonyCommands.setActivation(this.controller.get('mdn').mojo.getValue(), this.controller.get('msid').mojo.getValue(), 
			this.handleProgrammingSaved.bind(this));
	},
	
	handleProgrammingSaved: function(success, status) {
		if (!success) {
			this.controller.widget.showAlertDialog({
		    onChoose: function(value){},
		    message: status,
		    preventCancel:false,
		    choices:[
	        {label: $L('Done'), value:true, type:'dismiss'}    
		    ]
		  	});	  
		}
		else 
			this.controller.stageController.popScene(); 
	},
	
	
});
