import java.awt.Point;


/**
 * Useful abstraction of a point or a rectangle,
 * with facilities for parsing and conversion.
 */
public class Location
{
  // Fields
  protected int _x;
  protected int _y;
  protected int _w;
  protected int _h;
  protected String _name;
  protected String _desc;
  int tag;

  // contructors 

  /** create a 0-size location at 0,0 */
  public Location() { _x=0; _y=0; _w=0; _h=0; }

  /** create a 0-size location at x,y */
  public Location(int x, int y)
  {  _x = 0; _y = y; _w = 0; _h = 0; }

  /** create a location filling in all params */
  public Location(int x, int y, int w, int h)
  { _w = w; _x = x; _y = y; _h = h; }

  /** create a location from a Point */
  public Location(Point p)
  { _x = p.x; _y = p.y; _w = 0; _h = 0; }

  /** parse a location from a string of format
   *  x,y  or  x,y,w,h.
   */
  public Location(String s)
  { this(); fromString(s);  }

  // internal methods
  protected final boolean zerosized()
  { return (_w == 0 && _h == 0); }

  
  // accessors and conversions
  public int   x() { return _x; }
  public int   y() { return _y; }
  public int   w() { return _w; }
  public int   h() { return _h; }

  public final void setName(String n) { _name = n; }
  public final void setDesc(String d) { _desc = d; }
  public final String name() 
        { return (_name == null)?("noname"):(_name); }
  public final String desc()
        { return (_desc == null)?(""):(_desc); }

  /** convert Location to Point */
  public Point toPoint() { return new Point(_x, _y); }

  /** format this Location as a string */
  public String coordString() 
  {
     if (zerosized()) {
        return ("" + _x + "," + _y);
     } else {
        return ("" + _x +","+_y+","+_w+","+_h);
     }
  }

  // operational methods 
  public boolean within(Point p, int range) 
  {
    if (zerosized()) {
      if (Math.abs(p.x - _x) <= range &&
          Math.abs(p.y - _y) <= range)
        return true;
    }
    else {
      if ( (p.x >= (_x - range)) &&
           (p.x <= (_x + _w + range)) &&
           (p.y >= (_y - range)) &&
           (p.y <= (_y + _h + range)))
        return true;    
    }
    return false;
  }

 
  // internal methods

  /**
   * parse a string of format  x,y  or x,y,w,h
   * into a Location.  Also, a name for the location
   * may be given, preceding a colon, for example:
   *
   *      Lincoln, Nebraska:127,133
   */
  private final void fromString(String s)
  {
    int ix, bx, cx, lx;
    int[] ia;
    ia = new int[4];
    s = s.trim();
    String ns;
    String num;

    if ((cx = s.indexOf(":", 0)) > 0) {
        _name = s.substring(0,cx);
        bx = cx + 1;    
    } else bx = 0;
    ix = 0; 
    lx = s.length();
    do { 
      cx = s.indexOf(',', bx);
      num =  s.substring(bx, (cx<0)?(lx):(cx));
      ia[ix] = Integer.parseInt(num.trim());
      ix += 1;
      bx = cx + 1;
    } while(ix < 4 && cx > 0);
    _x = ia[0];
    _y = ia[1];
    _w = ia[2];
    _h = ia[3];
    return;
  }
  
  public String toString()
  {
        return _name;
  }
  
}
