package LocalNavigation;

import java.awt.Color;

import Carmen.*;

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

  /**
   * <p>The line's a coordinate in world frame.</p>
   **/
  public double lineA;

  /**
   * <p>The line's b coordinate in world frame.</p>
   **/
  public double lineB;

  /**
   * <p>The line's c coordinate in world frame.</p>
   **/
  public double lineC;

  private static final String CARMEN_GUI_LINE_NAME = "carmen_gui_line";
  private static final String CARMEN_GUI_LINE_FMT =
    "{double, double, double, int, int, int, double, string};";
  private static final int IPC_QUEUE_LENGTH = 10;
  
  /**
   * <p>Create a new GUILineMessage.</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 lineA the line's a coordinate in world frame
   * @param lineB the line's b coordinate in world frame
   * @param lineC the line's c coordinate in world frame
   **/
  public GUILineMessage(int r, int g, int b,
                        double lineA, double lineB, double lineC) {
    super(r, g, b);
    this.lineA = lineA;
    this.lineB = lineB;
    this.lineC = lineC;
  }
  
  /**
   * <p>Covers {@link #GUILineMessage(int, int, int, double, double,
   * double)}.</p>
   *
   * @param color the color, or null to use current
   **/
  public GUILineMessage(Color color,
                        double lineA, double lineB, double lineC) {
    this((color != null) ? color.getRed() : -1,
         (color != null) ? color.getGreen() : -1,
         (color != null) ? color.getBlue() : -1,
         lineA, lineB, lineC);
  }

  /**
   * <p>Convenience cover of {@link #GUILineMessage(int, int, int,
   * double, double, double)}, always uses current color.</p>
   **/
  public GUILineMessage(double lineA, double lineB, double lineC) {
    this(-1, -1, -1, lineA, lineB, lineC);
  }
  
  /**
   * <p>Create a new GUILineMessage.</p>
   *
   */
  public GUILineMessage() {
    timestamp = Util.getTime();
    host = Util.getHostName();    
  }
  
  
  /**
   * <p>Subscribe a class to GUILineMessages.</p>
   * 
   * @param handler a GUILineHandler
   */
  public static void subscribe(GUILineHandler handler) {
    subscribe(CARMEN_GUI_LINE_NAME, CARMEN_GUI_LINE_FMT, handler, 
              GUILineMessage.class, "handle");
    IPC.IPC.setMsgQueueLength(CARMEN_GUI_LINE_NAME, IPC_QUEUE_LENGTH);
  }
  
  /**
   * <p>Publish GUILineMessage to subscribers</p>
   */
  public void publish() {
    publish(CARMEN_GUI_LINE_NAME, CARMEN_GUI_LINE_FMT, this);
  }
}
