import objectdraw.*;

import java.awt.*;

/*
 * A simple basketball program.  It adds 2 points to 
 * a displayed score whenever the
 * mouse is DRAGGED into the oval.
 */
public class Basketball extends WindowController {
  // Location of the display
  private static final int DISPLAY_X = 150;
  private static final int DISPLAY_Y = 200;
  private static final int DISPLAY_SIZE = 16; // in points

  // Location and dimensions of the hoop
  private static final int HOOP_TOP = 50;
  private static final int HOOP_LEFT = 160;
  private static final int HOOP_WIDTH = 100;
  private static final int HOOP_HEIGHT = 60;

  // Initial location and dimensions of the ball
  private static final int BALL_X = 190;
  private static final int BALL_Y = 300;
  private static final int BALL_SIZE = 40;
  private static final Color BALL_COLOR = new Color(250, 115, 10);

  // the Text object which displays the count
  private Text display;

  // the oval that represent the hoop
  private FramedOval hoop;

  // the other oval (that represents the ball)
  private FilledOval ball;

  // the number of points
  private int score;

  // the last previous known location of the mouse
  private Location lastMouse;

  // Is the ball currently being dragged around the screen?
  private boolean ballGrabbed;

  /*
   * Initialize the counter and the text message.
   */
  public void begin() {
    score = 0;
    display = new Text("Your score is 0", DISPLAY_X, DISPLAY_Y, canvas);
    display.setFontSize(DISPLAY_SIZE);

    hoop = new FramedOval(HOOP_LEFT, HOOP_TOP, HOOP_WIDTH, HOOP_HEIGHT, canvas);
    
    ball = new FilledOval(BALL_X, BALL_Y, BALL_SIZE, BALL_SIZE, canvas);
    ball.setColor(BALL_COLOR);
  }

  /*
   * Note where mouse is depressed.
   */
  public void onMousePress(Location point) {
    lastMouse = point;
    ballGrabbed = ball.contains(point);
  }

  /*
   * Move the ball.
   */
  public void onMouseDrag(Location point) {
    if (ballGrabbed) {
      ball.move(point.getX() - lastMouse.getX(),
                point.getY() - lastMouse.getY());
      lastMouse = point;
    }
  }

  /*
   * Add to the score and update the text if player scores.
   */
  public void onMouseRelease(Location point) {
    if (ballGrabbed && hoop.contains(point)) {
      score = score + 2;
      display.setText("Your score is " + score);
    } else {
      display.setText("You Missed!");
    }
    ball.moveTo(BALL_X, BALL_Y);
  }
}
