ACPowerSupplyAssistant.prototype.ChargerRequest = undefined;
ACPowerSupplyAssistant.prototype.BatteryRequest = undefined;

ACPowerSupplyAssistant.prototype.isChargingFromAC = false;
ACPowerSupplyAssistant.prototype.BatteryLevel = undefined;
ACPowerSupplyAssistant.prototype.Step = 0;

function ACPowerSupplyAssistant()
{
	this.commandMenuItems = new Array();
	this.commandMenuModel = {
			visible:true,
			items: [{ }] 
    };
	this.cmdmenuItemDone = new Object();
}

ACPowerSupplyAssistant.prototype.Initialize = function()
{
	console.log("initialize");
	// this sets up the screne properly for the next time it's activated
	this.controller.get('instructions').update( $L("If the USB cable is already plugged in, unplug it.  Plug the small end of the USB cable into the phone.") );
	
	this.controller.get('image').src = "images/usbCable.png";
	//show Continue on right 
	var length = this.commandMenuItems.length;
	for (var i = 0; i < length; i++) {
		this.commandMenuItems.pop();
	}
	var cmdmenuItem1 = new Object();
    this.commandMenuItems.push(cmdmenuItem1);
	var cmdmenuItem2 = new Object();
    this.commandMenuItems.push(cmdmenuItem2);
	
    this.cmdmenuItemDone.label = $L("Continue");
    this.cmdmenuItemDone.command = "handleButton";
    this.commandMenuItems.push(this.cmdmenuItemDone);
	
	this.commandMenuModel.items = this.commandMenuItems;
	this.controller.modelChanged(this.commandMenuModel);
	this.controller.get('percentage').hide( );
}

ACPowerSupplyAssistant.prototype.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 (typeof this[event.command] == "function") {
			this[event.command](event);
		}
	} else if (event.type == Mojo.Event.commandEnable && 
		 (event.command == Mojo.Menu.helpCmd)) {
		event.stopPropagation();
	}
}

/************************************************************************
 * Do initial setup
 ************************************************************************/
ACPowerSupplyAssistant.prototype.setup = function()
{
	this.appMenuModel = {
		visible: true,
		items: [Mojo.Menu.editItem, Mojo.Menu.helpItem]
	};
	this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: true}, this.appMenuModel);
	var cmdmenuItem1 = new Object();
    this.commandMenuItems.push(cmdmenuItem1);
	var cmdmenuItem2 = new Object();
    this.commandMenuItems.push(cmdmenuItem2);
	
    this.cmdmenuItemDone.label = $L("Continue");
    this.cmdmenuItemDone.command = "handleButton";
    this.commandMenuItems.push(this.cmdmenuItemDone);
	
	this.commandMenuModel.items = this.commandMenuItems;

	
    this.controller.setupWidget(Mojo.Menu.commandMenu, {}, this.commandMenuModel);
	this.Initialize();
	//this.controller.get('Button').observe(Mojo.Event.tap, this.handleButton.bindAsEventListener(this));
}

ACPowerSupplyAssistant.prototype.handleButton = function()
{
	//var button = this.controller.get('Button');
	if (this.Step == 0)
	{
		this.Step++;
		this.controller.get('instructions').update( $L("Plug the large end of the USB cable into the AC adapter. Then plug it into an electrical outlet.") );
		this.controller.get('image').src = "images/ACPower.png";
		//show Skip on left
		var length = this.commandMenuItems.length;
		for (var i = 0; i < length; i++) {
			this.commandMenuItems.pop();
		}
		this.cmdmenuItemDone.label = $L("Skip");
   		this.cmdmenuItemDone.command = "handleButton";
    	this.commandMenuItems.push(this.cmdmenuItemDone);
		var cmdmenuItem1 = new Object();
    	this.commandMenuItems.push(cmdmenuItem1);
		var cmdmenuItem2 = new Object();
    	this.commandMenuItems.push(cmdmenuItem2);

		
		this.commandMenuModel.items = this.commandMenuItems;
		this.controller.modelChanged(this.commandMenuModel);
		//this.controller.get('Button').update( $L("Skip") );
	} 
	else if (this.Step == 1)
	{
		Logging.log( "AC Adapter", "Skipped" );
		SceneChoices.pushNextScene(this.controller.stageController);
	} 
	else if (this.Step == 2)
	{
		SceneChoices.pushNextScene(this.controller.stageController);
	}
	else
	{
		console.log("ACPowerSupply: Error, this.Step==" + this.Step );
	}
}

