!******************************************************************************!
! Test plotting from a KMZ file and swapping WMS Servers
!
! Arguments: Path to KMZ file
!
! @bug      2339 PLOT / LayerKML: Support <STYLEMAP>
! @bug      2581 PLOT / LayerWMS: memory enhancements
! @author:  J Bauer
!******************************************************************************!
startmacro cs:url1["http://nextmidas.com:8800/geoserver/ows"] &
           cs:url2["http://nextmidas.com:8800/wms"] &
           cs:layers1["shapePolitical,Relief,cadrg,imint"] &
           cs:layers2["bmng200401"] &
           cs:crsOverride[""] s:requestedVersion[""]

  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 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 setDatDir

  call createOptEntriesTable

  if interAct then

    set instructions "Initially you should see a basic map of the world containing &
                      KML features. Use the buttons to swap between map servers to  &
                      verify changes in the WMS Mapping Layers."        

    set kmzfile "nxm.sys.test.kmz_test.kmz"
    set aux.temp_test_kmz "jar:^{kmzfile}!/"

    ! URLs of WMS servers, including a bogus one to test failover
    set wms1 "^url1" !use geoserver Since 3.5.3
    set wms2 "^url2"
    set wms3 "http://somebogusurlxxxx:8080/wms/mapServer"

    ! configuration table for WMS connection
    set t:wmsT {}
    set wmsT.lt "WMS"
    TABLE OPTENTRIES CREATE {CRS="",WMS_VERSION=""}
    if CRSOVERRIDE STARTS "{" THEN
      table optentries SETKEY wmsT.CRS "CoordRefSystems=^{crsOverride},"
    elseif CRSOVERRIDE NEQS "" THEN
      ! NXM-3743 support adding CRS entries by name
      table optentries SETKEY wmsT.CRS "ByNameCRSs=^{crsOverride},"
    endif
    if REQUESTEDVERSION NEQS "" THEN
     table optentries SETKEY wmst.WMS_VERSION "RequestedVersion=^{requestedVersion},"
    endif
    set wmsT.CONNECTTIMEOUT "8"
    set wmsT.READTIMEOUT "8"
    set wmsT.AUTOCONNECT "false"
    set wmsT.IGNOREHREF ^ignoreHREF
    set wmsT.url "^wms3,^wms1"   ! bogus url listed first
    set wmsMenu "None,^wms1"
    if url2 NEQS "" then
      set wmsT.url "^{wmsT.url},^wms2"
      set wmsMenu "^{wmsMenu},^wms2"
    endif
    set wmsT.layers "^layers1"
    set wmsT.enable "+BS"
    set wmsT.USETILES "true"
    set wmsT.DOWNLOADTHREADS 4

    ! other KML files to add to the plot
    set kmlfile "^{env.nmroot}.nxm.sys.test.kmlTest.kml"

    ! name of the kml file inside the kmz file (usually 'doc.kml')
    set basekml "doc.kml{aux=temp_test_kmz}"

    ! background map
    set worldfile "^{datDir}.world.prm"

    if /debug then
      info "WMS config = ^wmsT "
    endif

    info "You should see warnings that LayerWMS cannot connect to 'somebogusurlxxx..."

    pipe on
      panel/msgid=MAIN/setup/controls=gc
      gcontrol label macro "Test MACRO"
      gcontrol menu  main "Exit with Status =" "PASS,FAIL" TABLE={ItemColors={PASS="GREEN",FAIL="RED"}} /tleft

      gcontrol text  idtext "Instructions" instructions 24 8 /title

      gcontrol menu wmsMenu "Map Servers" "^wmsMenu" "None" /chkbox /nc=1 /ttop

      plot/id=plot1 "^worldfile"|wmsT|"^basekml"

    pipe off

    rem aux.temp_test_kmz

  endif

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

!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
!******************************************************************************!
procedure processMessage m:msg


  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
    pipe stop

  elseif msg.name eqs "WMSMENU" then
    ! done this way to preserve case
    if msg.info eq 2 then
      set wmsT.layers "^layers1"  ! 4.0.0 moved setting of layers to before URL
      set url "^wms1"
      !set wmsT.layers "^layers1" ! old location
    elseif msg.info eq 3 then
      set wmsT.layers "^layers2"  ! 4.0.0 moved setting of layers to before URL
      set url "^wms2"
      !set wmsT.layers "^layers2" ! old location
    else
      Invoke reg.plot1.layers.wms.disconnect()
      return
    endif

    set reg.plot1.layers.wms.url "^url"
    Invoke reg.plot1.layers.wms.connect()
    set reg.plot1.layers.wms.layers "^{wmsT.layers}"   ! need to reset layers

  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
  if expectedException REXISTS then
    assert/text="^reg.main.name: Expected ERROR starts with: ^{expectedException}" expectedException EQSS emsg.data.message
    remove expectedException
  else
    assert/text="^reg.main.name: Caught unexpected exception ^{emsg.data}" "FALSE" isTrue
    if "Trace" SUBS ENV.DEBUG then invoke ,, emsg.data.printStackTrace()
  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

!******************************************************************************!
! 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
