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

var PoundRtnAssistant = Class.create({
	
	initialize: function() {
		TelephonyCommands.rtnQuery(this.rtnQueryDone.bind(this));
		TelephonyCommands.versionOsQuery(this.addValue.bind(this, "Version"));
		TelephonyCommands.subscribePreference("customizationPackageVersion", this.addValue.bind(this, "Configuration"));
		TelephonyCommands.hardwareRevisionQuery(this.addValue.bind(this, "Hardware Revision"));
		this.showErrorDialog = this.showErrorDialog.bind(this);
		this.msl = null;
	},
	
	rtnList: [],
	
	setup: function() {
		
		this.controller.document.body.addClassName("no-wallpaper");
			
		this.rtnListModel = {
            items: this.rtnList			
        };
		       
        this.template = {
			itemTemplate: 'poundRtn/listitem',            		          
        };		

        this.controller.setupWidget('rtnlist', this.template, this.rtnListModel);
		this.controller.get('resetButton').observe('mojo-tap', this.handleReset.bindAsEventListener(this));		
	},
	
	cleanup: function() {
		if(this.msl != null)
			TelephonyCommands.exitProgramMode(null);
		this.controller.document.body.removeClassName("no-wallpaper");
		this.rtnList.clear();		
	},
	
	rtnQueryDone: function(success, status, rtnInfo) {
		
		if (success) {
			var fields = $H(rtnInfo);
			fields.each(function(pair) {
				var obj = new Object();
				obj.FIELDNAME = pair.key.toUpperCase();
				obj.FIELDVALUE = pair.value;
				this.rtnList.push(obj);
			}.bind(this));			
		}		
		 else {
			this.controller.showAlertDialog({
		    onChoose: function(value){},
		    message: status,
		    preventCancel:false,
		    choices:[
	        {label: 'Done', value:true, type:'dismiss'}    
		    ]
		  	});	  
			}			
		this.controller.modelChanged(this.rtnListModel, this);
	},
	
	addValue: function(key, value) {
		QDLogger.log( "PoundRtnAssistant::addValue", key  , value);
		this.rtnList.push( {"FIELDNAME": key, FIELDVALUE: value});
		this.controller.modelChanged(this.rtnListModel, this);
	},
	
	handleReset:function(event) {
		QDLogger.log("Reset button Tapped");
		Event.stop(event);
		this.showMSLDialog();
	},
	
	verifiedMSL: function(success,msl) {
		this.msl = msl;
		QDLogger.log( "PoundRtnAssistant::verifiedMSL");
		
		if (success) {
			QDLogger.log( "PoundRtnAssistant::verifiedMSL", "Programming mode success");
			this.dialogBox = this.controller.showDialog({
			template: 'poundRtn/rtn-reset-dialog',			
			assistant: new RtnResetAssistant(this, this.msl),	
			preventCancel: true		
			});
			 
		} 
	},
	
	doneProgramming: function(success, status) {
		QDLogger.log( "PoundRtnAssistant::doneProgramming");
		this.msl = null;
		this.controller.stageController.popScene('rtn');
	},
	
	// after reset, wipe the phone.  this will cause a reboot.
	// on failure, pop error
	doneReset: function(success, status) {
		QDLogger.log( "PoundRtnAssistant::doneReset");
		if (success) {
			TelephonyCommands.exitProgramMode(this.doWipe.bind(this));
		} else {
			if (this.dialogBox && this.dialogBox.mojo) {
				this.dialogBox.mojo.close();
			} 
			
			// exit program mode, too
			if (this.msl != null) {
				TelephonyCommands.exitProgramMode(null);
			}
		
			this.showErrorDialog.delay(2);
		}
	},
	
	showErrorDialog: function() {
		this.controller.showAlertDialog({
			onChoose: function(value){},
		    message: "Failed to reset radio settings.",
		    preventCancel:false,
		   	choices:[
	        	{label: 'OK', value:true, type:'dismiss'}    
		 	]
		 });
	},
	
	doWipe: function() {
		QDLogger.log( "PoundRtnAssistant::doWipe", "*double-click nuke cloud* *5* *4* *3* *2* *1* *oh-no!* *pop*");
		TelephonyCommands.wipeDevice(); 
	},
	
	saveMSL: function(mslValue) {
		this.msl = mslValue;
	},
	
	showMSLDialog: function() {				
		this.dialogBox = this.controller.showDialog({
			template: 'template/phone-msl-dialog',
			assistant: new MslAssistant(this),	
			preventCancel: true		
		});
	},
	
	
});


var RtnResetAssistant = Class.create({
	
	initialize: function(sceneAssistant,mslVal) {
		QDLogger.log( "RtnResetAssistant::initialize");
		this.sceneAssistant = sceneAssistant;
		this.mslVal = mslVal;		
	},
	
	setup : function(parent) {
		this.parent = parent;			
		this.sceneAssistant.controller.get('reset').addEventListener(Mojo.Event.tap, this.handleReset.bindAsEventListener(this));
		this.sceneAssistant.controller.get('cancel').addEventListener(Mojo.Event.tap, this.handleCancel.bindAsEventListener(this));
	},
	
	
	handleCancel: function() {
		TelephonyCommands.exitProgramMode(this.sceneAssistant.doneProgramming.bind(this.sceneAssistant));
		this.parent.mojo.close();		
	},
	
	handleReset: function() {
		QDLogger.log( "RtnResetAssistant::handleReset");
		TelephonyCommands.radioDefaultsRestore('rtn',this.mslVal,this.sceneAssistant.doneReset.bind(this.sceneAssistant));
		// hide all buttons and plead for patience.
		this.sceneAssistant.controller.get('reset').hide();
		this.sceneAssistant.controller.get('cancel').hide();
		this.sceneAssistant.controller.get('message').innerText = "Resetting: please wait a few minutes. DO NOT CLOSE APP OR REMOVE BATTERY. Your phone will reboot automatically.  Thanks for being patient.";
	},		
	
	
});
