import java.awt.event.*;
import javax.swing.JOptionPane;
import javax.swing.JMenuItem;

public class SimulatorListener implements ActionListener {
   private Simulator simulator;
   
   public SimulatorListener (Simulator s){
      simulator = s;
   }
   /**
    * Nota: Se modifico el menu, por ende, se modificaron los eventos
    * relacionados con este.
    * Se agregaron las opciones de Reset, Gravity.
    */
   public void actionPerformed(ActionEvent e) {
      JMenuItem menuItem = (JMenuItem)(e.getSource());
      String text = menuItem.getText();
      if (text.equals("Start")) simulator.start();
      if (text.equals("Stop")) simulator.stop();
      if (text.equals("Reset")){ 
          int rst=JOptionPane.showOptionDialog(

                 menuItem, "Reset ?", "Settings",
                 JOptionPane.YES_NO_CANCEL_OPTION,
                 JOptionPane.QUESTION_MESSAGE, null,
                 new Object[] { "All", "Last Object Created" },
                 "All");
               if (rst != -1){
            if (rst == 0)simulator.reset();
                if (rst == 1)simulator.resetLast();
         }
      }
      if (text.equals("Delta t")) {
         String delta = JOptionPane.showInputDialog("Enter delta t:");
         simulator.setDelta_t(Float.parseFloat(delta));
      }
      if (text.equals("Refresh time")) {
         String delta = JOptionPane.showInputDialog("Enter refresh time:");
         simulator.setSamplingPeriod(Float.parseFloat(delta));
      }
      if (text.equals("Gravity")) {
         int gravity=JOptionPane.showOptionDialog(

                 menuItem, "Gravity ?    g = "+simulator.bsConfig.gravity, "Settings",
                 JOptionPane.YES_NO_CANCEL_OPTION,
                 JOptionPane.QUESTION_MESSAGE, null,
                 new Object[] { "Yes", "No Gravity","Change Value" },
                 "Yes");
         if (gravity != -1){
            if (gravity == 0);
            if (gravity == 1){
                simulator.setGravity(0);}
            if (gravity == 2){
                String grav = JOptionPane.showInputDialog("Enter gravity  g:");
                if(grav!=null){Float g=Float.parseFloat(grav);
                    simulator.setGravity(g);}
            }
        
         }
         
      }
   }
}