import context.arch.BaseObject;
import context.arch.comm.DataObject;
import context.arch.widget.WPresenceGroupURL;
import context.arch.handler.Handler;
import context.arch.subscriber.Subscriber;
import context.arch.storage.AttributeNameValues;
import context.arch.MethodException;
import context.arch.InvalidMethodException;
import context.arch.comm.language.DecodeException;
import context.arch.comm.language.InvalidDecoderException;
import context.arch.comm.language.EncodeException;
import context.arch.comm.language.InvalidEncoderException;
import context.arch.comm.protocol.ProtocolException;
import context.arch.comm.protocol.InvalidProtocolException;
import context.arch.util.Error;

import java.util.Vector;

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

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

  /**
   * Subscription ID for WPresenceGroupURL UPDATE callback
   */
  public static final String SUBSCRIBER_ID = "PresenceGroupURLApp";

  /**
   * Constructor that creates an instance of BaseObject at the DEFAULT_PORT.  It
   * subscribes to the WPresenceGroupURL and prints out callback info.
   *
   * @param location Location of the WPresenceGroupURL
   * @param prgwHost Hostname the widget is running on
   * @param prgwPort Port the widget is running on
   * @see context.arch.BaseObject
   * @see context.arch.BaseObject#subscribeTo(context.arch.handler.Handler, java.lang.String, int, java.lang.String,
		java.lang.String, int, java.lang.String, java.lang.String, java.lang.String)
   * @see context.arch.widget.WPresenceGroupURL
   */
  public PresenceGroupURLApp(String location, String prgwuHost, int prgwuPort) {
    this(location, DEFAULT_PORT, prgwuHost, prgwuPort);
  }

  /**
   * Constructor that creates an instance of BaseObject at the given port.  It
   * subscribes to the WPresenceGroupURL and prints out callback info and polls
   * the widget and prints out the results.
   *
   * @param location Location of the WPresenceGroupURL
   * @param localport Port the app is running on
   * @param prgwHost Hostname the widget is running on
   * @param prgwPort Port the widget is running on
   * @see context.arch.BaseObject
   * @see context.arch.BaseObject#subscribeTo(context.arch.handler.Handler, java.lang.String, int, java.lang.String,
		java.lang.String, int, java.lang.String, java.lang.String, java.lang.String)
   * @see context.arch.widget.WPresenceGroupURL
   */
  public PresenceGroupURLApp(String location, int localport, String prgwuHost, int prgwuPort) {
    BaseObject server = new BaseObject (localport);
    Error done = server.subscribeTo((Handler)this, localport, SUBSCRIBER_ID,
			prgwuHost, prgwuPort, WPresenceGroupURL.CLASSNAME+"_"+location, 
			WPresenceGroupURL.UPDATE, "PresenceGroupURLUpdate");
    System.out.println("done was "+done);
  }

  /**
   * 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 an IButton is docked with the reader in the WPersonPresence.
   *
   * @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.WPersonPresence
   * @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("PresenceGroupURLUpdate")) {
      System.out.println("\n\n\n**********************************************\n");
      AttributeNameValues anvs = new AttributeNameValues(data);
      String who = (String)anvs.getAttributeNameValue(WPresenceGroupURL.USERID).getValue();
      String url = (String)anvs.getAttributeNameValue(WPresenceGroupURL.URL).getValue();
      String when = (String)anvs.getAttributeNameValue(WPresenceGroupURL.TIMESTAMP).getValue();
      String where = (String)anvs.getAttributeNameValue(WPresenceGroupURL.LOCATION).getValue();
      System.out.print(who+" has the url ");
      System.out.print(url+" at the location ");
      System.out.print(where+" at time ");
      System.out.println(when+"\n");
      System.out.println("**********************************************\n");
    }
    else {
      throw new InvalidMethodException(Error.UNKNOWN_CALLBACK_ERROR);
    }
    return null;
  }


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

}
