import java.awt.*;
import WizCard;
import Util;
import EventFromFrame;
import EventFromProper;

public class EventInit extends WizCard
 {
  Checkbox observer_proper, observer_frame, intersect, frame_coord;
  TextField name;
  Choice colorChoice;

  Universe u;
  
  public EventInit(Wizard w, Universe u)
   {
    super(w);
    this.u = u;


    if (u.translation_freedom)
     {
      setLayout(new GridBagLayout());

      Util.add(this,
        new Label("There is currently no preferred location in this universe."),
        Util.ENDL); 

      Util.add(this,
        new Label("Thus, there is no need to specify the location of this event."),
        Util.ENDL); 

      Util.add(this,
        new Label("What would you like to call the event?"),
        Util.DEFAULT);

      Util.add(this,
        name = new TextField("Event " + Integer.toString(u.events.size()),40),
        Util.ENDL);

      Util.add(this,
        new Label("What color would you like to give the event?"),
        Util.DEFAULT);

      Util.add(this,
        colorChoice = Util.colorChoice(),
        Util.ENDL);
     }
    else if (u.lorentz_freedom)
     {
      setLayout(new GridBagLayout());

      Util.add(this,
        new Label("There is currently no preferred frame in this universe."),
        Util.ENDL); 

      Util.add(this,
        new Label("I'll therefore place your event at the origin."),
        Util.ENDL);

      Util.add(this,
        new Label("If you do not want this, press `Back' and create a reference frame."),
        Util.ENDL);

      Util.add(this,
        new Label("What would you like to call the event?"),
        Util.DEFAULT);

      Util.add(this,
        name = new TextField("Event " + Integer.toString(u.events.size())),
        Util.ENDL);

      Util.add(this,
        new Label("What color would you like to give the event?"),
        Util.DEFAULT);

      Util.add(this,
        colorChoice = Util.colorChoice(),
        Util.ENDL);
     }
    else
     {
      setLayout(new GridLayout(0,1));
      add(new Label("How will you specify the location of this event in spacetime?"));

      CheckboxGroup cg = new CheckboxGroup();
 
      add(observer_proper = new Checkbox("As the location of an observer at a certain proper time",cg,true));
      add(observer_frame = new Checkbox("As the location of an observer at a certain frame time",cg,false));
      add(intersect = new Checkbox("At the location where two objects intersect",cg,false));
      add(frame_coord = new Checkbox("Using the spacetime co-ordinates of a reference frame.",cg,false));
     }
   }

  public void doNext()
   {
    if (u.translation_freedom || u.lorentz_freedom)
     {
      SpacetimeEvent e = new SpacetimeEvent(0,0);
      e.name = name.getText();
      e.color = Util.colorOf(colorChoice);
      u.addEvent(e);
     }
    else 
     {
      if (frame_coord.getState())
       {
        if (u.frames.size() == 0) 
         {
          System.out.println("Error: No frames have yet been defined.");
          frame_coord.setState(false);
          return;
         }
        w.addCard(new EventFromFrame(w,u));
        return;
       }

      if (observer_proper.getState())
       {
        if (u.getNumObjects() == 0)
         {
          System.out.println("Error: No physical objects have yet been defined.");
          observer_proper.setState(false);
          return;
         }
        w.addCard(new EventFromProper(w,u));
        return;
       }

      if (intersect.getState())
       {
        if (u.getNumObjects() + u.rays.size() < 2)
         {
          System.out.println("Error: not enough objects and light rays.");
          intersect.setState(false);
          return;
         }
        // ???
       }

      if (observer_frame.getState())
       {
        if (u.frames.size() == 0) 
         {
          System.out.println("Error: No frames have yet been defined.");
          observer_frame.setState(false);
          return;
         }
        if (u.getNumObjects() == 0)
         {
          System.out.println("Error: No physical objects have yet been defined.");
          observer_frame.setState(false);
          return;
         }
//        ???
       }
     }
   }
 }
