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

// Allows a user to stack (and un-stack) wood.
public class RecWoodPile extends WindowController implements ActionListener {

        // dimensions of wood to be stacked
        private static final int WOOD_WIDTH = 100;
        private static final int WOOD_HT = 10;

        // the stacking displacement
        private static final int STACKING_DISP = 26;

        // the stack of wood
        private StackInterface woodStack;

        // buttons that allow a user to select a mode (stacking or unstacking)
        private JButton stackButton, unStackButton;

        // the x and y locations of the next item of wood to be stacked
        private static final int WOOD_X = 100;
        private int y = 340;

        // an image of wood
        private Image woodImage;

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

                stackButton = new JButton("Stack 'em");
                unStackButton = new JButton("Unstack 'em");

                JPanel buttonPanel = new JPanel();
                buttonPanel.add(stackButton);
                buttonPanel.add(unStackButton);

                getContentPane().add(buttonPanel, BorderLayout.SOUTH);

                // add listeners
                stackButton.addActionListener(this);
                unStackButton.addActionListener(this);

                woodImage = getImage("wood5.jpg");

                // the stack is initially empty
                woodStack = new EmptyStack();
        }

        public void actionPerformed(ActionEvent e) {
                if (e.getSource() == stackButton) {
                        // put a new piece of wood on the stack
                        VisibleImage wood = new VisibleImage(woodImage, 
                                                                                                 WOOD_X, y, canvas);
                        woodStack = new NonEmptyStack(wood, woodStack);
                        y = y - STACKING_DISP;
                } else {
                        // only remove a piece of wood if stack is not empty
                        if (!woodStack.isEmpty()) {
                                VisibleImage wood = woodStack.getFirst();
                                wood.removeFromCanvas();
                                woodStack = woodStack.getRest();
                                y = y + STACKING_DISP;
                        }
                }
        }
}
