package Challenge;

import java.awt.Color;

import Carmen.*;

import LocalNavigation.GUIMessage;

/**
 * <p>Message used to send a string to the GUI</p>
 * 
 * <p>The supplied ({@link #x}, {@link #y}) coordinates define the left side of
 * the baseline of the displayed text.</p>
 *
 * <p>Examples:
 * <pre>
 *    (new GUIStringMessage(0.5, 1.0, "foo").publish(); //use curent color
 *    (new GUIStringMessage(Color.BLUE, 2.5, 0.0, "bar")).publish();
 * </pre></p>
 *
 * @author vona
 **/
public class GUIStringMessage extends GUIMessage {

  /**
   * <p>The string.</p>
   **/
  public String string;

  /**
   * <p>The x coordinate.</p>
   **/
  public double x;

  /**
   * <p>The y coordinate.</p>
   **/
  public double y;
  
  private static final String CARMEN_GUI_STRING_NAME = "carmen_gui_string";
  private static final String CARMEN_GUI_STRING_FMT =
    "{string, double, double, int, int, int, double, string};";
  private static final int IPC_QUEUE_LENGTH = 10;

  /**
   * <p>Create a new GUIStringMessage.</p>
   *
   * <p>{@link #timestamp} and {@link #host} are set to the current time and
   * host, respectively.</p>
   *
   * @param r the red color component, negative to use current color
   * @param g the green color component, negative to use current color
   * @param b the blue color component, negative to use current color
   * @param x the string's left x coordinate in world frame (m)
   * @param y the string's baseline y coordinate in world frame (m)
   * @param string the string
   **/
  public GUIStringMessage(int r, int g, int b,
                          double x, double y, String string) {
    super(r, g, b);
    this.x = x;
    this.y = y;
    this.string = string;
  }
 
  /**
   * <p>Covers {@link #GUIStringMessage(int, int, int, double, double,
   * String)}.</p>
   *
   * @param color the color, or null to use current
   **/
  public GUIStringMessage(Color color, double x, double y, String string) {
    this((color != null) ? color.getRed() : -1,
         (color != null) ? color.getGreen() : -1,
         (color != null) ? color.getBlue() : -1,
         x, y, string);
  }

  /**
   * <p>Convenience cover of {@link #GUIStringMessage(int, int, int,
   * double, double, String)}, always uses current color.</p>
   **/
  public GUIStringMessage(double x, double y, String string) {
    this(-1, -1, -1, x, y, string);
  }
 
  /**
   * <p>Create a new GUIStringMessage.</p>
   *
   */
  public GUIStringMessage() {
    timestamp = Util.getTime();
    host = Util.getHostName();    
  }
  
  /**
   * <p>Subscribe a class to GUIStringMessages.</p>
   * 
   * @param handler a GUIStringHandler
   */
  public static void subscribe(GUIStringHandler handler) {
    subscribe(CARMEN_GUI_STRING_NAME, CARMEN_GUI_STRING_FMT, handler, 
              GUIStringMessage.class, "handle");
    IPC.IPC.setMsgQueueLength(CARMEN_GUI_STRING_NAME, IPC_QUEUE_LENGTH);
  }
  
  /**
   * <p>Publish GUIStringMessage to subscribers</p>
   */
  public void publish() {
    publish(CARMEN_GUI_STRING_NAME, CARMEN_GUI_STRING_FMT, this);
  }
}
