import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 * Program to do recursive drawings.
 * 
 * @author David Matuszek
 * @author TODO Put your name here
 * @version 0.0
 */
public class RecursiveDrawings extends JFrame implements  ActionListener {
    private JPanel drawingPanel = new JPanel();
    private JPanel controlPanel = new JPanel();
    private JRadioButton[] depthButtons = new JRadioButton[6];
    private ButtonGroup group = new ButtonGroup();
    private JButton[] drawingButtons = new JButton[6];

    private int depth;         // maximum depth of the recursion
    private int drawingNumber; // which drawing to make

    /**
     * Main method for this application.
     * @param args Unused.
     */
    public static void main(String[] args) {
        new RecursiveDrawings().run();
    }

    /**
     * Runs this RecursiveDrawings application.
     */
    public void run() {
        createWidgets();
        layOutGui();
        setSize(400, 500);
        attachListeners();
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    /**
     * Creates all the components needed by the application.
     */
    private void createWidgets() {
        for (int i = 0; i < 6; i++) {
            depthButtons[i] = new JRadioButton(" " + (i + 1));
            group.add(depthButtons[i]);
        }
        depthButtons[0].setSelected(true);
        for (int i = 0; i < 6; i++) {
            drawingButtons[i] = new JButton("Drawing " + (i + 1));
        }
        drawingButtons[0].setText("Squares");
    }

    /**
     * Arranges the components for this application.
     */
    private void layOutGui() {
        add(drawingPanel, BorderLayout.CENTER);
        add(controlPanel, BorderLayout.SOUTH);
        controlPanel.setLayout(new GridLayout(3, 4));
        for (int i = 0; i < 3; i++) {
            controlPanel.add(depthButtons[i]);
            controlPanel.add(depthButtons[i + 3]);
            controlPanel.add(drawingButtons[i]);
            controlPanel.add(drawingButtons[i + 3]);
        }
        setSize(400, 300);
    }

    /**
     * Attaches this RecursiveDrawings object as a listener for
     * all the drawing buttons. The depth radio buttons don't
     * get listeners.
     */
    private void attachListeners() {
        for (int i = 0; i < 6; i++) {
            drawingButtons[i].addActionListener(this);
        }
    }

    /**
     * Responds to a button press by setting the global variables
     * <code>depth</code> and <code>drawingNumber</code>, then
     * requesting that the painting be done.
     *  
     * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    public void actionPerformed(ActionEvent e) {
        for (int i = 0; i < 6; i++) {
            if (depthButtons[i].isSelected()) depth = i + 1;
            if (e.getSource() == drawingButtons[i]) drawingNumber =  i + 1;
        }
        repaint();
    }
    
    /**
     * Paints one of the drawings, based on the global variables
     * <code>depth</code> and <code>drawingNumber</code>.
     * 
     * @see java.awt.Container#paint(java.awt.Graphics)
     */
    public void paint(Graphics g) {
        super.paint(g);
        int width = drawingPanel.getWidth();
        int height = drawingPanel.getHeight();

        // clear panel
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, width, height);
        g.setColor(Color.BLACK);

        g.setClip(0, 0, width, height);
        int x = drawingPanel.getWidth() / 2;
        int y = drawingPanel.getHeight() / 2 + 10;
        int length = Math.min(x, y - 20);
        
        if (drawingNumber == 1) squares(g, x, y,  length, depth);
//        else if (drawingNumber == 2) drawYourPattern2(g,  yourArguments);
//        else if (drawingNumber == 3) drawYourPattern3(g,  yourArguments);
//        else if (drawingNumber == 4) drawYourPattern4(g,  yourArguments);
//        else if (drawingNumber == 5) drawYourPattern5(g,  yourArguments);
//        else if (drawingNumber == 6) drawYourPattern6(g,  yourArguments);
    }

    private void squares(Graphics g, int x, int y, int length, int  depth) {
        // TODO Supply this method
        g. fillRect(x - length / 2, y - length / 2, length, length); // junk this code
    }
}

