package Challenge;

import java.awt.*;
import java.awt.geom.*;

/**
 * <p>Represents a spherical fiducial object.<p>
 *
 * @author Marty Vona
 **/
public class Fiducial {

  /**
   * <p>Zero-based index of this fiducial.</p>
   **/
  public final int index;

  /**
   * <p>x coord of this fiducial in world frame.</p>
   **/
  public final double x;

  /**
   * <p>y coord of this fiducial in world frame.</p>
   **/
  public final double y;

  /**
   * <p>z coord of this fiducial in world frame.</p>
   **/
  public final double z;
  
  /**
   * <p>radius of this fiducial in meters.</p>
   **/
  public double radius;

  /**
   * <p> radius of footprint on GUI in meters </p>
   */
  public double footprint_radius;
  
  /**
   * <p>The projection of this fiducial on the xy plane.</p>
   **/
  public final Ellipse2D.Double footprint;

  /**
   * <p>The color of this fiducial.</p>
   **/
  public final Color color;

  /**
   * <p>Construct a new fiducial object.</p>
   *
   * @param index zero-based index of this fiducial
   * @param x x coord of this fiducial in world frame
   * @param y y coord of this fiducial in world frame
   * @param z z coord of this fiducial in world frame
   * @param radius radius of this fiducial in meters
   * @param h Hue color component of this fiducial
   * @param s Saturation color component of this fiducial
   * @param b Brightness color component of this fiducial
   **/
  public Fiducial(int index,
                  double x, double y, double z,
                  double radius,
                  String the_color) {

    this.index = index;

    this.x = x;
    this.y = y;
    this.z = z;
    this.radius = radius;
    
    //This code is to handle for fiducial pairings where
    //the fiducials are on top of each other. fiducials
    //which are lower will appear bigger.
    if(z > 0.3){
    	this.footprint_radius = radius;
    }
    else{
    	this.footprint_radius = 2*radius;
    }

    if (the_color.toLowerCase().compareTo("red") == 0) {
    	this.color = Color.red;
    }
    else if (the_color.toLowerCase().compareTo("green") == 0) {
    	this.color = Color.green;
    }
    else if (the_color.toLowerCase().compareTo("yellow") == 0) {
    	this.color = Color.yellow;
    }
    else if (the_color.toLowerCase().compareTo("lime") == 0) {
    	this.color = Color.cyan;
    }
    else { // string is "blue"
    	this.color = Color.blue;
    }

    footprint =
      new Ellipse2D.Double(x-this.footprint_radius, y-this.footprint_radius, this.footprint_radius*2.0, this.footprint_radius*2.0);
  }

  /**
   * <p>Covers {@link #toStringBuffer}, internally conses a StringBuffer.</p>
   **/
  public String toString() {
    StringBuffer sb = new StringBuffer();
    toStringBuffer(sb);
    return sb.toString();
  }
  
  /**
   * <p>Append a human-readable string representation of this fiducial to a
   * StringBuffer.</p>
   *
   * @param sb the StringBuffer
   **/
  public void toStringBuffer(StringBuffer sb) {
    sb.append(Integer.toString(index));
    sb.append(": r = ");
    sb.append(Double.toString(radius));
    sb.append(" @ (");
    sb.append(Double.toString(x));
    sb.append(", ");
    sb.append(Double.toString(y));
    sb.append(", ");
    sb.append(Double.toString(z));
    sb.append(")");
  }
}

