import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import WizCard;

public class Wizard extends Frame
 {
  Panel top, bottom;
  Button back,next,cancel;
  CardLayout topLayout;
  int numCards;

  Wizard()
   {
    super();
    pack();

    numCards = 0;

    setLayout(new BorderLayout());

    add(top = new Panel(), BorderLayout.CENTER);
    top.setLayout(topLayout = new CardLayout());

    add(bottom = new Panel(), BorderLayout.SOUTH);
    bottom.add(back = new Button("<<Back"));
    bottom.add(next = new Button("Next>>"));
    bottom.add(cancel = new Button("Cancel"));

    back.setForeground(Color.gray);
    cancel.setForeground(Color.gray);
    next.setForeground(Color.gray);

    back.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
       if (numCards <= 1) return;
       topLayout.previous(top);
       numCards--;
       top.remove(numCards);

       if (numCards == 1)
        {
         back.setForeground(Color.gray);
         cancel.setForeground(Color.gray);
        }
       next.setLabel("Next>>");
       bottom.validate();
      }
     });
    
    next.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
       if (numCards > 0)
         ((WizCard)top.getComponent(numCards-1)).doNext();
      }
     });

    cancel.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e)
      {
       reset();
      }
     });

    validate();
    setTitle("Scenario creator");
    setSize(600,400);
   }

   public void reset()
    {
     if (numCards <= 1) return;
     topLayout.first(top);
     for (int i = 1; i < numCards; i++)
       top.remove(1);
     numCards = 1;
 
     back.setForeground(Color.gray);
     cancel.setForeground(Color.gray);
     next.setLabel("Next>>");
     bottom.validate();
    }

   public void addCard(WizCard c)
    {
     top.add(c,Integer.toString(numCards));
     topLayout.last(top);
     numCards++;
     if (numCards > 1)
      {
       back.setForeground(Color.black);
       cancel.setForeground(Color.black);
      }
     next.setForeground(Color.black);
     if (c.isFinal)
       next.setLabel("Done");
     else
       next.setLabel("Next>>");

     validate();
    }

 }