ACPowerSupplyAssistant.prototype.handlePluggedIn = function()
{
	if (this.Step == 1)
	{
		this.Step++;
		this.controller.get('instructions').update( $L("The charging test takes a few minutes.  Please wait.") );
		this.controller.get('image').src = "images/charge.png";
		var length = this.commandMenuItems.length;
		for(var i = 0; i < length; i++)
			this.commandMenuItems.pop();
		
		var cmdmenuItem1 = new Object();
    	this.commandMenuItems.push(cmdmenuItem1);
		var cmdmenuItem2 = new Object();
    	this.commandMenuItems.push(cmdmenuItem2);
		this.cmdmenuItemDone.label = $L("Done");
   		this.cmdmenuItemDone.command = "handleButton";
    	this.commandMenuItems.push(this.cmdmenuItemDone);
		
		this.commandMenuModel.items = this.commandMenuItems;
		this.controller.modelChanged(this.commandMenuModel);
		//this.controller.get('Button').update( $L("Done") );

		if (this.BatteryLevel != true)
		{
			this.requestBatteryLevel();
		}
		else
		{
			this.controller.get('percentage').show( );
			this.controller.get('instructions').update( $L("The charging test is complete.") );
			Logging.log( "AC Adapter", "Battery = " + this.BatteryLevel );
		}
	}
}

ACPowerSupplyAssistant.prototype.handleChargerNotifications = function(data)
{
	console.log("ACPowerSupply: handleChargerNotifications=" + Object.toJSON( data ) );

	// Is this a charger notification?  // {"type": "usb", "connected":false, "current_mA":0}
	if (data.type === "usb")
	{
		this.isChargingFromAC = data.connected;
		if (data.current_mA != 1000)
			this.isChargingFromAC = false;
	}

	if (this.isChargingFromAC)
	{
		this.handlePluggedIn();
	}

}

ACPowerSupplyAssistant.prototype.handleBatteryNotifications = function(data)
{
	console.log( "ACPowerSupply: handleBatteryNotification=" + Object.toJSON(data) );
	
	// Is the battery level provided?
	if (data.percent) {
		this.BatteryLevel = data.percent;
		this.controller.get('percentage').update( $L("#{temp}% charged.").interpolate({temp: this.BatteryLevel}) );

		if (this.Step == 2)
		{
			this.controller.get('percentage').show( );
			this.controller.get('instructions').update( $L("The charging test is complete.") );
			Logging.log( "AC Adapter", "Battery = " + this.BatteryLevel );
		}
	}
}

ACPowerSupplyAssistant.prototype.activate = function()
{
	this.Step = 0;
	
	// Subscribe to Charger Notifications
	this.ChargerRequest = new Mojo.Service.Request("palm://com.palm.bus/signal/addmatch", {
		parameters: {
			'category': '/com/palm/power',
			'method': 'chargerStatus'
		},
		onSuccess: this.handleChargerNotifications.bind(this),
		onFailure: function() 
			{
			//TODO!!!!
				console.log( "ACPowerSupply: Failed to subscribe to com/palm/power/chargerStatus");
			}
		});

	this.BatteryRequest = new Mojo.Service.Request("palm://com.palm.bus/signal/addmatch", {
		parameters: {
			'category': '/com/palm/power',
			'method': 'batteryStatus'
		},
		onSuccess: this.handleBatteryNotifications.bind(this),
		onFailure: function() 
			{
				//TODO!!!!
				console.log("ACPowerSupply: Failed to subscribe to com/palm/power/batteryStatus");
			},
	});
}

ACPowerSupplyAssistant.prototype.cleanup = function()
{
	SceneChoices.push( "ACPowerSupply" );
}

ACPowerSupplyAssistant.prototype.deactivate = function()
{
	if (this.ChargerRequest != undefined)
		this.ChargerRequest.cancel();

	if (this.BatteryRequest != undefined)
		this.BatteryRequest.cancel();

	this.Initialize();
}

ACPowerSupplyAssistant.prototype.requestBatteryLevel = function()
{
	// This will cause a signal to go through and we'll get a status via handleBatteryNotifications 
	this.request = new Mojo.Service.Request("palm://com.palm.power/com/palm/power/batteryStatusQuery", {} );
}
