import objectdraw.*;

import java.awt.*;

import javax.swing.*;


/**
 * Drawing Program with a Collection of Shapes.
 */
public class DrawingProgram extends WindowController {
    private static final int SIZE = 100;

    // menus for shape, color, and command
    private JComboBox shapeChoice;
    private JComboBox colorChoice;
    private JComboBox commandChoice;

    // the item that is currently selected
    private DrawableInterface selected;
    
    // the last location of the mouse
    private Location lastPoint;
    
    private ShapeCollection shapes = new ShapeCollection();
    
    // Set up GUI components for program
    public void begin() {
        // create panel to hold choice buttons
        JPanel menuPanel = new JPanel();

        // menu for selecting or adding
        commandChoice = new JComboBox();
        commandChoice.addItem("Add new item");
        commandChoice.addItem("Recolor item");
        commandChoice.addItem("Move item");
        commandChoice.addItem("Delete item");
        commandChoice.addItem("Send to back");
        menuPanel.add(commandChoice);

        // Set up menu for shapes
        shapeChoice = new JComboBox();
        shapeChoice.addItem("Circle");
        shapeChoice.addItem("Square");
        menuPanel.add(shapeChoice);

        // Set up menu for colors
        colorChoice = new JComboBox();
        colorChoice.addItem("Red");
        colorChoice.addItem("Green");
        colorChoice.addItem("Blue");
        menuPanel.add(colorChoice);

        // Add the panel to screen
        getContentPane().add(menuPanel, BorderLayout.SOUTH);
        validate();
    }

    // When the user clicks in the canvas, check the settings of the command menu to
    // determine what action to take.
    public void onMousePress(Location point) {
        selected = null; // indicate nothing currently selected

        String buttonLabel = commandChoice.getSelectedItem().toString();

        if (buttonLabel.equals("Add new item")) {
            addNewAt(point);
        } else if (buttonLabel.equals("Recolor item")) {
            recolorShapeAt(point);
        } else if (buttonLabel.equals("Move item")) {
            selectShapeAt(point);
        } else if (buttonLabel.equals("Delete item")) {
            deleteShapeAt(point);
        } else {
            sendToBackShapeAt(point);
        }
    }

    // This method implements the "Add new item" command.
    // Add new geometric shape where clicked.  Type and color of object is determined by the
    // settings of the color and shape menus.
    private void addNewAt(Location point) {
            DrawableInterface newShape;

            // computer top-left corner of shape so it is center over mouse
            Location topLeft = new Location(point.getX() - (SIZE / 2),
                                            point.getY() - (SIZE / 2));
                    
            String shapeString = shapeChoice.getSelectedItem().toString();

            // create new object to be shape chosen
            if (shapeString.equals("Square")) {
                newShape = new FilledRect(topLeft, SIZE, SIZE, canvas);
            } else {
                newShape = new FilledOval(topLeft, SIZE, SIZE, canvas);
            }

            newShape.setColor(getSelectedColor());
            shapes.add(newShape);
        
    }

    /*
     * Change the shape and color of the item the user clicks on.
     */
    private void recolorShapeAt(Location point) {
        DrawableInterface shape = shapes.selectedShape(point);

        if (shape != null) {
            shape.setColor(getSelectedColor());
        }
    }


    // Remove top-most geometric item clicked in.  
    // If didn't click in any then don't do anything.
    private void deleteShapeAt(Location point) {
        DrawableInterface shape = shapes.selectedShape(point);

        if (shape != null) {
            shapes.remove(shape);
            shape.removeFromCanvas();
        }
    }
    
    
    // Set select to indicate top-most geometric item clicked in, and send it to front of
    // screen.  If didn't click in any then select stays null.  Remember where clicked in
    // lastPoint.
    private void selectShapeAt(Location point) {
       selected = shapes.selectedShape(point);

        if (selected != null) {
            shapes.remove(selected);
            shapes.add(selected);
            selected.sendToFront();
        }
        
        lastPoint = point;
    }

     // Remove top-most geometric item clicked in.  
    // If didn't click in any then don't do anything.
    private void sendToBackShapeAt(Location point) {
        DrawableInterface shape = shapes.selectedShape(point);

        if (shape != null) {
            shapes.remove(shape);
            shapes.addFirst(shape);
            shape.sendToBack();
        }
    }



    // If something was selected then drag it and remember where left off
    public void onMouseDrag(Location point) {
        if (selected != null) {
            selected.move(point.getX() - lastPoint.getX(),
                          point.getY() - lastPoint.getY());
            lastPoint = point;
        }
    }
    
    

    // Returns the color corresponding to the string selected in the
    // color menu.
    private Color getSelectedColor() {
        // get color showing in the color menu
        String colorString = colorChoice.getSelectedItem().toString();
        
        if (colorString.equals("Red")) {
            return Color.RED;
        } else if (colorString.equals("Green")) {
            return Color.GREEN;
        } else {
            return Color.BLUE;
        }
    }
}
