import objectdraw.*;

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


public class DrawingProgram extends WindowController implements ItemListener, ChangeListener {

    // Size and locations of buttons in the palette used to create shapes
    private static final int BUTTON_HEIGHT = 40;
    private static final int BUTTON_WIDTH = 40;

    // Location and shape of the drawing area
    private static final int AREA_X = BUTTON_WIDTH;
    private static final int AREA_Y = 0;
    private static final int AREA_WIDTH = 300;
    private static final int AREA_HEIGHT = 300;

    // Location and shape of the buttons used to change colors
    private static final int COLORS_X = AREA_X + AREA_WIDTH + 2;
    private static final int COLORS_Y = 0;
    private static final int COLOR_WIDTH = BUTTON_WIDTH;
    private static final int COLOR_HEIGHT = BUTTON_HEIGHT;

    // Size parameters
    private static final int MIN_SIZE = 10;
    private static final int MAX_SIZE = 100;
    private static final int INIT_SIZE = 20;

    // boundary of area drawing in
    private FramedRect drawingArea;

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

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

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

    // Size of newly created objects
    private JSlider sizeSlider;

    /*
     * 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");

        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);

        // Create and add the size slider
        sizeSlider = new JSlider(JSlider.VERTICAL, MIN_SIZE, MAX_SIZE, INIT_SIZE);
        sizeSlider.addChangeListener(this);

        contentPane.add(sizeSlider, BorderLayout.WEST);

        contentPane.validate();

        // Draw the drawing area
        drawingArea = new FramedRect(AREA_X, AREA_Y, AREA_WIDTH, AREA_HEIGHT, canvas);

        // Create color palette
    }

    /*
     * 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)) {
            int size = sizeSlider.getValue();
            Object choice = figureMenu.getSelectedItem();

            if (choice.equals("FramedSquare")) {
                newShape = new FramedRect(pt, size, size, canvas);
            } else if (choice.equals("FramedCircle")) {
                newShape = new FramedOval(pt, size, size, canvas);
            } else if (choice.equals("FilledSquare")) {
                newShape = new FilledRect(pt, size, size, canvas);
            }
        } else if (newShape != null) {
            // Handle color buttons
/*            if (redRect.contains(pt)) {
                newShape.setColor(Color.red);
            } else if (blueRect.contains(pt)) {
                newShape.setColor(Color.blue);
            } else if (greenRect.contains(pt)) {
                newShape.setColor(Color.green);
            } else if (yellowRect.contains(pt)) {
                newShape.setColor(Color.yellow);
            }
            */
        }
    }
    
    public void stateChanged(ChangeEvent evt) {
        newShape.setWidth(sizeSlider.getValue());    
        newShape.setHeight(sizeSlider.getValue());            
    }
    
    /**
     * 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;
    }
}
