import objectdraw.*;

// import the Swing libraries
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class DrawingProgram extends WindowController implements ItemListener {
    // Width of border around drawing area
    private static final int BORDER_X = 10;
    private static final int BORDER_Y = 10;

    // Default size when we create a new shape
    private static final int DEFAULT_SIZE = 50;

    // The menu determining which kind of object is drawn
    private JComboBox figureMenu;

    // The menu determining which color the object should be
    private JComboBox colorMenu;

    // boundary of area drawing in
    private FramedRect drawingArea;

    // The variable to hold the last shape created
    private DrawableInterface newShape;

    // Used to support the usual dragging algorithm
    private boolean dragging;
    private Location lastMouse;

    /*
     * Draws the program with the buttons and a blank drawing area
     */
    public void begin() {
        // Create menu for selecting figures
        figureMenu = new JComboBox();
        figureMenu.addItem("FramedSquare");
        figureMenu.addItem("FramedCircle");
        figureMenu.addItem("FilledSquare");

        // create menu for selecting colors
        colorMenu = new JComboBox();
        colorMenu.addItem("Red");
        colorMenu.addItem("Blue");
        colorMenu.addItem("Green");
        colorMenu.addItem("Yellow");

        colorMenu.addItemListener(this);

        Container contentPane = getContentPane();
        contentPane.add(figureMenu, BorderLayout.SOUTH);
        contentPane.add(colorMenu, BorderLayout.NORTH);
        contentPane.validate();

        // Draw the drawing area
        drawingArea = new FramedRect(BORDER_X, BORDER_Y,
                                     canvas.getWidth() - (2 * BORDER_X),
                                     canvas.getHeight() - (2 * BORDER_Y), canvas);
    }

    /*
     * When the user clicks the button on a shape button, create that shape.
     * When the user clicks on a color button, change the button of the last
     * shape created.  If the drawing area is empty, clicking on a color button
     * does nothing.
     */
    public void onMouseClick(Location pt) {
        // Handle shape buttons
        if (drawingArea.contains(pt)) {
            Object figureChoice = figureMenu.getSelectedItem();

            if (figureChoice.equals("FramedSquare")) {
                newShape = new FramedRect(pt, DEFAULT_SIZE, DEFAULT_SIZE, canvas);
            } else if (figureChoice.equals("FramedCircle")) {
                newShape = new FramedOval(pt, DEFAULT_SIZE, DEFAULT_SIZE, canvas);
            } else if (figureChoice.equals("FilledSquare")) {
                newShape = new FilledRect(pt, DEFAULT_SIZE, DEFAULT_SIZE, canvas);
            }
        }
    }

    /**
     * Select a new color for the last object drawn
     */
    public void itemStateChanged(ItemEvent event) {
        if (newShape != null) {
            Object colorChoiceString = colorMenu.getSelectedItem();

            if (colorChoiceString.equals("Red")) {
                newShape.setColor(Color.RED);
            } else if (colorChoiceString.equals("Blue")) {
                newShape.setColor(Color.BLUE);
            } else if (colorChoiceString.equals("Green")) {
                newShape.setColor(Color.GREEN);
            } else if (colorChoiceString.equals("Yellow")) {
                newShape.setColor(Color.YELLOW);
            }
        }
    }

    /*
     * Drag the shape with the mouse.  Notice that it doesn't matter what
     * kind of shape (FramedRect, FilledOval, etc.) we have.
     */
    public void onMouseDrag(Location pt) {
        if (dragging) {
            newShape.move(pt.getX() - lastMouse.getX(),
                          pt.getY() - lastMouse.getY());
            lastMouse = pt;
        }
    }

    /*
     * Start a drag
     */
    public void onMousePress(Location pt) {
        if ((newShape != null) && newShape.contains(pt)) {
            dragging = true;
            lastMouse = pt;
        }
    }

    /*
     * Stop a drag
     */
    public void onMouseRelease(Location pt) {
        dragging = false;
    }
}
