/**
 * Class to recursively draw broccoli
 */
import objectdraw.*;
import java.awt.*;

public class BroccoliBranch extends ActiveObject
      implements BroccoliPart {

  private static final int pauseTime = 1000;  // pause 1 sec before drawing

  // dark green color for broccoli
  private static final Color BROCCOLI_COLOR = new Color(0,135,0);

  // Below this size draw flower
  private static final double MINSIZE = 25.0;

  // How much broccoli shrinks each call
  private final static double TOP_FRACT = 0.8;

  // stem of broccoli
  private AngLine stem;

  // branches of broccoli
  private BroccoliPart left, center, right;

  private DrawingCanvas canvas;                                // canvas to draw on

  private double size;                                                        // size of stem
  private double direction;                                         // angle of stem with positive x-axis

  /**
   * Draw broccoli by recursively drawing branches (and flower)
   */
  public BroccoliBranch(Location startLocation, double size,
                        double direction, DrawingCanvas canvas)
  {
    // Draw stem and color green
    stem = new AngLine(startLocation, size, direction, canvas);
    stem.setColor(BROCCOLI_COLOR);

    this.canvas = canvas;                                                // save parameters for execute
    this.direction = direction;
    this.size = size;
    start();
  }

  public void run() {
    Location destLocation = stem.getEnd();        // end of stem

    pause(pauseTime);

    if ( size > MINSIZE ) {                  // draw branches
      left = new BroccoliBranch(destLocation, size * TOP_FRACT,
                                direction + Math.PI/9.0, canvas);
      center = new BroccoliBranch(destLocation, size * TOP_FRACT,
                                  direction, canvas);
      right = new BroccoliBranch(destLocation, size * TOP_FRACT,
                                 direction - Math.PI/9.0, canvas);
    } else {                                 // draw flowers
      left = new Flower(destLocation, size * TOP_FRACT,
                        direction + Math.PI/9.0, canvas);
      center = new Flower(destLocation, size * TOP_FRACT,
                          direction, canvas);
      right = new Flower(destLocation, size * TOP_FRACT,
                         direction - Math.PI/9.0, canvas);
    }

  }

  /**
   * @param x,y amount to move broccoli
   */
  public void move( double x, double y) {
    stem.move(x,y);                                        // move stem

    left.move(x,y);                                        // move other parts
    center.move(x,y);
    right.move(x,y);
  }

  /**
   * @param point location to be checked for containment in broccoli
   */
  public boolean contains( Location point ) {
    return (stem.contains(point) || left.contains(point) ||
            center.contains(point) || right.contains(point));
  }

}
