!******************************************************************************!
! Tests plotting of files for layerMWS to plot from GeoServer and
! getting Capabilities service message
!
! 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
!
! @Note: Does not work with Xinerama displays, since they count as one (large) screen.
! @author  Juan Velasquez
! @Bug: NXM-2947 Plot / LayerWMS: Should not received maxWidth or MaxHeight from
!                             geoService message
! @since NeXtMidas 3.7.1
!******************************************************************************!
!
! Using geoserver on port 8800 as the default map server
startmacro/msgid=main cs:url["http://nextmidas.com:8800/geoserver/ows"] &
                      cs:layers["shapePolitical,Relief,cadrg,imint"] 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 setDatDir

  call createOptEntriesTable

  set this.timer(0) 10
  if interAct then
    set instructions "You should see two plots of the world, each with a WMS layer.\n\n&
                    You Should NOT see a 'file not found' alert on either plot.\n&
                    Click continue after maps are loaded. A test will be performed for\n&
                    the maxWidth and maxHeight which should not be in the XML Capabilities\n&
                    service response received by the GeoServer\n"

    set file1 "^{datDir}.world.prm"
    set file2 "^{datDir}.cities.prm"

    ! Set a result as a Table with the WMS info
    set file3 {LT=WMS,IGNOREHREF=^ignoreHref,URL="^url",LAYERS="^layers"}
    call addFile3TableEntries
    res file3
    ! Set the same info as a Table in String form (requires escape sequence handling to be enabled)
    set file4 "{LT=WMS,IGNOREHREF=^ignoreHref,^{OPTENTRIES.WMS_VERSION}^{OPTENTRIES.CRS}URL=\"^url\",LAYERS=\"^{layers}\"}"

    pipe on
      panel/setup/controls=gc/msgid=main
      plot/id=top file1|file2|file3
      plot/id=bottom file1|file2|file4

      gcontrol label macro "MACRO"
      gcontrol button main  "" "Continue"
      gcontrol text idtext  "INSTRUCTIONS" "^instructions" 24 18 /title
    pipe off
  endif

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

!******************************************************************************!
procedure processMessage m:msg
!  say "Msg ^msg.name = ^msg.data"
  if msg.name EQS "MAIN" then
    assert/STATUS=^service.maxwidth expectedException NREXISTS
    assert/STATUS=^service.maxheight expectedException NREXISTS
    pipe stop
  elseif msg.name EQS "TIMER"
    set this.timer(0) 0
    ! save the result table from the getCapabilities GeoService message
    res capt reg.top.layers.wms.getCapabilities()
    res capb reg.bottom.layers.wms.getCapabilities()
    ! the service record should not contain the maxWidht or maxHeight optional parameters
    res service "^{capt.service}"
  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 for use in string representation
!******************************************************************************!
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

!******************************************************************************!
! Add optional LayerWMS LAYER entries to existing file3 table
!******************************************************************************!
procedure addFile3TableEntries

  if CRSOVERRIDE STARTS "{" THEN
    table file3 SETKEY CoordRefSystems "^{crsOverride}"
  elseif CRSOVERRIDE NEQS "" THEN
    ! NXM-3743 support adding CRS entries by name
    table file3 SETKEY ByNameCRSs "^{crsOverride}"
  endif
  if REQUESTEDVERSION NEQS "" THEN
    table file3 SETKEY RequestedVersion "^{requestedVersion}"
  endif

return
