package nxm.sys.intr;

import nxm.sys.lib.Intrinsic;
import nxm.sys.lib.Parser;

/**
  Asks user for input at the terminal

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

  String label, prompt, answer;

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

  @SuppressWarnings("deprecation")  // for M.terminal - since 3.7.1 for NXM-2813
  public int process() {

    label = MA.getU("LABEL");
    prompt = MA.getU("PROMPT");

    do {
      answer = M.terminal.getInput(prompt,"",0);

      if (answer.length()==0) {
        int b1 = prompt.indexOf('[');
        int b2 = prompt.lastIndexOf(']');
        if (b1>=0 && b2>0) answer = prompt.substring(b1+1,b2);
      }

      if (!label.startsWith("Q:")) {
        Parser p = new Parser(answer);
        p.clean();
        answer = p.get(1);
      }

    } while (answer.length()==0 && !MA.find("/ONCE"));

    MR.put (label,answer);

    return FINISH;
  }
}
