package context.apps.PersonPresenceApp;

import context.arch.BaseObject;
import context.arch.comm.DataObject;
import context.arch.widget.WPersonNamePresence2;
import context.arch.handler.Handler;
import context.arch.InvalidMethodException;
import context.arch.MethodException;
import context.arch.storage.AttributeNameValues;
import context.arch.util.Error;

/**
 * This class is a test application that uses the WPersonNamePresence2.  It
 * contains code that tests subscribing to the widget.
 * @see context.arch.handler.Handler 
 * @see context.arch.widget.WPersonNamePresence2
 */
public class SimpleApp implements Handler {

  /** 
   * Default port to start the BaseObject on is 5555
   */
  public static final int DEFAULT_PORT = 5555;

  /**
   * Subscription ID for WPersonNamePresence2 UPDATE callback
   */
  public static final String SUBSCRIBER_ID = "SimpleApp";

  private int numHandles = 0;
  private int port;  

  /**
   * Constructor that creates an instance of BaseObject at the given port.  It
   * subscribes to the WPersonNamePresence2 and prints out callback info and polls
   * the widget and prints out the results.
   *
   * @param localport Port the app is running on
   * @param ppwHost Hostname the widget is running on
   * @param ppwPort Port the widget is running on
   * @see context.arch.BaseObject
   * @see context.arch.BaseObject#subscribeTo(context.arch.handler.Handler, int, java.lang.String,
		java.lang.String, int, java.lang.String, java.lang.String, java.lang.String)
   * @see context.arch.widget.WPersonNamePresence2
   */
  public SimpleApp(String location, int localport, String ppwHost, int ppwPort) {
    BaseObject server = new BaseObject (localport);
    port = localport;
    String widgetId = WPersonNamePresence2.CLASSNAME+"_"+location;
    
    Error error5 = server.subscribeTo(this, port, SUBSCRIBER_ID, ppwHost, ppwPort, widgetId,
                 WPersonNamePresence2.UPDATE, "presenceUpdate");
    System.out.println("Subscription with no attributes/conditions: "+error5.getError());
  }

  /**
   * This method is required to implement the Handler interface.  It is called when
   * the callbacks this object subscribes to are triggered.  In this case, it is
   * called when a button is pressed in the WPersonNamePresence2 GUI.
   *
   * @param callback Name of the callback to handle
   * @param data DataObject containing the callback event data
   * @see context.arch.handler.Handler
   * @see context.arch.widget.WPersonNamePresence2
   * @exception context.arch.InvalidMethodException if the callback specified isn't recognized	
   * @exception context.arch.MethodException if the callback specified can't be handled successfully
   */
  public DataObject handle(String callback, DataObject data) throws InvalidMethodException, MethodException {
    if (callback.equals("presenceUpdate")) {
      AttributeNameValues atts = new AttributeNameValues(data);
      System.out.println("subscription results are: "+atts);
    }
    return null;
  }


  /**
   * Main method that creates an instance of SimpleApp
   */
  public static void main (String argv[]) {
    if (argv.length == 4) {
      System.out.println("Attempting to create a SimpleApp on "+argv[2]+" at "+argv[0]);    
      SimpleApp sa = new SimpleApp(argv[0], Integer.parseInt(argv[1]), argv[2], Integer.parseInt(argv[3]));
    }
    else {
      System.out.println("USAGE: java SimpleApp <location> [port] <WPersonNamePresence2Host/ip> <WPersonNamePresence2Port>");
    }
  }

}
