import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import objectdraw.*;

// A very simple drawing program.
public class ScribbleController extends WindowController implements ActionListener, ItemListener {

        // user modes
        private static final int DRAWING = 1;
        private static final int MOVING = 2;

        // the current scribble
        private ScribbleInterface currentScribble;

        // stores last point for drawing or dragging
        private Location lastPoint;

        // whether the most recent scribble has been selected for moving
        private boolean draggingScribble;

        // buttons/boxes that allow user to select modes
        private JButton setDraw, setMove;
        private JComboBox setColor;

        // the current mode -- drawing by default
        private int chosenAction;

        // create and hook up the user interface components
        public void begin() {

                setDraw = new JButton("Draw");
                setMove = new JButton("Move");
                setColor = new JComboBox();
                setColor.addItem("Red");
                setColor.addItem("Blue");
                setColor.addItem("Green");
                setColor.addItem("Yellow");

                JPanel buttonPanel = new JPanel();
                buttonPanel.add(setDraw);
                buttonPanel.add(setMove);
                buttonPanel.add(setColor);

                Container contentPane = getContentPane();
                contentPane.add(buttonPanel, BorderLayout.SOUTH);
                contentPane.validate();

                // add listeners
                setDraw.addActionListener(this);
                setMove.addActionListener(this);
                setColor.addItemListener(this);

                chosenAction = DRAWING;
        }

        // if in drawing mode then start with empty scribble
        // if in moving mode then prepare to move
        public void onMousePress(Location point) {
                if (chosenAction == DRAWING) {
                        // start with an empty scribble for drawing
                        currentScribble = new EmptyScribble();

                } else if (chosenAction == MOVING) {
                        // check if user clicked on current scribble
                        draggingScribble = currentScribble.contains(point);
                }

                // remember point of press for drawing or moving
                lastPoint = point;
        }

        // if in drawing mode, add a new segment to scribble
        // if in moving mode then move it
        public void onMouseDrag(Location point) {
                if (chosenAction == DRAWING) {
                        // add new line segment to current scribble
                        Line newSegment = new Line(lastPoint, point, canvas);

                        currentScribble = new NonEmptyScribble(newSegment, currentScribble);
                } else if (chosenAction == MOVING && draggingScribble) {
                        // drag current scribble
                        currentScribble.move(point.getX() - lastPoint.getX(), point.getY() - lastPoint.getY());
                }
                // update for next move or draw
                lastPoint = point;
        }
        
        public void onMouseRelease(Location point) {
                if (chosenAction == DRAWING) {
                        setColor();
                }
        }

        // Set mode according to button pressed.
        public void actionPerformed(ActionEvent e) {
                if (e.getSource() == setDraw) {
                        chosenAction = DRAWING;
                } else if (e.getSource() == setMove) {
                        chosenAction = MOVING;
                }
        }


        private void setColor() {
                Object colorChoiceString = setColor.getSelectedItem();
                if (colorChoiceString.equals("Red")) {
                        currentScribble.setColor(Color.red);
                } else if (colorChoiceString.equals("Blue")) {
                        currentScribble.setColor(Color.blue);
                } else if (colorChoiceString.equals("Green")) {
                        currentScribble.setColor(Color.green);
                } else if (colorChoiceString.equals("Yellow")) {
                        currentScribble.setColor(Color.yellow);
                }
        }
        

        public void itemStateChanged(ItemEvent arg0) {
                if (currentScribble != null) {
                        setColor();
                }
        }
}
