import java.awt.*;
import WizCard;
import Util;
import ObjectFromEvent;
import ObjectFromFrame;
// import ObjectFromIntersection;
// import ObjectFromObject;

public class ObjectInit extends WizCard
 {
  TextField name, mass,length;
  Choice colorChoice, turnChoice, createChoice, initialChoice;

  Universe u;

  public static final int INERTIAL = 0;
  public static final int VARIABLE = 1;
  public static final int LONG = 2;
 
  int type;
 
  
  public ObjectInit(Wizard w, Universe u, int type)
   {
    super(w);
    this.u = u;
    this.type = type;

    setLayout(new GridBagLayout());

    Util.add(this,
        new Label("Please enter the following general data about the object."),
        Util.ENDL); 

    Util.add(this,
        new Label("Name: "),
        Util.DEFAULT); 

    Util.add(this,
        name = new TextField(defaultName(),40),
        Util.ENDL);

    Util.add(this,
        new Label("Invariant mass (rest mass): "),
        Util.DEFAULT); 

    Util.add(this,
        mass = new TextField("0.0",40),
        Util.ENDL);

    Util.add(this,
      new Label("Display Color:"),
      Util.DEFAULT);

    Util.add(this,
      colorChoice = Util.colorChoice(),
      Util.ENDL);

    if (type == LONG)
     {
      Util.add(this,
        new Label("Length: "),
        Util.DEFAULT); 

      Util.add(this,
        length = new TextField("1.0",40),
        Util.ENDL);
     }

    if (type == VARIABLE)
     {
      Util.add(this,
        new Label("At turnarounds, velocity is"),
        Util.DEFAULT);

      Util.add(this,
        turnChoice = new Choice(),
        Util.ENDL);

      turnChoice.add("equal to velocity before turnaround");
      turnChoice.add("equal to velocity after turnaround");
      turnChoice.add("the average of the two velocities");
     }

    if (u.translation_freedom)
     {
      Util.add(this,
        new Label("The initial location is: no preferred location."),
        Util.ENDL);
     }
    else
     {
      Util.add(this,
        new Label("The initial location is"),
        Util.DEFAULT);

      Util.add(this,
        initialChoice = new Choice(),
        Util.ENDL);

      initialChoice.add("an existing event.");
      initialChoice.add("the intersection of two objects.");
      initialChoice.add("the location of an existing object.");
      initialChoice.add("given using a reference frame.");
     }
 
    Util.add(this,
      new Label("The object"),
      Util.DEFAULT);

    Util.add(this,
      createChoice = new Choice(),
      Util.ENDL);

    createChoice.add("will come into existence at the initial location.");
    createChoice.add("has existed since time immemorial.");
   }

  String defaultName()
   {
    switch (type)
     {
      case INERTIAL: return "Observer " + Integer.toString(u.inertialObjects.size());
      case VARIABLE: return "Object " + Integer.toString(u.variableObjects.size());
      case LONG: return "Rod " + Integer.toString(u.longObjects.size());
     }
    return "???";
   }

  public void doNext()
   {
    PhysicalObject obj = new InertialObject();

    switch(type)
     {
      case INERTIAL: break;
      case VARIABLE: 
        obj = new VariableObject();
        switch (turnChoice.getSelectedIndex())
         {
          case 0: ((VariableObject)obj).continuity = VariableObject.BEFORE; 
                  break;
          case 1: ((VariableObject)obj).continuity = VariableObject.AFTER; 
                  break;
          case 2: ((VariableObject)obj).continuity = VariableObject.AVERAGE; 
                  break;
         }
        break;
      case LONG:     
        obj = new LongObject(); 
        try
         {
          obj.length = Util.doubleValue(length);
         }
        catch (NumberFormatException e)
         {
          System.out.println("Error: Length must be a number.");
          return;
         }
        break;
     }

    try
     {
      obj.mass = Util.doubleValue(mass);
     }
    catch (NumberFormatException e)
     {
      System.out.println("Error: Mass must be a number.");
      return;
     }

    obj.name = name.getText();
    obj.color = Util.colorOf(colorChoice);
  
    if (createChoice.getSelectedIndex() == 0) 
      obj.prehistoric = false;

    if (u.translation_freedom)
     { 
       // ???
      return;
     }

    switch(initialChoice.getSelectedIndex())
     {
      case 0:
        if (u.events.size() == 0)
         {
          System.out.println("Error: No events have been defined.");
          return;
         }
        w.addCard(new ObjectFromEvent(w,u,obj,type));
        return;
      case 1:
        if (u.getNumObjects() + u.rays.size() < 2)
         {
          System.out.println("Error: Not enough existing objects.");
          return;
         }
//        w.addCard(new ObjectFromIntersection(w,u,obj,type));
        return;
      case 2:
        if (u.getNumObjects() == 0)
         {
          System.out.println("Error: No objects have previously been defined.");
          return;
         }
 //       w.addCard(new ObjectFromObject(w,u,obj,type));
        return;
      case 3:
        if (u.frames.size() == 0)
         {
          System.out.println("Error: No frames have been defined.");
          return;
         }
        w.addCard(new ObjectFromFrame(w,u,obj,type));
        return; 
     }
   }
 }
