import java.awt.*;

import objectdraw.DrawableInterface;
import objectdraw.FilledRect;
import objectdraw.FramedOval;
import objectdraw.FramedRect;
import objectdraw.Location;
import objectdraw.WindowController;

import javax.swing.*;
import java.awt.event.*;


public class DrawingProgram extends WindowController
                            implements ItemListener, ActionListener {
    // 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;

    // Initial Location of shapes to be drawn
    private Location defaultLocation;

    // Buttons determining which kind of object is drawn
    private JButton framedSquareButton, framedCircleButton, filledSquareButton;

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

    // Current color on color menu
    private Color currentColor;

    // 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 buttons for selecting figures
        framedSquareButton = new JButton("FramedSquare");
        framedCircleButton = new JButton("FramedCircle");
        filledSquareButton = new JButton("FilledSquare");

        framedSquareButton.addActionListener(this);
        framedCircleButton.addActionListener(this);
        filledSquareButton.addActionListener(this);

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

        colorMenu.addItemListener(this);
        
        // Create panel for buttons
        JPanel buttonPanel = new JPanel();
        buttonPanel.add(framedSquareButton);
        buttonPanel.add(framedCircleButton);
        buttonPanel.add(filledSquareButton);
        
        // Create panel for buttonPanel and choice menu
        JPanel bottomPanel = new JPanel();
        bottomPanel.setLayout(new GridLayout(2,1));
        bottomPanel.add(buttonPanel);
        bottomPanel.add(colorMenu);
 
        Container contentPane = getContentPane();
        contentPane.add(bottomPanel, BorderLayout.SOUTH);
        contentPane.validate();

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

        defaultLocation =
            new Location(
                (canvas.getWidth() - DEFAULT_SIZE) / 2,
                (canvas.getHeight() - DEFAULT_SIZE) / 2);
    }

    /* 
     * 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 actionPerformed(ActionEvent event) {
        if (event.getSource() == framedSquareButton) {
            newShape =
                new FramedRect(defaultLocation, DEFAULT_SIZE, DEFAULT_SIZE, canvas);
        } else if (event.getSource() == framedCircleButton) {
            newShape =
                new FramedOval(defaultLocation, DEFAULT_SIZE, DEFAULT_SIZE, canvas);
        } else if (event.getSource() == filledSquareButton) {
            newShape =
                new FilledRect(defaultLocation, DEFAULT_SIZE, DEFAULT_SIZE, canvas);
        }
        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);
        }
    }
    


    /**
     * Select a new color for the last object drawn
     */
    public void itemStateChanged(ItemEvent event) {
        if (newShape != null) {
            this.setColor();
        }
    }

    /* 
     * 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 loc) {
        if (dragging) {
            newShape.move(
                loc.getX() - lastMouse.getX(),
                loc.getY() - lastMouse.getY());
            lastMouse = loc;
        }
    }

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

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

}
