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

/**
 * Draw and drag around a bullseye to demonstrate 
 * recursive drawing.
 */
public class BullsEyeController extends WindowController implements ActionListener {

    private JSlider sizeSlider;
    
    private static final int INIT_SIZE = 32;
    private static final int MIN_SIZE = 24;
    private static final int MAX_SIZE = 1024;
    
    private BullsEyeInterface eye;
    
    private JLabel count;
    
    private boolean dragging = false;
    private Location lastPt;
    
    public void begin() {
        sizeSlider = new JSlider(JSlider.HORIZONTAL, MIN_SIZE, MAX_SIZE, INIT_SIZE);
        sizeSlider.setMajorTickSpacing(BullsEyeInterface.RING_GAP);
        sizeSlider.setSnapToTicks(true);
        JPanel panel = new JPanel(new GridLayout(2,1));
        JPanel countPanel = new JPanel();
        JButton rings = new JButton("Ring Count:");
        rings.addActionListener(this);
        count = new JLabel("None", JLabel.LEFT);
        countPanel.add(rings);
        countPanel.add(count);
        panel.add(sizeSlider);
        panel.add(countPanel);
        Container contentPane = getContentPane();
        contentPane.add(panel, BorderLayout.SOUTH);
        contentPane.validate();
    }
    
    public void actionPerformed(ActionEvent e) {
        if (eye != null) {
            count.setText("" + (eye.numRings()));
        }
    }
    
    
    public void onMouseClick(Location pt) {
        double radius = sizeSlider.getValue();
        eye = new BullsEyeRing(pt,radius,canvas);
    }
    
    
    
    public void onMousePress(Location pt) {
        if (eye != null) {
            dragging = eye.contains(pt);
            lastPt = pt;
        } else {
            dragging = false;
        }
        
    }
    
    public void onMouseDrag(Location pt){
        if (dragging) {
            eye.move(pt.getX() - lastPt.getX(),
                     pt.getY() - lastPt.getY());
            lastPt = pt;
        }
    }
    
}
