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

var PoundDebugAssistant = Class.create({
	initialize: function() {
		this.update = false;
		this.radioDebugReq = TelephonyCommands.radioDebugSet(true,true,this.debugQueryDone.bind(this));
		PowerdCommands.getBatteryPowerLevel(this.handleBatteryLevel.bind(this));
	},
	
	fieldMap: $H({
					percent:		"Battery Voltage", 
					// 1X
					rfmode:			"RF Mode",
					cpstate:		"Call Processing State",
					"channel(1x)":	"Channel # (1x)",
					sid: 			"SID", 
					nid: 			"NID",
					so:				"Service Option #",
					rssivalue: 		"RSSI Value", 
					ecio: 			"Rx Ec/Io", 
					activepn:		"Active PN",
					rx: 			"Rx Power (in dBM)", // is this the 1x one?
					tx:				"Tx Power",
					badframes:		"Bad Frames",
					totalframes:	"Total Frames",
					fer: 			"FER %", 
					fingerpns:		"Finger PNs",
					neighborpn: 	"Neighbor Set PN(s)",
					previnuse:		"PREV In Use",
					stationclassmark:"Station Class Mark",
					vocoderpreference: "Vocoder Preference",
					slottedmodeuse:	"Slotted Mode Use",
					slotcycleindex: "Slot Cycle Index",
					mcc:			"MCC",
					mnc:			"MNC",
					accolc:			"ACCOLC", 
					homesysregistration: "Home System Registration", 
					frc:			"FRC", 
					rrc:			"RRC", 
					txadj:			"Tx, Adjust", 
					lat:			"Latitude",
					"long":			"Longitude",  // in quotes because long could be a keyword
					meid:			"MEID",
					// EVDO
					band: 			"BAND",
					anaaaauthstate:	"AN/AAA Authentication (EvDO)",
					currentipaddress:"Data Session Curr. IP Addr. (EvDO)",
					activesetpilotpn: "Active Set Pilot PN(s) (EvDO)",
					channelnumber: 	"Channel # (EvDO)",
					receivesignalstrength: "Receive Signal Strength (EvDO)", // is this the evdo one?
					receivepower:	"Receive Power (EvDO)",
					channelper: 	"Channel PER (EvDO)",
					datasessionstate:"Data State",
					evdouati:		"EVDO UATI", 
					colorcode:		"Subnet Color Code (EvDO)", 
					reqdatarate:	"Requested Data Rate (EvDO)",
					evdorri:		"EVDO RRI", 
					hdrsessionstate:"HDR Session State (EvDO)",
					ipprotoinuse:	"IP Proto In Use (EvDO)", 
					subnetid:		"Subnet (EvDO)",
					
					
				
					
				}),
	debugList: [],
		
	setup: function() {
		this.controller.document.body.addClassName("no-wallpaper");
			
		this.debugListModel = {
            items: this.debugList			
        };
		       
        this.template = {
			itemTemplate: 'poundDebug/listitem',            		          
        };	
		
        this.controller.setupWidget('debuglist', this.template, this.debugListModel);	
		this.debuglistWidget = this.controller.get('debuglist');		
			
	},
	
	cleanup: function() {
		TelephonyCommands.radioDebugSet(false,false,null);
		this.controller.document.body.removeClassName("no-wallpaper");
		this.debugList.clear();		
		PowerdCommands.powerSubscriptionCancel();
		this.radioDebugReq.cancel();		
	},
	
	debugQueryDone: function(success, status, debugInfo) {
					
		if(!debugInfo)
			return;
				
		if(this.update) {
			this.handleUpdates(debugInfo);
			return;
		}			
			
		if (success) {
			var fields = $H(debugInfo);
			
			this.fieldMap.each(function(pair) {
				var obj = new Object();
				
				if(pair.key == "percent")
					return;
				
				if(fields.get(pair.key) == undefined)
					return;
					
				obj.FIELDNAME = pair.value;
				obj.FIELDVALUE = fields.get(pair.key).gsub(',',', ');
				obj.TILKEY = pair.key;
				obj.TILVALUE = fields.get(pair.key);
				if(obj.FIELDVALUE.length == 1)
					obj.FIELDVALUE = obj.FIELDNAME + "  "+ obj.FIELDVALUE;				
				this.debugList.push(obj);
			}.bind(this));					
			this.update = true;			
		}		
		 else {
			this.controller.showAlertDialog({
		    onChoose: function(value){},
		    message: status,
		    preventCancel:false,
		    choices:[
	        {label: 'Done', value:true, type:'dismiss'}    
		    ]
		  	});	  
			}			
		this.controller.modelChanged(this.debugListModel, this);
	},
	
	handleUpdates: function(debugInfo) {
					
		var fields = $H(debugInfo);
		
		for(var i=0; i<this.debugList.length; i++) {
			
			if(this.debugList[i].FIELDNAME == "Battery Voltage")
				continue;
			
			var val = fields.get(this.debugList[i].TILKEY);
			if(this.debugList[i].TILVALUE === val)
				continue;
										
			this.debugList[i].FIELDVALUE = val.gsub(',',', ');
			this.debugList[i].TILVALUE = val;
			if(this.debugList[i].FIELDVALUE.length == 1)
				this.debugList[i].FIELDVALUE = this.debugList[i].FIELDNAME + "  "+ this.debugList[i].FIELDVALUE;
			this.debuglistWidget.mojo.invalidateItems(i,1);
							
		}			
			
	},
	
	handleBatteryLevel: function(payload) {
		
		if(!payload.voltage_mV)
			return;
		
		var value = this.fieldMap.get("percent");
		
		if(this.batUpdate) {
			for(var i=0; i<this.debugList.length; i++) {
			if(this.debugList[i].FIELDNAME == value) {
				this.debugList[i].FIELDVALUE = payload.voltage_mV + " mV";
				this.debuglistWidget.mojo.invalidateItems(i,1);
				break;
			}					
			}
		}
		else {
			var batObj = new Object();
			batObj.FIELDNAME = value;
			batObj.FIELDVALUE = payload.voltage_mV + " mV";
			this.debugList.push(batObj);
			this.batUpdate = true;
			this.controller.modelChanged(this.debugListModel);
		}		
			
	}
	
});
