package nxm.vnc.prim;

import java.awt.BorderLayout;
import java.awt.ScrollPane;

import nxm.sys.lib.Message;
import nxm.sys.lib.Table;
import nxm.sys.libg.GMenu;
import nxm.sys.libg.GPrimitive;
import nxm.sys.libg.MWindow;
import nxm.vnc.libvnc.tightvnc.NxmViewer;

/**
 VNCVIEWER - Provides a VNC viewer that can be embedded in NeXtMidas windows

 &lt;HOST&gt; Host name of the Vncserver
 &lt;PORT&gt; Port for the VNC Server (for :1 it would be 5901)

    @author  Warren Knight
    @since   NeXtMidas 2.3.0
*/
public class vncviewer extends GPrimitive {
  private NxmViewer viewer;

  /** The bitmask that is and'ed with an exit message to verify if the exit message should be used. */
  private int exitMask = -1;

  /** Indicates that exiting should be allowed from the "Exit" option in the menu. */
  private static final int EXIT_MENU=0x1; //, EXIT_MESSAGE=0x2;

  /** The flags matching the exit options. */
  public static String exitFlagsList = "Menu";//,Message";

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

  public int open () {
    String title = MA.getS("/TITLE", "VNCVIEWER");
    //String theme = MA.getU("/THEME");
    Table   params   = Table.copyOf(MA.getKV());
    exitMask = MA.getOptionMask("/EXIT", exitFlagsList, exitMask);

    MW = new MWindow(title, this);
    MW.open();
    MW.setTheme (MA.getO("/THEME"));
    MW.addTo(this);
    MW.panel.setLayout(new BorderLayout());

    // Create a ScrollPane which will hold a panel with VncCanvas
    // inside.
    ScrollPane desktopScrollPane = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED);
    desktopScrollPane.setSize(MW.panel.getSize());
    MW.panel.add(desktopScrollPane,"Center");


    viewer = new NxmViewer(M, params);
    viewer.setSize(desktopScrollPane.getSize());
    desktopScrollPane.add(viewer);

    viewer.init();

    return NORMAL;
  }

  public int process () {
    if (viewer != null && !viewer.isShutdown()) {
      return NOOP;
    } else {
      System.out.println("Returning Finish");
      return FINISH;
    }
  }

  public int close () {
    if (!viewer.isShutdown()) {
      viewer.setShutdown();
    }
    MW.close();
    MW = null;

    return FINISH;
  }

  public int processMessage (Message msg) {
    if (MW.processMessage(msg) > 0) {
      // Message processed by MWindow
    }
    else if (msg.name.equals("EXIT")) {
      // jph -- exit by message must always be allowed unless the menu exit
      // is re-implemented to not send an EXIT message
      //if ((exitMask&EXIT_MESSAGE)!=0) {
        return FINISH;
      //}
    }
    else if (msg.name.equals("TITLE")) {
      MW.setTitle((String)msg.data);
    }
    else if (msg.name.equals("MENU")) {
      // Pop up the various options available for this graphic.
      String menu = null;

      if ((exitMask&EXIT_MENU)!=0) {
        menu = menu + ",Exit";
      }
      //new GMenu(MW,"TextBox",menu,0,0,this);
      new GMenu(this.MW,"TextBox",menu,0,0,this);
    }
    else if (msg.name.equals("REFRESH")) {
      MW.refresh();
    }
    else if (msg.name.equals("POINTER")
          || msg.name.equals("SHOWN")
          || msg.name.equals("MOVED")
          || msg.name.equals("FOCUS") ) {
      //TBD -- What to do with these messages
    }
    else if(msg.name.equals("WINDOW")) {
      if(msg.data.toString().equals("CLOSING")) {
        msg.name = "EXIT";
        MQ.put(msg);
      }
    }
    else {
//      M.warning("VNCVIEWER: Got unknown message : "+ msg.name);
    }
    return NORMAL;
  }
}
