

class Scale
{
  int w,h;
  color c;
  int currX, currY;
  char currSide;
  int topPart, offset;
  int leeway;
  
  public Scale(int newW, int newH, color newC)
  {
    currX = currY = -1;
    currSide = ' ';
    
    w = newW;
    h = newH;
    c = newC;
    
    topPart = 10;
    offset = 25;
    leeway = 0;//30;
  }
  
  // Draw with x,y in the bottom left corner of the scale
  public void draw(int x, int y)
  {
    currX=x;
    currY=y;

    fill(c);
    rect(x, y-h+topPart, w, h-topPart);
    rect(x+offset, y-h, (w-(3*offset))/2, topPart);
    rect(x + (2*offset) + ((w-(3*offset))/2), y-h, (w-(3*offset))/2, topPart);
    
    PFont font = loadFont("BerlinSansFB-Reg-48.vlw");
    textFont(font);
    fill(0);
    textAlign(CENTER);
    text("heavier-o-meter", currX + w/2, currY - 2);
  }
  
  // Draw with one side of the scale lit up to show which item is heavier
  // Arg side must be l or r
  public void drawHeavier(char side)
  {
    currSide = side;
    fill(c);
    rect(currX, currY-h+topPart, w, h-topPart);
    PFont font = loadFont("BerlinSansFB-Reg-48.vlw");
    textFont(font);
    fill(0);
    textAlign(CENTER);
    text("heavier-o-meter", currX + w/2, currY - 2);
    
    
    if (side == 'l' || side == 'L')
    {
      fill(250,0,0);
      rect(currX+offset, currY-h, (w-(3*offset))/2, topPart);
      
      fill(c);
      rect(currX + (2*offset) + ((w-(3*offset))/2), currY-h, (w-(3*offset))/2, topPart);
    }
    else if (side == 'r' || side == 'R')
    { 
      fill(c);
      rect(currX+offset, currY-h, (w-(3*offset))/2, topPart);
      
      fill(250,0,0);
      rect(currX + (2*offset) + ((w-(3*offset))/2), currY-h, (w-(3*offset))/2, topPart);
    }
    else
    {
      fill(c);
      rect(currX+offset, currY-h, (w-(3*offset))/2, topPart);
      
      fill(c);
      rect(currX + (2*offset) + ((w-(3*offset))/2), currY-h, (w-(3*offset))/2, topPart);
    }
  }
  
  public boolean hitTest(int x, int y, int bottleClickX, int bottleClickY, int bottleW, int bottleH)
  {
    int leftOfBottle = bottleClickX;
    int rightOfBottle = bottleW - leftOfBottle;
    
    int aboveBottle = abs(bottleClickY);
    int belowBottle = bottleH - aboveBottle;
    
    
    int startX = currX - rightOfBottle - leeway;
    int endX = currX + w + leftOfBottle + leeway;
    
    //int startY = currY - h /*- aboveBottle*/ - 200 - leeway;
    int startY = currY - h - (200+35);
    int endY = currY + belowBottle + leeway;
    
    if (x >= startX && x <= endX &&
        y >= startY && y <= endY)
    {
      return true;
    }
    else
    {
      return false;    
    }
  }
  
  // Which side is the mouse closer to?
  public char whichSide(int x, int y)
  {
    if (x <= currX + w/2)
      return 'l';
    else
      return 'r';
  }
  
  
  // Return the x coordinate of the midpoint of a particular side of the scale
  public int midXOfSide(char side)
  {
    
    //rect(x+offset, y-h, (w-(3*offset))/2, topPart);
    //rect(x + (2*offset) + ((w-(3*offset))/2), y-h, (w-(3*offset))/2, topPart);
    
    if (side == 'l')
    {
      return currX + offset + (w-(3*offset))/4;
    }
    else
    {
      return currX + offset + (w-(3*offset))/2 + offset + (w-(3*offset))/4;
    }
  }
  
  // Return the y coordinate of the midpoint of a particular side of the scale
  public int midYOfSide(char side)
  {
    // The side doesn't matter for y-coordinate
    return currY - (h);
  }
  
  
}
