package nxm.ucl.prim;

import java.net.URLEncoder;

import nxm.sys.lib.Message;
import nxm.sys.lib.Shell;
import nxm.sys.libg.GPrimitive;
import nxm.ucl.lib.HttpWin;
import nxm.ucl.lib.HttpWin.HttpWinCallBackIF;

/**
  Displays a web page.

  @author MSZ
  @author Jay Hallam
*/
public class httpwindow extends GPrimitive implements HttpWinCallBackIF {
  private HttpWin hw = null;

  private String onExitList = "HIDE,CLOSE";
  private String onExit = "HIDE"; // Default as run from macro
  private String page = null;

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

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

  @Override
  public int open() {
    try {
      boolean runFromShell = (M.macro == null);
      if (runFromShell) onExit = "CLOSE";
      String desiredExit = MA.getS("/ONEXIT",onExit);
      info("open: desiredExit "+desiredExit);
      if (runFromShell && !desiredExit.equals("HIDE")) {
        exitOnClose();
        page = "file://"+Shell.getNmRoot()+"htdocs/index.html";
      } else {
        hideOnClose();
      }
      String muser = MA.getCS("USER","");
      String mpass = MA.getCS("PASS","");
      boolean show = MA.getState("/SHOW", runFromShell);
      String title = MA.getCS("/TITLE","HTML");
      page  = MA.getCS("PAGE",page);
      exitMask  = MA.getOptionMask("/EXIT",exitList,exitMask);
      info("httpwin.open: in Shell? "+runFromShell+" exit Mask="+exitMask+" onExit="+onExit
               +" user '"+muser+"', page "+page+" show? "+show);

      hw = new HttpWin();
      hw.registerCallBack(this); //jph
      if (muser.length()>0) { setUserPass(muser,mpass); }
      if (page != null) { setPage(page); }
      if (show) {
        show();
      } else {
        hide();
      }
      hw.setTitle(title);//jph
    } catch (Exception e) {
      System.out.println("Exception:"+e);
      e.printStackTrace();
    }
    return NORMAL;
  }

  /* No need for a process loop, just do default primitive behavior*/
  @Override
  public synchronized int processMessage (Message msg) {
    try {
      info("Processing a message: "+msg.name);
      if (msg.name.equals("SETPAGE")) hw.setPage((String)msg.data);
      if (msg.name.equals("EXIT") && (exitMask&EXIT_MESSAGE)!=0) shutdown();
    } catch (Exception e) {
      System.out.println("Exception:"+e);
      e.printStackTrace();
    }
    return NORMAL;
  }


  @Override
  public int close() {
    info("httpwin.close: closing ");
    if (hw != null) { hw.destroyFrame(); }
    return NORMAL;
  }

  private void shutdown () {
    info("shutdown, onExit "+onExit);
    if (onExit.equals("CLOSE")) {
      info("httpwin.shutdown, destroyFrame");
//      if (hw != null) { hw.destroyFrame(); }
      setState(FINISH);
    } else {
      // Do nothing for HIDE on exit
      info("httpwin.shutdown, do nothing");
    }
  }

  /* Interface Callbacks - Start */
  @Override
  public void httpWinClosed () {
    info("httpwin.httpWinClosed... state "+state+" onExit "+onExit);
    if ((exitMask&EXIT_WINDOW)!=0) shutdown();
  }

/* Interface Callbacks - End */
  private void info (String in) {
    if (verbose) {
      if (M == null) {
        System.out.println("httpwin: "+in);
      } else {
        M.info("httpwin: "+in);
      }
    }
  }

  /** Set the page to be viewed
   *  @param etxt the page */
  public void setPage(String etxt)                   { if (hw!=null) hw.setPage(etxt); }
  /** Set the page to be viewed
   *  @param turl     The URL String
   *  @param mimetype The Mime-type
   */
  public void setPage(String turl,String mimetype)   { if (hw!=null) hw.setPage(turl,mimetype); }
  /** Set the text
   *  @param ext      The data string
   *  @param mimetype The Mime-type
   */
  public void setText(String ext,String mimetype)    { if (hw!=null) hw.setText(ext,mimetype); }
  /** Set the user name and password
   *  @param euser user name
   *  @param epass user password
   */
  public void setUserPass(String euser,String epass) { if (hw!=null) hw.setUserPass(euser,epass); }

  /** Get the page url
   *  @return the page url
   */
  public String getPage() { return page; }

  /** Get the URL encoder string
      @param in The encoding type
      @return the URL encoder string
      @see URLEncoder
  */
  public String getEncoded(String in) {
    try {
      if (in != null) return URLEncoder.encode(in, "US-ASCII");
    } catch (Exception e) {
      System.out.println("Exception "+e);
      e.printStackTrace();
    }
    return "";
  }
  /** Hide or show the window.
    @param in 1=SHOW 0=HIDE
  */
  public void show(int in) {
    if (hw!=null) {
      if (in == 0) {
        hw.hide();
      } else {
        hw.show();
      }
    }
  }
  /** Hide the window. */
  public void hide() { if (hw!=null) hw.hide(); }
  /** Make the window visible. */
  public void show() { if (hw!=null) hw.show(); }
  /** Set to HIDE when window is closed. */
  public void hideOnClose() { if (hw!=null) hw.hideOnClose(); onExit = "HIDE"; }
  /** Set to EXIT when window is closed. */
  public void exitOnClose() { if (hw!=null) hw.exitOnClose(); onExit = "CLOSE"; }
}