import java.awt.Color;
import java.awt.Graphics;

public class Window {
    /** The length of the resizing triangle's non-hypotenuse edges. */
    private static final int RESIZE_TRIANGLE_SIZE = 10;

    /** The name of this window. */
    private String name;

    /** The color of this window. */
    private Color color;

    /** The x-coordinate of this window's left edge. */
    private int x;

    /** The y-coordinate of this window's top edge. */
    private int y;

    /** The width of this window, in pixels. */
    private int width;

    /** The height of this window, in pixels. */
    private int height;

    /** The window below this one, or null if this is bottom window */
    private Window below;

    /** The window above this one, or null if this is top window */
    private Window above;

    /** Constructs a window with the given name, color,
     * position, and size. */
    public Window(String inName, Color inColor, int inX, int inY,
            int inWidth, int inHeight) {
        name = inName;
        color = inColor;
        x = inX;
        y = inY;
        width = inWidth;
        height = inHeight;
    }

    /** Returns the x-coordinate of this window's left edge. */
    public int getX() {
        return x;
    }

    /** Returns the y-coordinate of this window's top edge. */
    public int getY() {
        return y;
    }

    /** Returns the width of this window, in pixels. */
    public int getWidth() {
        return width;
    }

    /** Returns the height of this window, in pixels. */
    public int getHeight() {
        return height;
    }

    /** Returns true if the requested point lies within this window. */
    public boolean contains(int qx, int qy) {
        return x <= qx && qx < x + width && y <= qy && qy < y + height;
    }

    /** Paints this window using g, highlighting it as the selected window. */
    public void paint(Graphics g, boolean selected) {
        g.setColor(color);
        g.fillRect(x, y, width, height);
        g.setColor(Color.BLACK);
        g.drawRect(x, y, width, height);
        if(selected) {
            g.drawRect(x + 1, y + 1, width - 2, height - 2);
        }

        int resizeSize = Math.min(RESIZE_TRIANGLE_SIZE,
                Math.min(width, height));
        g.drawLine(x + width, y + height - resizeSize,
                x + width - resizeSize, y + height);
    }

    /** Returns a string representation of this window. */
    public String toString() {
        return name + "[" + x + "," + y + "/"
            + width + "x" + height + "]";
    }

    /** Changes the location of this window as specified. */
    public void setLocation(int inX, int inY) {
        x = inX;
        y = inY;
    }

    /** Changes the size of this window as specified. */
    public void setSize(int inWidth, int inHeight) {
        width = inWidth;
        height = inHeight;
    }

    /** Returns true if the query point lies within this
     * window's resize region. The method assumes that the query
     * point lies within the overall window. */
    public boolean isInResizeRegion(int queryX, int queryY) {
        int resizeSize = Math.min(RESIZE_TRIANGLE_SIZE,
                Math.min(width, height));
        return queryX + queryY >= x + width + y + height - resizeSize;
    }

    /** Returns the window just below this one, or null if this is the bottom
     * window. */
    public Window getWindowBelow() {
        return below;
    }

    /** Returns the window just above this one, or null if this is the top
     * window. */
    public Window getWindowAbove() {
        return above;
    }

    /** Updates the window remembered as the one just below this window. */
    public void setWindowBelow(Window value) {
        below = value;
    }

    /** Updates the window remembered as the one just above this window. */
    public void setWindowAbove(Window value) {
        above = value;
    }

}
