/**
 *	Generated by Dragonfly SDK
 *
 */
package gpsconfig.servicetracker;

import java.io.IOException;

import org.osgi.framework.BundleContext;

import com.buglabs.application.AbstractServiceTracker;
import com.buglabs.bug.module.gps.pub.IGPSModuleControl;
import com.buglabs.menu.AbstractMenuNode;
import com.buglabs.menu.IMenuNode;
import com.buglabs.menu.IMenuProvider;

 /**
 *	Service tracker for the BugApp Bundle;
 *
 */
public class GPSConfigServiceTracker extends AbstractServiceTracker {	
	private static final String MENU_PATH = "GPS Config";
	private static final String MENU_ITEM_TO_ACTIVE = "Switch to active";
	private static final String MENU_ITEM_TO_PASSIVE = "Switch to passive";
	private IGPSModuleControl gpsControl;
	private IMenuProvider menuProvider;
	private IMenuNode menuNode;

	public GPSConfigServiceTracker(BundleContext context) {
		super(context);
	}
	
	/**
	 * Determines if the application can start.
	 */
	public boolean canStart() {
		return super.canStart();
	}
	
	/**
	 * If canStart returns true
     * this method is called to start the application.
     * Place your fun logic here. 
	 */
	public void doStart() {
		System.out.println("GPSConfigServiceTracker: start");
		gpsControl = (IGPSModuleControl) getService( IGPSModuleControl.class );
		menuProvider = (IMenuProvider) getService(IMenuProvider.class);
		menuNode = new AbstractMenuNode(
				usingActiveAntenna() ? MENU_ITEM_TO_PASSIVE : MENU_ITEM_TO_ACTIVE) {
			public void execute() throws Exception {
				toggleAntenna();
			}
		};
		menuProvider.registerMenu(MENU_PATH, menuNode);
	}

	/**
	 * Called when a service that this application depends is unregistered.
	 */
	public void doStop() {
		System.out.println("GPSConfigServiceTracker: stop");
		menuProvider.unregisterMenu(MENU_PATH);
	}

	/**
	 * Allows the user to set the service dependencies by
     * adding them to services list returned by getServices().
     * i.e.nl getServices().add(MyService.class.getName());
	 */
	public void initServices() {
		getServices().add(IGPSModuleControl.class.getName());
		getServices().add(IMenuProvider.class.getName());
	}
	
	private void toggleAntenna() {
		try {
			// switch to the opposite antenna from what we are using right now
			if (usingActiveAntenna()) {
				System.out.println("Switching to passive antenna");
				final int ret = gpsControl.setPassiveAntenna();
				System.out.println("ret="+ret);
			}
			else {
				System.out.println("Switching to active antenna");
				final int ret = gpsControl.setActiveAntenna();
				System.out.println("ret="+ret);
			}

			menuNode.setName(usingActiveAntenna() ? MENU_ITEM_TO_PASSIVE : MENU_ITEM_TO_ACTIVE);
			menuProvider.notifyMenuUpdate(MENU_PATH);
		}
		catch (IOException e) {
			System.err.println("Unable to toggle antenna");
			e.printStackTrace();
		}
	}
	
	private boolean usingActiveAntenna() {
		try {
			/*
			 * ...
			 * bit 7, 6: 0, 1 Passive Antenna
			 * 			 1, 0 Active Antenna
			 * ...
			 */
			final int status = gpsControl.getStatus();
			System.out.println("status="+status);
			// mask off bits 6 and 7 and check for 7=1 and 6=0
			return (status & 0xC0) == IGPSModuleControl.STATUS_ACTIVE_ANTENNA;
		} catch (IOException e) {
			System.err.println("Unable to query antenna state");
			e.printStackTrace();
		}
		return false;
	}
}