package nxm.sys.intr;

import nxm.sys.lib.Intrinsic;
import nxm.sys.lib.MidasException;
import nxm.sys.lib.Shell;
import nxm.sys.libg.MBox;
import nxm.sys.libg.MJBrowser;
import nxm.sys.libg.MJFrame;
import nxm.sys.libg.MJHelp;
import tools.BuildProps;         /* Used to access build properties. */

/** Displays HELP for a given keyword or query string. HELP can create the help index via the /INDEX
    switch. HELP displays a graphical user interface to query the help index via the /GUI switch.
    Otherwise HELP is an interactive command that users can list the search results and the result
    contents in a NeXtMidas shell.

    @author unknown / J. Czechowski IV / Jay Hallam / Neon Ngo
 */
abstract class HelpGui extends Intrinsic {
  /** Swing Frame containing the Help GUI */
  private MJFrame helpGUIFrame;
  /** reference to the Help GUI */
  private MJHelp  mjhelp;
  /** default Close operation for /GUI or /LITEGUI */
  private int defaultCloseOp;
  /** is Shell running under one-shot mode ie. {@link Shell#isOneShot()} */
  private boolean isOneShot;

  /** has the Help LiteGUI been started? */
  private boolean liteGUIRunning;

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

  public int open () {
    super.open();
    helpGUIFrame   = null;
    mjhelp         = null;
    isOneShot      = Shell.isOneShot();
    liteGUIRunning = false;

    // default to a quarter second poll time, since nothing is done in process() for /GUI and /LITEGUI
    if (isOneShot) pollTime = args.getD("/POLL", 0.25);

    if ((MA.getState("/GUI") || MA.getState("/LITEGUI")) && isOneShot) defaultCloseOp = MJFrame.EXIT_ON_CLOSE;
    else defaultCloseOp = MJFrame.DISPOSE_ON_CLOSE;
    return NORMAL;
  }

  public int close () {
    super.close();
    if (helpGUIFrame != null) {
      mjhelp = null;
      helpGUIFrame = null;
    }
    return NORMAL;
  }

  /** Start Interactive HelpGUI (it will stay up even after HELP command FINISHes).
   *  HelpGUI exits when user closes GUI or when Shell exits.
   *
   *  @param sysBuildProps The BuildProps for the SYS Option Tree
   *  @return NOOP under one-shot mode (otherwise GUI closes immediately),
   *          FINISH otherwise
   */
  int runHelpGUI (BuildProps sysBuildProps) {
    if (helpGUIFrame == null) {
      try {
        if (helpGUIFrame == null) initHelpGUIComponents(sysBuildProps.getPath("help.index.dir"));
        helpGUIFrame.setVisible(true);
        mjhelp.requestFocusForSearch(); // request focus on search text input field
      } catch (Throwable t) {
        throw new MidasException("Aborted run of HelpGUI.", t);
      }
    }
    return (isOneShot) ? NOOP : FINISH;
  }

  /** Launch the "lite" HelpGUI w/o the MJBrowser (viewer) and exit.
   *
   *  @param sysBuildProps The BuildProps for the SYS Option Tree
   *  @return NOOP under one-shot mode (otherwise GUI closes immediately),
   *          FINISH otherwise
   */
  int runHelpLiteGUI (BuildProps sysBuildProps) {
    if (!liteGUIRunning) {
      try {
        String indexPath = sysBuildProps.getPath("help.index.dir");
        MJHelp.runAsApplication("NeXtMidas Search", indexPath, 500, 400, Integer.valueOf(defaultCloseOp));
        liteGUIRunning = true;
      } catch (Exception e) {
        M.error("Aborted run of Lite HelpGUI. "+e);
      }
    }
    return (isOneShot) ? NOOP : FINISH;
  }

  /** Create and initialize HelpGUI graphic components (Frame, JSplitPane, MJHelp, MJBrowser).
   *  Does not set the frame to visible.
   *
   *  @param indexPath The index path
   */
  private void initHelpGUIComponents (String indexPath) {
    helpGUIFrame = new MJFrame("NeXtMidas Search", new MBox(20, 30, 800, 600));
    javax.swing.JSplitPane jSplitPane = new javax.swing.JSplitPane();
    mjhelp = new MJHelp();
    MJBrowser browser = new MJBrowser();
    mjhelp.setViewer(browser);

    jSplitPane.setDividerLocation(220);
    jSplitPane.setDividerSize(8);

    jSplitPane.setLeftComponent(mjhelp);
    jSplitPane.setRightComponent(browser);

    helpGUIFrame.getContentPane().add(jSplitPane, java.awt.BorderLayout.CENTER);
    helpGUIFrame.pack();
    helpGUIFrame.setDefaultCloseOperation(defaultCloseOp);

    mjhelp.setIndexPath(indexPath);
  }
} // end class HelpGui
