package nxm.sys.libg;

import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import nxm.sys.inc.MessageHandler;
import nxm.sys.inc.ProvisionalUseOnly;
import nxm.sys.lib.Shell;
import nxm.sys.libg.Theme;

/**
  This class creates a Swing-based {@link JEditorPane} and displays styled text (e.g. HTML)
  as a GCONTROL widget. A scrollpane (Linux only) is enabled while displaying content larger then
  the size of the widget used.
  @author Edward R. Cable, Neon Ngo
  @since NeXtMidas 3.1.1
 */
public class GSText extends GWidget {

  protected JEditorPane editorPane;
  private JScrollPane scrollPane;
  private static boolean fixTimingIssueWithTextWidth = true;

  /**
    Instantiate a new styled text area, initialize the parameters, and open the graphics panel.
    @param parent the parent object
    @param name the name of this styled text area
    @param stext the styled text to be displayed
    @param aw the width
    @param ah the height
    @param flags the widget flags
    @param mh the MessageHandler
   */
  public GSText (Object parent, String name, String stext, int aw, int ah, int flags,
      MessageHandler mh) {
    super("GSText", parent, name, flags, mh);
    open(name, aw, ah, stext);
    addTo(parent);
  }

  /**
    Open the graphics window for this style text area with the given name. This area will be sized
    according to the given width and height, and will display the given text.
    @param name the name of this styled text area
    @param aw the width
    @param ah the height
    @param stext the styled (e.g. HTML) text to be displayed
   */
  public void open (String name, int aw, int ah, String stext) {
    open(); // get text sizes
    super.open(name, aw * tw, ah * th + ta);
    setupGSTextArea(stext, aw, ah);
  }

  /**
    Define the layout of the JEditorPane and the scrollpane (Linux only)
    @param stext The HTML/text/RTF assigned to the label
    @param ah the height
    @param aw the width
   */
  void setupGSTextArea (String stext, int aw, int ah) {
    if (stext == null) {
      throw new IllegalArgumentException("STEXT cannot be null.");
    }
    editorPane = new JEditorPane();
    editorPane.setContentType("text/html");
    editorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE); // Needed for forcing foreground text color specifications
    editorPane.setText(stext);
    editorPane.setVisible(true);
    editorPane.setSize(aw * tw, ah * th + ta);
    editorPane.setEditable(false);
    editorPane.setBackground(theme.cwbg);
    editorPane.setForeground(theme.cwfg);

    // Windows and Solaris do not size the scroll pane properly so for now it is only Linux
    if (Shell.osname.equals("Linux")) {
      scrollPane = new JScrollPane(editorPane);
      scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
      scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      panel.add(scrollPane);
    }
    else {
      panel.add(editorPane);
    }
    setBounds();
  }

  public void resize (int mode) {
    super.resize(mode);
    setBounds();
  }

  /** Defines the the bounds of the JEditorPane frame */
  private void setBounds () {
    editorPane.setBounds(bw, bw + titleh, pw, ph);
    if (scrollPane != null) {
      // Keep the scroll pane the same size as the textArea
      scrollPane.setBounds(bw, bw + titleh, pw, ph);
    }
  }

  @Override
  public void setBounds (int x, int y, int w, int h) {
    super.setBounds(x, y, w, h);
    // the code below is intended to fix very intermittent issue NXM-3550
    // where a GText under a GLabel has old size information and is too narrow
    if (fixTimingIssueWithTextWidth) {
      // This issue only occurs when there is a visible scroll bar
      // Matching this code to GText, which has a placement problem when
      // these changes are in place and the scroll bar is not visible
      if ((scrollPane != null) && (scrollPane.getVerticalScrollBar().isVisible())) {
        pw = w-bw-bw;
        editorPane.setBounds(bw, bw + titleh, pw, ph);
        scrollPane.setBounds(bw, bw + titleh, pw, ph);
      }
    }
  }

  /** Sets the value of styled text to be assigned to the JEditorPane.
      @param value The new text for this object.
   */
  public void setValue (String value) {
    editorPane.setText(value);
  }

  /** Returns the value of the text assigned to the JEditorPane.
      @return The current value of this control.
   */
  public String getValue () {
    return editorPane.getText();
  }

  /** Sets the value of the foreground and background themes on the widget */
  @Override
  public void setWidgetTheme (Object o) {
    super.setWidgetTheme(o);
    editorPane.setForeground(theme.getWidgetForeground());
    editorPane.setBackground(theme.getWidgetBackground());
  }

  @Override
  public void setTheme (Theme theme) {
    super.setTheme(theme);
    editorPane.setBackground(theme.cwbg);
    editorPane.setForeground(theme.cwfg);
  }

  @Override
  protected int processEventMessage (String name, int info, Object data) {
    if (button==3 && !is(PERM)) { // right mouse button (close if NOT permanent)
      close();
      return 1;
    }
    else {
      //  System.out.println("Process Event Message: " + name + " info " + info + " data  " + data);
    }
    return -1;
  }

  /** Turn off fix intended to fix timing error where text area does not
   *  get updated with correct width
   *
   *  @param useFix false to turn off fix
   *  @since NeXtMidas 3.9.3 for NXM-3550
   */
  @ProvisionalUseOnly ("temporary means of backing out fix")
  public static void setFixTimingIssueWithTextWidth(String useFix) {
    fixTimingIssueWithTextWidth = useFix.toLowerCase().equals("true");
  }
}
