package nxm.ucl.prim;


import java.awt.Component;
import java.awt.Frame;

import nxm.sys.lib.Message;
import nxm.sys.libg.GPrimitive;  //NXM-3678 since 4.0.0 change to libg
import nxm.sys.libg.MWindow;

/** This provides macros with a Swing File Chooser.

    @author  Neon Ngo
    @since   NeXtMidas 2.5.0
    @deprecated Since NeXtMidas 3.3.1, use POPUP FUNC=FILE
*/
@Deprecated // TODO: remove in 4.1.2
public class filechooser extends GPrimitive {
  private String        msgName;     // The message name to use.
  private String        result;      // The name of the result to output.
  private Component     parent;      // The parent component.
  private MWindow       parentMW;    // The MWindow for the parent component (if available).
  private nxm.ucl.libg.MJFileChooser filechooser; // The Swing File Chooser being used.

  /** default constructor */
  @Deprecated
  public filechooser () {}

  public int open() {
    M.deprecate("Since NeXtMidas 3.3.1: use POPUP FUNC=FILE instead of FILECHOOSER;UCL");
    int    status   = NORMAL;

    String title         = MA.getU("TITLE", "FILE CHOOSER");
    String seed          = MA.getCS("SEED" , "");
    String filter        = MA.getCS("FILTER", "*.tmp|prm");
    String selectedAuxes = MA.getU("AUXLIST", MR.getS("AUX.WRITE"));
    String flags         = MA.getCS("/FLAGS");      // see {@link MJFileChooser#optionList}
    String pathOption    = MA.getCS("/PATHOPTION"); // see {@link MJFileChooser#pathOptionList}
    result               = MA.getU("RESULT","").toUpperCase(); // result name
    msgName              = MA.getS("/MSGNAME", "FILECHOOSER");

    // NTN: this is code block is copied from nxm.sys.prim.popup to set parent window
    Object win = MA.getO("/WIN", null);
    if (win instanceof Component) {
      parent = (Component)win;
    } else if (win instanceof GPrimitive) {
      parentMW = ((GPrimitive)win).MW;
      parent   = ((GPrimitive)win).getGraphicsContainer();
    } else {
      if ((win == null) && (M.macro != null)) { win = "PANEL"; }

      win = MR.get("REG."+win);
      if (win instanceof GPrimitive) {
        parentMW = ((GPrimitive)win).MW;
        parent   = ((GPrimitive)win).getGraphicsContainer();
      }
    }

    Frame parentFrame = null;
    if (parent instanceof Frame) {
      parentFrame = (Frame) parent;
    } else if (parentMW!=null && parentMW.parent instanceof Frame) {
      parentFrame = (Frame) parentMW.parent;
    }

    filechooser = new nxm.ucl.libg.MJFileChooser(parentFrame, title, seed, filter, selectedAuxes, flags, pathOption, M);

    filechooser.setVisible(true);
    return status;
  }

  public int process() {
    int status;
    if (!filechooser.isVisible()) { status = FINISH; }
    else                          { status = NOOP; } // Wait for input

    return status;
  }

  public int close() {
    int    info = filechooser.getInfo();
    Object data = filechooser.getValue();

    sendMessage(new Message(msgName, info, data));
    if ((result != null) && (result.length() > 0)) {
      // save user selected filename into specified RESULT parameter
      MR.put(result, data);
    } else if (verbose) {
      M.type("input = " + data);
    }

    filechooser.dispose();
    return NORMAL;
  }

  public int processMessage (Message msg) {
    int status = NORMAL;
    if (verbose) M.type("filechooser - Got message: " + msg);

    if      (msg.name.equals(name))      MR.put(name,msg.data);
    else if (msg.name.equals("CANCEL"))  status = FINISH;
    else if (msg.name.equals("EXIT"))    status = FINISH;
    else if (msg.name.equals("REFRESH")) filechooser.refresh();
    else                                 status = super.processMessage(msg);
    return status;
  }
}
