package nxm.sys.prim;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import nxm.sys.lib.Message;
import nxm.sys.lib.Midas;
import nxm.sys.lib.Time;
import nxm.sys.libg.GPrimitive;
import nxm.sys.libg.MJText;
import nxm.sys.libg.MWText;
import nxm.sys.libg.MWindow;

/**
  A class to display results parameters in a compact window

  @author Jeff Schoen
*/
public class display extends GPrimitive implements ActionListener {

  int nargs,exit;
  double pause,tlast;
  Arg[] args;
  MWText MWT;

  private static final int EXIT_RETURN=0x1, EXIT_MENU=0x2,
          EXIT_MESSAGE=0x4, EXIT_WINDOW=0x8, EXIT_ALL=0xF, EXIT_AUTO=0x10;
  private static final String exitList = "RETURN,MENU,MESSAGE,WINDOW,AUTO";
  boolean exiting;

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

  public int open () {

    nargs = MA.numberOf();

    pause = MA.getD("/PAUSE",0.5);
    int fontsz = MA.getL("/FONTSZ",12);
    int flags  = MJText.TEXT | MJText.BACKL | MJText.COLOR;

    exit = (M.pipeMode==Midas.PINIT)? 0 : EXIT_ALL;
    exit = MA.getOptionMask("/EXIT",exitList,exit);
    int rows = MA.getL("/ROWS", nargs+1);
    int cols = MA.getL("/COLS", 40);

    // The text window
    MWT = new MWText("Display",nargs,rows,cols,fontsz,flags,this);
    MWT.mtext.addActionListener (this);
    MW = (MWindow) MWT;
    MW.open();
    MW.addTo(this);

    args = new Arg[nargs];
    for (int i=0; i<nargs; i++) parseArg(i);

    return NORMAL;
  }

  public int process () {
    if (exiting) return FINISH;
    double time = Time.current();
    if (time-tlast<pause) return NOOP;
    tlast = time;
    for (int i=0; i<nargs; i++) checkArg(i);
    return NORMAL;
  }

  public int processMessage (Message msg) {
    int status = NORMAL;
         if (msg.name.equals("EXIT")) exiting = (exit&EXIT_MESSAGE)!=0;
    else if (msg.name.equals("RESIZE")) MW.resize(0);
    else if (msg.name.equals("WINDOW")) {
      String action = (String)msg.data;
      if (action.equals("CLOSING")) exiting = (exit&EXIT_WINDOW)!=0;
    }
    else status = MW.processMessage(msg);
    return status;
  }

  public int close () {
    MW.close();
    return NORMAL;
  }

  private void parseArg (int i) {
    int j;
    String line = MA.getU(i+1);
    String separator = " ==> ";
    Arg arg = new Arg();
    j = line.indexOf("//");
    if (j>0) { separator = line.substring(j+2); line = line.substring(0,j); }
    if (separator.charAt(0)=='"') separator = separator.substring(1,separator.length()-1);
    String prompt = line;
    j = line.indexOf("/");
    if (j>0) { prompt = line.substring(j+1); line = line.substring(0,j); }
    if (prompt.charAt(0)=='"') prompt = prompt.substring(1,prompt.length()-1);
    arg.name = line;
    arg.prompt = prompt + separator;
    args[i] = arg;
    MWT.mtext.add(arg.prompt);
    checkArg(i);
  }

  private void checkArg (int i) {
    Arg arg = args[i];
    Object obj = MR.get(arg.name);
    if (obj==null) return;
    String value = obj.toString();
    if (arg.value==null || !value.equals(arg.value)) {
      arg.value = value;
      MWT.mtext.setLine(i+1,arg.prompt+arg.value);
    }
  }

  /** An inner class for arguments passed to display */
  public class Arg {
    String name;
    String prompt;
    String value;

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

  public void actionPerformed (ActionEvent ae) {
    String action = ae.getActionCommand();
    //System.out.println("Action "+action);
  }

}
