import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import Wizard;
import MainCard;
import Universe;
import MyCanvas;
import Util;

public class RelApp extends Applet implements ActionListener, ItemListener
 {
  Wizard w;
  Panel p;
  MyCanvas c;
  Universe u;
  GridBagLayout gridbag;
  Choice gotoChoice, frameChoice;

  void addButton(String name, GridBagConstraints gc, String action)
   {
    Button b = new Button(name);
    Util.add(p,b,gc);
    b.addActionListener(this);
    b.setActionCommand(action);
   }
 
  public void init()
   {
    setLayout(new BorderLayout());

    add(p = new Panel(), BorderLayout.EAST);
    add(c = new MyCanvas(), BorderLayout.CENTER);


    gridbag = new GridBagLayout();

    p.setLayout(gridbag);

    Util.add(p,new Label("Show frame:"),Util.DEFAULT);
    Util.add(p,frameChoice = new Choice(), Util.ENDL);
    frameChoice.addItemListener(this);

    frameChoice.add("Centered frame");
    frameChoice.add("Frame last visited");

    Util.add(p,new Label("Go to:"),Util.DEFAULT);
    Util.add(p,gotoChoice = new Choice(), Util.ENDL);
    gotoChoice.addItemListener(this);

    addButton("Boost left",Util.DEFAULT,"boost left");
    addButton("Boost right",Util.ENDL,"boost right");

    addButton("^", Util.CENTER, "up");
    addButton("<", Util.DEFAULT, "left");
    addButton(">", Util.ENDL, "right");
    addButton("v", Util.CENTER, "down");

    addButton("Zoom in", Util.DEFAULT, "zoom in");
    addButton("Zoom out", Util.ENDL, "zoom out");

    u = new Universe(c,this);

    ReferenceFrame f = new ReferenceFrame();
    f.name = "Frame 0";
    u.addFrame(f);

    SpacetimeEvent e = new SpacetimeEvent(f,2,3);
    e.name = "Red event";
    e.color = Color.red;
    u.addEvent(e);

    InertialObject o = new InertialObject(e,0.5);
    o.name = "John";
    o.color = Color.green;
    u.addInertialObject(o);
 
    e = new SpacetimeEvent(f,0,4);
    e.name = "Blue event";
    e.color = Color.blue;
    u.addEvent(e);

    w = new Wizard();
    w.setTitle("Scenario Editor");

    w.addCard(new MainCard(w, u));

    w.show();
   }

   public void actionPerformed(ActionEvent e)
   {
    String s = e.getActionCommand();
    if (s == "up") c.shift(0,c.scale / 5);
    if (s == "down") c.shift(0,-c.scale / 5);
    if (s == "left") c.shift(-c.scale / 5,0);
    if (s == "right") c.shift(c.scale / 5,0);
    if (s == "zoom in") c.zoomIn(1.25);
    if (s == "zoom out") c.zoomOut(1.25);
    if (s == "boost left") c.boost(-0.1);
    if (s == "boost right") c.boost(0.1);
   }

   public void itemStateChanged(ItemEvent e)
    {
     if (e.getSource() == gotoChoice) gotoStateChanged();
     else frameStateChanged();
    }

   void frameStateChanged()
    {
     int i = frameChoice.getSelectedIndex();
     if (i == 0)
      {
       c.showStandardFrame = true;
       c.showRecentFrame = false;
       c.redraw();
       return;
      }
     if (i == 1)
      {
       c.showStandardFrame = false;
       c.showRecentFrame = true;
       c.redraw();
       return;
      }
     c.showStandardFrame = false;
     c.showRecentFrame = false;
     c.displayed_frame = u.frameAt(i-2);
     c.redraw();
    }

   void gotoStateChanged()
    {
     int i = gotoChoice.getSelectedIndex();

     if (i < u.frames.size())
      {
       c.setStickyFrame(u.frameAt(i));
       return;
      }
     i -= u.frames.size();

     if (i < u.inertialObjects.size())
      {
       c.setStickyFrame(u.inertialAt(i).closest(c.f.origin()));
       return;
      } 
     i -= u.inertialObjects.size();

     if (i < u.variableObjects.size())
      {
       c.setStickyFrame(u.variableAt(i).closest(c.f.origin()));
       return;
      } 
     i -= u.variableObjects.size();

     if (i < u.longObjects.size())
      {
       c.setStickyFrame(u.longAt(i).closest(c.f.origin()));
       return;
      } 
     i -= u.longObjects.size();

     if (i < u.rays.size())
      {
       // ???
       return;
      }
     i -= u.rays.size();

     c.setOrigin(u.eventAt(i));
     c.recent_frame = c.f;
     c.redraw();
    }

   public void stop()
    {
     if (w != null) w.dispose();
     w = null;
    }

   public void destroy()
    {
     if (w != null) w.dispose();
     w = null;
    }

 }
