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

var PoundEvdoAssistant = Class.create({
	
	evdoModes: [
					{label: '1xRTT only', value: 'only1x'}, 
					{label: 'EVDO Rev 0 only', value: 'onlyevdo'}, 
					{label: 'Hybrid Rev 0', value: 'hybrid'}, 
					{label: 'EVDO only', value: 'evdoonlyreva'},
					{label: 'Hybrid Rev A',value: 'hybridreva'}
	],
	
	//Model for Evdo Mode Selection List
	EvdoModel: {currentEvdoMode: 'only1x'},
	
	initialize: function() {
		QDLogger.log("Pound Evdo Assistant");
		TelephonyCommands.evdoQuery(this.onEvdoQueryDone.bind(this));
		this.appControl = Mojo.Controller.getAppController();
		this.appAssistant = this.appControl.assistant;
		
	},
	
	onEvdoQueryDone: function(success, status, mode) {
		if (success) {
			this.EvdoModel.currentEvdoMode = mode;
			this.saveSelection = mode;			
		} else {
			this.EvdoModel.currentEvdoMode = '';
			this.controller.showAlertDialog({
		    onChoose: function(value){},
		    message: status,
		    preventCancel:true,
		    choices:[
	        {label: $L('Done'), value:true, type:'dismiss'}    
		    ]
		  	});	  
			}
		this.controller.modelChanged(this.EvdoModel,this);
	},
	
	setup: function(){
		this.controller.document.body.addClassName("no-wallpaper");
							
		this.controller.setupWidget('evdoSelector', {
			choices: this.evdoModes,
			modelProperty: 'currentEvdoMode'
		}, this.EvdoModel);
		
		this.controller.get('evdoSelector').observe('mojo-property-change', this.selectorChanged.bind(this));
						
		this.controller.get('saveButton').observe(Mojo.Event.tap,this.saveActivated.bind(this));
		this.controller.get('doneButton').observe(Mojo.Event.tap,this.doneActivated.bind(this));
	},
	
	cleanup: function() {
		this.controller.document.body.removeClassName("no-wallpaper");
	},
	
	selectorChanged: function(event) {
		this.saveSelection = event.value;
		QDLogger.log("PoundEvdoAssistant::selectorChanged", "Selected Value ", this.saveSelection);
	},
	
	
	saveActivated: function() {
		TelephonyCommands.evdoSet(this.saveSelection, this.onSaveDone.bind(this));				
	},
	
	onSaveDone: function(success, status) {
		
		if (success) {
			if (this.appAssistant.rebootOnRadioSettingsChanged) {
				this.promptToReboot();
			} else {
				this.showErrorDialog("Resetting radio to save changes. Please wait...");
				TelephonyCommands.resetRadio(this.radioResetDone.bind(this));	
			}
						
		} else {
			this.showErrorDialog(status || "Error occurred.");
		}
		
	},
	
	radioResetDone: function(success, status) {
		if (success) {
			this.controller.stageController.popScene();
		} else {
			this.showErrorDialog(status || "Error occurred.");
		}
		
	},
	
	doneActivated: function() {
		this.controller.stageController.popScene();		
	},
	
	showErrorDialog: function(msg){
		this.controller.showAlertDialog({
			onChoose: function(value){				
			},
			message: msg,
			choices: [{
				label: $L('Close'),
				value: false,
				type: 'dismiss'
			}]
		});
	},
	
	promptToReboot: function() {
		this.controller.showAlertDialog({
			onChoose: function(value){
				TelephonyCommands.rebootPhone();				
			},
			message: "Phone must now reboot.",
			choices: [{
				label: 'Reboot',
				value: false,
				type: 'dismiss'
			}]
		});
	}
	
});
