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 {
    // 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;
    
    private static final int MIN_SIZE = 10;
    private static final int MAX_SIZE = 100;
    
    // boundary of area drawing in
    private FramedRect drawingArea;

    // The variable to hold the last shape created
    // The Resizable2DInterface is like DrawableInterface,
    // but it also lets us change the size of the object.
    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;

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

    // Determines size of new shape.
    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");

        // Create menu for selecting colors
        colorMenu = new JComboBox();
        colorMenu.addItem("Red");
        colorMenu.addItem("Blue");
        colorMenu.addItem("Green");
        colorMenu.addItem("Yellow");
        colorMenu.addItemListener(this);        // listen for color changes
        
        // Create size slider
        sizeSlider = new JSlider(JSlider.VERTICAL, MIN_SIZE, MAX_SIZE, DEFAULT_SIZE);
        sizeSlider.addChangeListener(this);     // listen for size changes
            
        // Create Panel to hold choice menus
        JPanel menus = new JPanel();
        menus.add(figureMenu);
        menus.add(colorMenu);
        
        // Add the components to the window
        Container contentPane = getContentPane();
        
        contentPane.add(menus, BorderLayout.SOUTH);
        contentPane.add(sizeSlider, BorderLayout.WEST);
        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 drawing area, we create
     * a new shape based on the figure choice menu.  We use
     * the size of the sizeSlider's value as well.
     */
    public void onMouseClick(Location pt) {
        if (drawingArea.contains(pt)) {
            int size = sizeSlider.getValue();
            Object figureChoice = figureMenu.getSelectedItem();

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

    public void itemStateChanged(ItemEvent event) {
        if (newShape != null) {
            this.setColor();
        }
    }

    /*
     * Helper method to change color of current shape.
     */
    private void setColor() {
          
        Object colorChoice = colorMenu.getSelectedItem();
        
        if (colorChoice.equals("Red")) {
            newShape.setColor(Color.RED);
        } else if (colorChoice.equals("Blue")) {
            newShape.setColor(Color.BLUE);
        } else if (colorChoice.equals("Green")) {
            newShape.setColor(Color.GREEN);
        } else if (colorChoice.equals("Yellow")) {
            newShape.setColor(Color.YELLOW);
        }
    }
    
    
    /*
     * Change size of last object drawn.
     */
    public void stateChanged(ChangeEvent event) {
        if (newShape != null) {
            int size = sizeSlider.getValue();
            newShape.setSize(size, size);  
        }
    }
    
    /*
     * 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;
    } 
}
