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 DISPLAYSIZE = 16; // in points

    // Location and dimensions of the hoop
    private static final int HOOPTOP = 50;
    private static final int HOOPLEFT = 160;
    private static final int HOOPWIDTH = 100;
    private static final int HOOPHEIGHT = 60;

    // Initial location and dimensions of the ball
    private static final int BALLX = 190;
    private static final int BALLY = 300;
    private static final int BALLSIZE = 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(DISPLAYSIZE);

        hoop = new FramedOval(HOOPLEFT, HOOPTOP, HOOPWIDTH, HOOPHEIGHT, canvas);
        ball = new FilledOval(BALLX, BALLY, BALLSIZE, BALLSIZE, 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 if (ballGrabbed) {
            display.setText("You Missed!");
        }
        ball.moveTo(BALLX, BALLY);
    }
}
