!
! Test plotting map from OGC WMS Map Server onto PLOT without an accompanying world.prm.
! This also show how to plot all available layers on the Map Server (minus the "tiger-ny", "shapePolitical", and
! "cadrg" layers). (If the tiger-ny layer was displayed, then the world map would be hidden by a solid gray layer.
! If the layers were displayed minus only the tiger-ny layer, the the political, rather that the relief map would
! be in the foreground.  Since 3.5.3 - this test was adjusted to work with GeoServer instance on port 8800
!
! Arguments:
!   url              - url
!   layers           - layers to display
!   crsOverride      - (OPTIONAL) Table that will override the table created from wms_cord_system.xml
!   requestedVersion - (OPTIONAL) Specific WMS Version request to be given priority over default
!
! Bug:     1349 LayerMap: WMS and NMS look strange if not accompanied by a world file
! Bug:     2880 LayerWMS should support layers with a space in their name
! Author:  Neon Ngo
!******************************************************************************!
startmacro cs:url["http://nextmidas.com:8800/geoserver/ows"] &
           cs:layers["*,-tiger-ny,-shapePolitical,-cadrg"]  cs:crsOverride[""] &
           s:requestedVersion[""]
  switch INTERACTIVE interAct GET DEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE
  switch IGNOREHREF  ignoreHREF   GET DEF=FALSE SDEF=TRUE ! Ignore GetCapabilities HREF and use original URL

  invoke z:jvmHangFix nxm.sys.lib.Shell.getJVMHangWorkaround()
  res z:mytrue true
  invoke ,, nxm.sys.lib.Shell.setJVMHangWorkaround(^mytrue)

  call createOptEntriesTable

  if layers EQS ""
    res layers "*,-tiger-ny,-shapePolitical,-cadrg"
    res layerSpaceTest "Click on 'Layer with spaces' to show USA and SD archaeological sites"
  else
    res layerSpaceTest "Do NOT Click on 'Layer with spaces'"
  endif

  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE

  if interAct then
    pipe on
      panel/msgid=MAIN/setup/controls=gc
      plot/id=idplot {LT="WMS",IGNOREHREF=^ignoreHREF,^{OPTENTRIES.WMS_VERSION}URL="^url",^{OPTENTRIES.CRS}LAYERS="^layers",ConnectTimeout=30,ReadTimeout=120} view=LatLon

      gcontrol label macro "MACRO"
      gcontrol menu    main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}

      set instructions "World map from WMS Map Server should be displayed with Position (0,0) in the middle of the PLOT. \n&
            ^layerSpaceTest"

      gcontrol text    idtext "Instructions" "^instructions" 24 10 /title
      gcontrol button showLayerWithSpaces "Show" "Layer with spaces" /Toggle /ChkBox

    pipe off
  endif

  invoke ,, nxm.sys.lib.Shell.setJVMHangWorkaround(^jvmHangFix)
endmacro

!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: Interactive tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name EQS "SHOWLAYERWITHSPACES" then
    if msg.info eq 1 then ! since 3.5.3 to test BUG 2880
      res reg.idplot.layers.wms.layers "topp:states,Test Layer with spaces in name"
    else
      res reg.idplot.layers.wms.layers layers
    endif
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! Create Table holding optional LayerWMS LAYER entries
!******************************************************************************!
procedure createOptEntriesTable

  TABLE OPTENTRIES CREATE {CRS="",WMS_VERSION=""}
  if CRSOVERRIDE STARTS "{" THEN
    table optentries SETKEY CRS "CoordRefSystems=^{crsOverride},"
  elseif CRSOVERRIDE NEQS "" THEN
    ! NXM-3743 support adding CRS entries by name
    table optentries SETKEY CRS "ByNameCRSs=^{crsOverride},"
  endif
  if REQUESTEDVERSION NEQS "" THEN
    table optentries SETKEY WMS_VERSION "RequestedVersion=^{requestedVersion},"
  endif

return
