import objectdraw.*;

/**
 * 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 ITEM_LEFT = 115;
  private static final int ITEM_TOP = 10;

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

  //The piece of laundry to be sorted.
  private LaundryItem 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;

  // Determines if next laundry should be a tshirt or a pair of pants
  private RandomIntGenerator laundryGenerator = new RandomIntGenerator(1, 2);

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

  // true if the tshirt is being dragged
  private boolean draggingIt;

  // counters to measure trainees accuracy
  private int numsorted;

  // counters to measure trainees accuracy
  private int 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(ITEM_LEFT, ITEM_TOP, canvas);
  }

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

  // If mouse is dragged from a position within the item, move the item with it
  public void onMouseDrag(Location point) {
    if (draggingIt) {
      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 (draggingIt) {
      // 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.removeFromCanvas();

        // Make a new item
        int num = laundryGenerator.nextValue();

        if (num == 1) {
          item = new TShirt(ITEM_LEFT, ITEM_TOP, canvas);
          
        } else {
          item = new Pants(ITEM_LEFT, ITEM_TOP, canvas);
        }

      } else {
        mistakes = mistakes + 1;
        item.moveTo(ITEM_LEFT, ITEM_TOP);
      }
    }

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