import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.*;


/**
  * the class that forms structure so that words can be displayed
  *
  * @author Aleksey Babeyev,Jim Congdon,Eric Hwuang,Corey Miller, & Jack Wen
  * @see TextPanel
  * @see PaintPanel
  * @see Controller
  */
public class HarpoonGUI extends Frame implements Runnable
{
  HarpoonGUI(Controller con)
  {/**
    * initializer for class, builds gui
    */
    myCon = con;

    setLayout(new BorderLayout());
    setSize(BASEWIDTH,BASEHEIGHT);
    myDim = this.getSize();

    myStatus = new List(1);
    myCurrentPanel = new TextPanel();
 
    myButtons = new Panel();
	
    myCurrentPanel.setSize(myDim.width, (int) (myDim.height*.80));
    myButtons.setSize(myDim.width,  (int) (myDim.height*.20));

    this.add(BorderLayout.NORTH,myButtons);
    this.add(BorderLayout.CENTER,myCurrentPanel);
    this.add(BorderLayout.SOUTH,myStatus);

    MenuBar bar = new MenuBar();
    
    Menu fileMenu = new Menu("File");
    bar.add(fileMenu);
    setMenuBar(bar);

    // clear menu item bound to clear the textfield and the listbox
    MenuItem clearMI = new MenuItem("Clear");
    clearMI.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent ev)
      {
	StatusUpdate("");
      }
    });
    fileMenu.add(clearMI);

    // quit menu item bound to anonymous quit action listener
    MenuItem quitMI = new MenuItem("Quit");
    quitMI.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent ev)
      {
        System.exit(0);
      }
    });
    fileMenu.add(quitMI);


    Button robotB = new Button("Robot");
    robotB.addActionListener(
      new ActionListener()
      {
	public void actionPerformed(ActionEvent e)
	{
	  if (myAnimator != null)
	  {
	    // do something
	  }
	}
      });
    myButtons.add(robotB);

    myChoice = new Choice();
    myButtons.add(myChoice);
    
    myPauser = new Button("Pause");
    myPauser.addActionListener(
      new ActionListener()
      {
	public void actionPerformed(ActionEvent e)
	{
	  if (myAnimator != null)
	  {
	    myPauser.setLabel("Start");
	    myAnimator = null;
	  }
	  else
	  {
	    myPauser.setLabel("Pause");
	    start();
	  }
	}
      });
    myButtons.add(myPauser);

    Button quitB = new Button("Quit");
    quitB.addActionListener(
      new ActionListener()
      {
	public void actionPerformed(ActionEvent e)
	{
	  System.exit(1);
	}
      });
    myButtons.add(quitB);

    pack();
    //setSize(BASEWIDTH,BASEHEIGHT);
    show();
    myAnimator = new Thread(this);
    myAnimator.start();
    setTitle("Loading Bay 1.0");
  }

  public void start()
  {/**
    *  gets gui in motion
    */
    myAnimator = new Thread(this);
    myAnimator.start();
  }

  Image loadImage(String imageName)
  {/**
    * access image from files and creates image object
    */
    MediaTracker tracker = new MediaTracker(this);
    Image im = getToolkit().getImage("images/"+imageName);
    tracker.addImage(im,0);
    
    // wait for image to load
    
    try   { tracker.waitForID(0); }
    catch (InterruptedException e)
    {
      //     displayStatus("image loading interrupted");
      return null;
    }
    return im;
  }

  public Container GetContainer()
  {/**
    * returns the container used to draw the graphics
    */
    return myCurrentPanel;
  }

  public void StatusUpdate(String s)
  {/**
    * changes update bar text
    */
    myStatus.removeAll();
    myStatus.add(s);
  }

  public void addComponent(Component com)
  {/**
    * adds image component to displayed graphic
    */
    myCurrentPanel.add(com);
  }

  public void addComponent(Component com,int index)
  {/**
    * specifies the priority with which the item gets painted
    */
    myCurrentPanel.add(com,index);
  }

  public int GetComIndex(Component com)
  {/**
    * returns index of component from within the current Panel
    */
    return myCurrentPanel.getCompIndex(com);
  }

  public void run()
  {/**
    * thread process
    */
    
    while (Thread.currentThread() == myAnimator)
    {
      //System.out.println("GUI");
      myCurrentPanel.paint(myCurrentPanel.getGraphics());
      try
      {
	Thread.sleep(50);
      }
      catch (InterruptedException e){
      }
    }
  }

  public void paint(Graphics g)
  {/**
    * paints the graphic to screen
    */
    super.paint(g);
  }
    
  public void update(Graphics g)
  {/**
    * calls the paint process
    */
    paint(g);   // no clear/erase, just paint
  }

  private Controller myCon;
  private Thread    myAnimator;
  private Dimension myDim;
  private List myStatus;
  private PopupMenu myPopup;
  private Panel     myButtons;
  private Vector myPanels;
  private TextPanel     myCurrentPanel;
  private Choice    myChoice;
  private Button    myPauser;

  private static final int BASEWIDTH = 800;
  private static final int BASEHEIGHT = 700;

}
