!
! Tests GeoWebCache WMS support.
!
! Arguments:
!   url              - url
!   layers           - layers to display (one at a time)
!   transitions      - resolutions where to switch to next layer
!
! @issue   NXM-3807 LayerWMS GeoWebCache support
! @author  Jeanette McCall
! @since   NeXtMidas 4.1.0
!
!******************************************************************************!
startmacro cs:url["http://nextmidas.com:8800/geoserver/ows"] cs:layers["shapePolitical,Relief,cadrg,imint"] &
           transitions["500,1.5e3,1e8"]

  switch INTERACTIVE  interAct     GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE     baseline     GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)
  switch NOCONFIGMENU noconfigMenu GET DEF=FALSE SDEF=TRUE

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

  call setDatDir

  ! Turn on gwc compatibility checking
  invoke origGwcCompatibilityChecking nxm.sys.libg.LayerWMS.getGwcCompatibilityChecking()
  invoke ,, nxm.sys.libg.LayerWMS.setGwcCompatibilityChecking(true)
 
  set Z:configMenu TEST(noConfigMenu,isFalse)

  if TRANSITIONS NEQS "" THEN
    res urltransitions "TRANSITIONS=\"^{transitions}\","
  else
    res urltransitions ""
  endif

  ! can add in TRANSITIONS="500,1.5e3,1e8", after LAYERS for testing transitions

  if interAct then

    pipe on
      panel/msgid=MAIN/setup/controls=gc
      plot/id=idplot ^{datDir}.world.prm{ENABLE=+BS}|{LT=WMS,DEBUG=TILESBBOX,URL="^url",LAYERS="^layers",^{urltransitions}GWC=true,NumRetriesIfBGColor=4} view=LatLon options=+dbuf READOUTEXT=+Show|+WMS_level READOUT=+onAxis

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

      set baselineInstructions ""
      if baseline OR configMenu then set baselineInstructions "Move the config menu out of the way, but do not close it. "
      set instructions "^{baselineInstructions}\n\n&
                        Zooming in and panning should generally be more responsive &
                        if the URL you have chosen is a GeoWebCache, as this is run &
                        with GWC=true to trigger settings that allow the user to get cached results.\n\n&
                        Configuration menu setting should be:\n\n&
                        'Configuration:'\n  &
                          should have: USETILES='true' and\n               &
                                       GEOWEBCACHE='true'\n  &
                          should not have: BGCOLOR=\n\n&
                         'BgColor:' should show the color white,\n           &
                                    but it is left off the URL request."
      gcontrol text    idtext "Instructions" "^instructions" 24 20 /title

      gcontrol/nc=2 button zooms "DIFFERENT ZOOMS (performance evaluation only)" &
                  "US,SOUTH AMERICA,AUSTRALIA,AFRICA,UK,ITALY,GERMANY,NORTH PACIFIC,INDIAN OCEAN" ""

      if baseline OR configMenu then
        invoke ,, reg.idplot.layers.wms.configure
      endif

    pipe off

  endif  ! end if interAct

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

!******************************************************************************!
procedure processMessage m:msg
  !say "Got Message: name=^msg.name info=^msg.info data=^msg.data"

  if msg.name eqs "MAIN" then
    ! Check if URLs are gwc-compatible
    invoke gwcCompatibleURLs nxm.sys.libg.LayerWMS.getGwcCompatibleURLs()
    assert/text="^reg.main.name: URLs are compatible with GeoWebCache" gwcCompatibleURLs isTrue

    ! Restore the original state of gwc-compatibility checking
    invoke ,, nxm.sys.libg.LayerWMS.setGwcCompatibilityChecking(^origGwcCompatibilityChecking)

    assert/text="^reg.main.name: Interactive tests" msg.data eqss "PASS"
    pipe stop

  elseif msg.name eqss "ZOOMS" then
    if msg.data eqss US  then
      message send IDPLOT  name=zoom DATA={X1=-124,X2=-72,Y1=8,Y2=65}
    elseif msg.data eqss "SOUTH AMERICA" then
      message send IDPLOT  name=zoom DATA={X1=-90,X2=-20,Y1=-60,Y2=12}
    elseif msg.data eqss "AUSTRALIA" then
      message send IDPLOT  name=zoom DATA={X1=110,X2=160,Y1=-50,Y2=0}
    elseif msg.data eqss "AFRICA" then
      message send IDPLOT  name=zoom DATA={X1=-20,X2=55,Y1=-40,Y2=35}
    elseif msg.data eqss "GERMANY" then
      message send IDPLOT  name=zoom DATA={X1=6,X2=16,Y1=44,Y2=55}
    elseif msg.data eqss "ITALY" then
      message send IDPLOT  name=zoom DATA={X1=6,X2=18,Y1=35,Y2=48}
    elseif msg.data eqss "UK" then
      message send IDPLOT  name=zoom DATA={X1=-6,X2=2,Y1=50,Y2=58}
    elseif msg.data eqss "NORTH PACIFIC" then
      message send IDPLOT  name=zoom DATA={X1=-157,X2=-128,Y1=10,Y2=50}
    elseif msg.data eqss "INDIAN OCEAN" then
      message send IDPLOT  name=zoom DATA={X1=70,X2=95,Y1=-35,Y2=5}
    endif
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid dats: ^msg.data"
  endif
return

!******************************************************************************!
! Set the dat directory
!******************************************************************************!
procedure setDatDir
  invoke nxmMajor env.nmversion.subString(0,1)
  invoke major java.lang.Integer.valueOf(nxmMajor)
  if major GE 4 then
    invoke s:datDir           nxm.sys.test.DatLocator.getDatDirLocation()
    invoke s:datDirSlash      nxm.sys.test.DatLocator.getDatDirSlashLocation()
    invoke z:sysDatFull       nxm.sys.test.DatLocator.sysDatFull()
    invoke z:sysTestDatExists nxm.sys.test.DatLocator.sysTestDatExists()
  else
    set s:datDir           "nxm.sys.dat"
    set s:datDirSlash      "nxm/sys/dat"
    set z:sysDatFull       true
    set z:sysTestDatExists false
  endif
return
