!
! Test the plotting maps from OGC WMS Map Server and toggling on/off WMS layers.
!
! Author:  J. Hallam / J. Bauer
!
! Since 3.5.3 - this test was adjusted to default to GeoServer instance on port 8800
!******************************************************************************!
startmacro cs:url["http://nextmidas.com:8800/geoserver/ows"] &
           cs:layers["shapePolitical,cadrg"]  cs:crsOverride[""] &
           s:requestedVersion[""]

  switch INTERACTIVE interAct GET DEF=TRUE  SDEF=TRUE
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE
  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)

  set defaultInstructions &
      "Should see a plot of the world with the nations of the world. Click on &
       'GREYSCALE' and 'FULL COLOR' to toggle the greyscale settings, then click on &
       the 'ZOOM' button to see a portion of the Persian Gulf that includes both &
       CADRG maps and simple political shapes. "
  
  set afterZoom "Click on the CADRG and POLITICAL buttons below to toggle the map/shape type."

  set afterTogglePol "You should see the political map shapes toggle on/off. "
  set afterToggleCadrg "You should see the CADRG map shapes toggle on/off. "

  set toggleChoices "^layers"

  call createOptEntriesTable

  if interAct then
    pipe init

    panel/msgid=MAIN/setup/controls=gc
    plot/id=idplot world|{LT="WMS",IGNOREHREF=^ignoreHREF,^{OPTENTRIES.WMS_VERSION}URL="^url",^{OPTENTRIES.CRS}LAYERS="^layers"}
    say "{LT="WMS",IGNOREHREF=^ignoreHREF,^{OPTENTRIES.WMS_VERSION}URL="^url",^{OPTENTRIES.CRS}LAYERS="^layers"}"

    set wmsLayer reg.idplot.layers.wms

    gcontrol label mainLabel "Main"
     gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
     set gc.main.itemColors {PASS=GREEN,FAIL=RED}
    gcontrol text idtext "Instructions" "" 40 12 /title

    gcontrol  button zoom      ""           "ZOOM"

    gcontrol LABEL optionsLabel "Options"
     gcontrol menu toggle "TOGGLE MASK" "^toggleChoices" "ALL" /tleft /toggle

     gcontrol button greyscale "" "GREYSCALE,FULL COLOR" "FULL COLOR"
    pipe off

  endif

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

!******************************************************************************!
procedure open
  ! Automatically called
   call setInstructions "^defaultInstructions"
return

!******************************************************************************!
procedure processMessage m:msg
  !say "msg.name ^msg.name, data ^msg.data"

  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: Interactive tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name EQS "ZOOM" then
    message sendx id=idplot name=zoom info=1 data={XMIN=D:47.15,XMAX=D:56.4,YMIN=D:23.54,YMAX=D:30.5}
    call setInstructions "^afterZoom"
  elseif msg.name EQS "TOGGLE" then
    if msg.data EQS "POLITICAL" then
      set reg.idplot.layers.wms.layers "~shapePolitical"
      call setInstructions "^afterTogglePol"
    elseif msg.data EQS "CADRG" then
      set reg.idplot.layers.wms.layers "~cadrg"
      call setInstructions "^afterToggleCadrg"
    else
      res ucLayer msg.data
      invoke ucLayers layers.toUpperCase()
      invoke l:startLayer ucLayers.indexOf(^ucLayer)
      if startLayer GE 0 then
        invoke l:lengthLayer uclayer.length()
        calc l:endLayer startLayer lengthLayer +
        invoke csLayer layers.substring(startLayer,endLayer)
        set reg.idplot.layers.wms.layers "~^csLayer"
      else
        info "Layer ^ucLayer not found in layers: ^mylayers"
      endif
    endif
  elseif msg.name EQS "GREYSCALE" then
    if msg.data EQS "GREYSCALE" then
      set wmsLayer.greyscale true
    else ! FULL COLOR
      set wmsLayer.greyscale false
    endif
  else
    ! do nothing
    say "^reg.main.name: Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif

return

!******************************************************************************!
procedure setInstructions s:msgString
  set reg.panel.controls.idtext.value msgString
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
