package nxm.sys.intr;

import nxm.sys.lib.Intrinsic;
import nxm.sys.lib.Terminal;
import nxm.sys.lib.Time;

/** Pauses for specified time.

    @author Jeff Schoen
 */
public class Pause extends Intrinsic {

  /** default constructor */
  public Pause () {}

  public int process() {

    // get parameters
    double sec = MA.getD("TIME");
    verbose = verbose || M.macro==null;

    // implement pause
    if (sec > 0.0) {
      if (verbose) {
        M.print("Pausing "+sec+" seconds ");  // since 3.7.1 using M.print instead of M.terminal.write to allow logging - NXM-1813
        for (;sec>=1.0;sec-=1.0) {
          Time.pause(1.0);
          M.print(".");
        }
        if (sec>0.0) Time.sleep(sec);
        M.print(" done\n");
      }
      else {
        /* Do the (non-verbose) waiting all-at-once since the cumulative overhead of each call to
           Time.wait(..) makes the above code much less accurate.  */
        Time.pause(sec);
      }
    }
    else if (sec == -1) {
      if (M.macro==null) M.error("Indefinite PAUSE only functions in a macro");
      M.macro.suspend=1;
    }
    else if (sec == -2) {
      MT.getInput("Press return to continue...","",Terminal.INPUT_RETURN);
    }
    else {
      M.warning("Unsupport sleep value: "+sec);
    }

    return (FINISH);
  }
}
