import objectdraw.*;
import java.awt.*;

/*
 * Example Drag4Shirts
 *
 * Drag a shirt around window, given choice of red shirt, blue shirt,
 * green shirt, and yellow shirt.
 *
 * Jim Teresco, Siena College, CSIS 120, Fall 2011
 * Based on example from Williams College CSCI 134
 *
 * $Id: Drag4Shirts.java 1576 2011-03-24 04:32:40Z terescoj $
 */

public class Drag4Shirts extends WindowController {

    // starting location of t-shirt
    private static final int ITEM_LEFT = 75;
    private static final int ITEM_TOP = 50;
    private static final int SND_OFFSET = 150;

    // T-shirts on the screen
    private TShirt redShirt, blueShirt, greenShirt, yellowShirt;

    // The piece of laundry to be moved.
    private TShirt selectedShirt;

    // Previously noted position of mouse cursor
    private Location lastPoint;

    // Whether user is actually dragging a shirt
    private boolean dragging;

    
    // display the shirts
    public void begin() {
        redShirt = new TShirt(ITEM_LEFT, ITEM_TOP, canvas);
        redShirt.setColor(Color.red);

        blueShirt = new TShirt(ITEM_LEFT + SND_OFFSET, ITEM_TOP, canvas);
        blueShirt.setColor(Color.blue);
        
        greenShirt = new TShirt(ITEM_LEFT, ITEM_TOP + SND_OFFSET, canvas);
        greenShirt.setColor(Color.green);

        yellowShirt = new TShirt(ITEM_LEFT + SND_OFFSET, ITEM_TOP + SND_OFFSET, canvas);
        yellowShirt.setColor(Color.yellow);
        
        selectedShirt = blueShirt; // blue shirt on top at start
   }

    // Whenever mouse is depressed, note its location
    // and which (if any) shirt the mouse is on.
    public void onMousePress(Location point) {
        lastPoint = point;

        // test the selected shirt (which is on top) first.
        if (selectedShirt.contains(point)) {
            dragging = true;
        } else if (blueShirt.contains(point)) {
            selectedShirt = blueShirt;
            dragging = true;
            selectedShirt.sendToFront();
        } else if (redShirt.contains(point)) {
            selectedShirt = redShirt;
            dragging = true;
            selectedShirt.sendToFront();
        } else if (greenShirt.contains(point)) {
            selectedShirt = greenShirt;
            dragging = true;
            selectedShirt.sendToFront();
        } else if (yellowShirt.contains(point)) {
            selectedShirt = yellowShirt;
            dragging = true;
            selectedShirt.sendToFront();
        } else {
            dragging = false;
        }
    }

    // If mouse is dragged, move the selected shirt with it
    // If didn't press mouse on a shirt, do nothing
    public void onMouseDrag(Location point) {
        if (dragging) {
            selectedShirt.move(point.getX() - lastPoint.getX(),
                               point.getY() - lastPoint.getY());
            lastPoint = point;
        }
    }

    // move both shirts back to starting positions when mouse leaves window
    public void onMouseExit(Location point) {
        redShirt.reset();
        blueShirt.reset();
        greenShirt.reset();
        yellowShirt.reset();
        dragging = false;
    }
}
