package LocalNavigation;

import java.awt.Color;

import Carmen.*;

/**
 * <p>Message used to send a segment to the GUI</p>
 * 
 * <p>Examples:
 * <pre>
 *    (new GUISegmentMessage(0.0, 0.0, 1.0, 2.0)).publish(); //use curent color
 *    (new GUISegmentMessage(Color.BLUE, 0.0, 0.0, 1.0, 2.0)).publish();
 * </pre></p>
 *
 * @author vona
 **/
public class GUISegmentMessage extends GUIMessage {

  /**
   * <p>The segment's start x coordinate in world frame (m).</p>
   **/
  public double sx;

  /**
   * <p>The segment's start y coordinate in world frame (m).</p>
   **/
  public double sy;

  /**
   * <p>The segment's end x coordinate in world frame (m).</p>
   **/
  public double ex;

  /**
   * <p>The segment's end y coordinate in world frame (m).</p>
   **/
  public double ey;

  private static final String CARMEN_GUI_SEGMENT_NAME = "carmen_gui_segment";
  private static final String CARMEN_GUI_SEGMENT_FMT =
    "{double, double, double, double, int, int, int, double, string};";
  private static final int IPC_QUEUE_LENGTH = 10;
  
  /**
   * <p>Create a new GUISegmentMessage.</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 sx the point's start x coordinate in world frame (m)
   * @param sy the point's start y coordinate in world frame (m)
   * @param ex the point's end x coordinate in world frame (m)
   * @param ey the point's end y coordinate in world frame (m)
   **/
  public GUISegmentMessage(int r, int g, int b,
                           double sx, double sy, double ex, double ey) {
    super(r, g, b);
    this.sx = sx;
    this.sy = sy;
    this.ex = ex;
    this.ey = ey;
  }
  
  /**
   * <p>Covers {@link #GUISegmentMessage(int, int, int, double, double,
   * double, double)}.</p>
   *
   * @param color the color, or null to use current
   **/
  public GUISegmentMessage(Color color,
                           double sx, double sy, double ex, double ey) {
    this((color != null) ? color.getRed() : -1,
         (color != null) ? color.getGreen() : -1,
         (color != null) ? color.getBlue() : -1,
         sx, sy, ex, ey);
  }

  /**
   * <p>Convenience cover of {@link #GUISegmentMessage(int, int, int,
   * double, double, double, double)}, always uses current color.</p>
   **/
  public GUISegmentMessage(double sx, double sy, double ex, double ey) {
    this(-1, -1, -1, sx, sy, ex, ey);
  }
  
  /**
   * <p>Create a new GUISegmentMessage.</p>
   *
   */
  public GUISegmentMessage() {
    timestamp = Util.getTime();
    host = Util.getHostName();    
  }
  
  
  /**
   * <p>Subscribe a class to GUISegmentMessages.</p>
   * 
   * @param handler a GUISegmentHandler
   */
  public static void subscribe(GUISegmentHandler handler) {
    subscribe(CARMEN_GUI_SEGMENT_NAME, CARMEN_GUI_SEGMENT_FMT, handler, 
              GUISegmentMessage.class, "handle");
    IPC.IPC.setMsgQueueLength(CARMEN_GUI_SEGMENT_NAME, IPC_QUEUE_LENGTH);
  }
  
  /**
   * <p>Publish GUISegmentMessage to subscribers</p>
   */
  public void publish() {
    publish(CARMEN_GUI_SEGMENT_NAME, CARMEN_GUI_SEGMENT_FMT, this);
  }
}
