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

/**
 * 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
{

	private static final int BASKETTOPS = 150;	// Constants controlling basket locations
	private static final int BASKETLEFT = 20;
	private static final int BASKETOFFSET = 100;

	private static final int ITEM_LEFT = 115;		// Constants controlling shirt's position and size
	private static final int ITEM_TOP = 50;
	
	private static final int SCORELINE = 250;		// Location where score should be displayed
	private static final int SCORELEFT = 40;
	
	private static final int FONTSIZE = 16;

  /**
   * The piece of laundry to be sorted.
   */
   Tshirt item;
  
  /**
   * baskets for white, dark, and color washing machines.
   */
   LaundryBasket white;
   LaundryBasket dark;
   LaundryBasket colors;
   
  	// The basket corresponding to shirt's color
   LaundryBasket correct;
   
  	// Previously noted position of mouse cursor
  	Location lastPoint;
  
   	// counters to measure trainees accuracy
   	int numsorted, mistakes;
  
  	// Display of current result
  	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);
 	
    item= new Tshirt(ITEM_LEFT,ITEM_TOP,canvas);
    
   	numsorted = 0;
   	mistakes = 0;
   	
   	scoreDisplay = new Text("Correct: " + numsorted + "    Incorrect: " + mistakes,
   	                         SCORELEFT,SCORELINE,canvas);
   	scoreDisplay.setFontSize(FONTSIZE);
   	
    correct=white;
    
  }
  
  
  	// Whenever mouse is depressed, note its location
  public void onMousePress(Location point)
  {
  		lastPoint = point;
  }

		// If mouse is dragged from a position within the item, move the item with it
	public void onMouseDrag(Location point)
	{
		if ( item.contains(lastPoint) )
		{
			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 (item.contains(lastPoint))
	{ 
		// 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.hide ();
	    	
				// Get next shirt.
			item= new Tshirt(ITEM_LEFT,ITEM_TOP,canvas);
	  	 
		} else {
			mistakes = mistakes + 1;
			item.moveTo(ITEM_LEFT,ITEM_TOP);
	   	}
	       	
	    scoreDisplay.setText("Correct: " + numsorted + "    Incorrect: " + mistakes);

	   }
	}
  }