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

/**
 * A laundry sorting game.  The user drags a shirt to the proper
 * "washing machine" according to the shirt's color.
 */
public class LaundrySorter extends WindowController {
    // Constants controlling basket locations
    private static final int BASKETTOPS = 120;
    private static final int BASKETLEFT = 20;
    private static final int BASKETOFFSET = 130;

    // Constants controlling shirt's position and size
    private static final int ITEMLEFT = 115;
    private static final int ITEMTOP = 10;

    // Location where score should be displayed
    private static final int SCORELINE = BASKETTOPS + 150;
    private static final int SCORELEFT = 40;

    private RandomIntGenerator pickAColor = new RandomIntGenerator(0,255);
    
    //The piece of laundry to be sorted.
    private TShirt item;
    
    // baskets for white, dark, and color washing machines.
    private LaundryBasket white;
    private LaundryBasket dark;
    private LaundryBasket colors;

    // The basket corresponding to shirt's color
    private LaundryBasket correct;

    // Previously noted position of mouse cursor, and whether we're draggin
    private Location lastPoint;
    private boolean dragging;

    // counters to measure trainees accuracy
    private int numSorted, mistakes;

    // Display of current result
    private Text scoreDisplay;

    // Initializes the applet by constructing all the objects.
    public void begin() {

        white = new LaundryBasket(BASKETLEFT + 0 * BASKETOFFSET, BASKETTOPS, "whites", canvas);
        dark = new LaundryBasket(BASKETLEFT + 1 * BASKETOFFSET, BASKETTOPS, "darks", canvas);
        colors = new LaundryBasket(BASKETLEFT + 2 * BASKETOFFSET, BASKETTOPS, "colors", canvas);

        numSorted = 0;
        mistakes = 0;

        scoreDisplay = new Text("Correct: " + numSorted + "    Incorrect: " + mistakes, SCORELEFT, SCORELINE, canvas);
        scoreDisplay.setFontSize(13);

        item = new TShirt(ITEMLEFT, ITEMTOP, canvas);
        item.setColor(Color.red);

    }

    // Whenever mouse is depressed, note its location
    public void onMousePress(Location point) {
        lastPoint = point;
        dragging = item.contains(point);
    }

    // If mouse is dragged from a position within the item, move the item with it
    public void onMouseDrag(Location point) {
        if (dragging) {
            item.move(point.getX() - lastPoint.getX(), point.getY() - lastPoint.getY());
            lastPoint = point;
        }
    }

    // Checks if the item has been place in the correct basket
    public void onMouseRelease(Location point) {

        if (dragging) {
            // Determine correct basket
            if (item.colorValue() > 600) {
                correct = white;
            } else if (item.colorValue() > 250) {
                correct = colors;
            } else {
                correct = dark;
            }
        }

        // if the object was dragged to the correct basket
        if (correct.contains(point)) {
            numSorted = numSorted + 1;
            item.setColor(new Color(pickAColor.nextValue(), pickAColor.nextValue(), pickAColor.nextValue()));
        } else {
            mistakes = mistakes + 1;
        }

        item.moveTo(ITEMLEFT, ITEMTOP);

        scoreDisplay.setText("Correct: " + numSorted + "    Incorrect: " + mistakes);

    }
}
