import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.applet.Applet;

public class CardStackRunner extends Applet implements ActionListener
{
    JLabel pic;

    JLabel toStringLbl, sizeLbl;

    Card c = new Card ();
    CardStack s = new CardStack ();

    Color backgroundColour = new Color (145, 46, 46);
    Color buttonColour = new Color (182, 58, 58);
    Color buttonText = Color.white;
    Color titleColour = new Color (255, 255, 255);
    Font titleFont = new Font ("Arial", Font.PLAIN, 30);
    Font promptFont = new Font ("Arial", Font.PLAIN, 20);
    Dimension boardSquare = new Dimension (140, 190);

    public void init ()
    {
	s.shuffle ();
	c = s.peek ();

	setBackground (backgroundColour);
	resize (350, 580);
	JLabel title = new JLabel ("Run Your Card Stack");
	title.setFont (titleFont);
	title.setForeground (titleColour);

	pic = new JLabel (createImageIcon (c.getPicName ()));
	pic.setBackground (backgroundColour);
	pic.setPreferredSize (boardSquare);

	toStringLbl = new JLabel ("Top: " + c.getPicName ());
	toStringLbl.setPreferredSize (new Dimension (350, 20));
	toStringLbl.setHorizontalAlignment (0);
	toStringLbl.setForeground (titleColour);

	sizeLbl = new JLabel ("Stack Size: " + s.size ());
	sizeLbl.setPreferredSize (new Dimension (350, 20));
	sizeLbl.setHorizontalAlignment (0);
	sizeLbl.setForeground (titleColour);

	//Create the buttons
	JButton pop = new JButton ("Pop");
	pop.setForeground (buttonText);
	pop.setFont (titleFont);
	pop.setBackground (buttonColour);
	pop.setPreferredSize (new Dimension (200, 40));
	pop.setActionCommand ("pop");
	pop.addActionListener (this);

	JButton peek = new JButton ("Peek");
	peek.setForeground (buttonText);
	peek.setFont (titleFont);
	peek.setBackground (buttonColour);
	peek.setPreferredSize (new Dimension (200, 40));
	peek.setActionCommand ("peek");
	peek.addActionListener (this);

	JButton size = new JButton ("Size");
	size.setForeground (buttonText);
	size.setFont (titleFont);
	size.setBackground (buttonColour);
	size.setPreferredSize (new Dimension (200, 40));
	size.setActionCommand ("size");
	size.addActionListener (this);

	JButton isEmpty = new JButton ("isEmpty");
	isEmpty.setForeground (buttonText);
	isEmpty.setFont (titleFont);
	isEmpty.setBackground (buttonColour);
	isEmpty.setPreferredSize (new Dimension (200, 40));
	isEmpty.setActionCommand ("isEmpty");
	isEmpty.addActionListener (this);

	JButton isFull = new JButton ("isFull");
	isFull.setForeground (buttonText);
	isFull.setFont (titleFont);
	isFull.setBackground (buttonColour);
	isFull.setPreferredSize (new Dimension (200, 40));
	isFull.setActionCommand ("isFull");
	isFull.addActionListener (this);

	JButton shuffle = new JButton ("Shuffle");
	shuffle.setForeground (buttonText);
	shuffle.setFont (titleFont);
	shuffle.setBackground (buttonColour);
	shuffle.setPreferredSize (new Dimension (200, 40));
	shuffle.setActionCommand ("shuffle");
	shuffle.addActionListener (this);

	JLabel spacer = new JLabel ();
	spacer.setPreferredSize (new Dimension (350, 1));

	add (title);
	add (pic);
	add (spacer);
	add (toStringLbl);
	add (sizeLbl);
	add (pop);
	add (peek);
	add (size);
	add (isEmpty);
	add (isFull);
	add (shuffle);
    }


    public void actionPerformed (ActionEvent e)
    {
	if (e.getActionCommand ().equals ("pop"))
	{
	    if (s.size () >= 2)
	    {
	       //fill in
	    }
	    else if (s.size () == 1)
	    {
		s.pop ();
		pic.setIcon (createImageIcon ("cardBack_blue2"));
		toStringLbl.setText ("Top: none");
		sizeLbl.setText ("Stack Size: 0");
	    }
	    else
	    {
		JOptionPane.showMessageDialog (null,
			"Cannot Pop. Stack is Empty.",
			"Error ",
			JOptionPane.ERROR_MESSAGE);
	    }
	}
	else if (e.getActionCommand ().equals ("peek"))
	{
	    //fill in
	}
	else if (e.getActionCommand ().equals ("size"))
	{
	    //fill in
	}
	else if (e.getActionCommand ().equals ("isEmpty"))
	{
	    //fill in

	}
	else if (e.getActionCommand ().equals ("isFull"))
	{
	    //fill in
	}
	else if (e.getActionCommand ().equals ("shuffle"))
	{
	    //fill in
	}
    }


    protected static ImageIcon createImageIcon (String path)
    {
	java.net.URL imgURL = CardStackRunner.class.getResource (path);
	if (imgURL != null)
	{
	    return new ImageIcon (imgURL);
	}
	else
	{
	    System.err.println ("Couldn't find file: " + path);
	    return null;
	}
    }
}


