!******************************************************************************!
! Test that autoscale minimum and maximum values are being correctly set on 
! various Layers.
!
! Issues: NXM-2937 PLOT: LayerScatter constant flicker
!         NXM-2984 PLOT: Layers findRange() not setting values used for autoscaling
!
! @author:  Jeanette McCall
! @since    NeXtMidas 3.7.1
!******************************************************************************!
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  SDEF=TRUE  ! for normal interactive test(s)
  switch BASELINE    baseline GET DEF=FALSE SDEF=TRUE  ! for defects or more obscure test case(s)
  switch ALTTESTENV  alttestenv GET DEF=FALSE SDEF=TRUE
  switch ALTWMS      altwms     GET DEF=FALSE SDEF=TRUE ! WMS is available in alttestenv, still skip min/max test
  switch IGNOREHREF ignoreHREF  GET DEF=FALSE SDEF=TRUE ! Ignore WMS 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

  res alttestenv_instructions ""
  if alttestenv isTrue then
    res alttestenv_instructions "NOTE: THOUGH 'LayerWMS' WILL FAIL, THIS TEST IS USEFUL &
                                 EVEN IF THERE IS NO ACCESS TO A WMS SERVER, but you may &
                                 skip this test if you will run it under test_plot_layerwms_configure\n\n"
  endif

  if interAct then
    set instructions "^{alttestenv_instructions}&
                      You should see 5 plots. \n\n&
                      1) Make sure the plots with a LayerScatter Layer (i.e. the LayerShape and LayerWMS plots) &
                         are not flickering.\n\n&
                      2) Click the 'Check Auto Scale Min/Max' button look at the terminal to make sure there are no failures.\n\n&
                      3) Open a MMB menu on one of the smaller plots. Choose 'scale'. Is it rendering correctly?"
                      
    call setupLayerVehicle

    pipe on
      if alttestenv ISFALSE then
        httpserv 9080 DIR="nxm/sys/test" NAME="test"
      endif

      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 20 /title

      result airportQuals {HEADERROWS=1,LAYER={LT="Scatter",X="LONGITUDE",Y="LATITUDE",LINE={COLOR="red",TYPE="symbol"}}}
      plot/id=plot_layshape  ^{datDir}.world.shp|^{datDir}.airports.csv^{airportQuals} view=latlon
      if alttestenv AND altwms ISFALSE then
        plot/id=plot_laywms ^{datDir}.world.prm
      else
        plot/id=plot_laywms {LT="WMS",IGNOREHREF=^ignoreHREF,^{OPTENTRIES.WMS_VERSION}URL="^url",^{OPTENTRIES.CRS}LAYERS="^layers",&
                             ConnectTimeout=30,ReadTimeout=120}|&
                            ^{datDir}.airports.csv^{airportQuals}    view=LatLon
      endif
      plot/id=plot_layimage  ^{datDir}.Sunset.jpg
      plot/id=plot_layveh    ^{datDir}.world.prm|^{datDir}.capital_cities.prm|{LT=Vehicle,CONEFILL=TRUE}
        set lay   reg.plot_layveh.layers.vehicle
        set lay.vehicle("MYVEH1") myveh1
        set lay.vehicle("MYVEH2") myveh2
        set lay.vehicle("MYVEH3") myveh3
        set lay.vehicle("MYVEH4") myveh4
      plot/id=plot_laydraw  ^{datDir}.fftdemo.prm|{LT="Drawable",name="VENN"} OPTIONS=+DBuffer
        set layVenn reg.plot_laydraw.layers.VENN
        feature ,, vennRed    {NAME="RED",   LINE={COLOR="#66FF0000"},LAYER=layVenn,TYPE=BOX|FILLED, X=1.5e6,Y=1.0E6,DX=1.5e6,DY=1.0e6} plot_laydraw
        feature ,, vennGreen  {NAME="GREEN", LINE={COLOR="#6600FF00"},LAYER=layVenn,TYPE=BOX|FILLED, X=2.0e6,Y=0.75E6,DX=1.5e6,DY=1.0e6} plot_laydraw
        feature ,, vennBlue   {NAME="BLUE",  LINE={COLOR="#660000FF"},LAYER=layVenn,TYPE=BOX|FILLED, X=2.5e6,Y=1.0E6,DX=1.5e6,DY=1.0e6} plot_laydraw
        feature ,, vennYellow {NAME="YELLOW",LINE={COLOR="#99FFFF00"},LAYER=layVenn,TYPE=VBAR|FILLED,X=4.5e6,Y=1.0E6,DX=0.5e6,DY=1.5e6} plot_laydraw
        feature ,, vennWhite  {NAME="WHITE", LINE={COLOR="#66AAAAAA"},LAYER=layVenn,TYPE=CIRC|FILLED,X=3e6,Y=0.75E6,DX=0.5e6,DY=0.5e6} plot_laydraw

      gcontrol button checkAutoscale "" "Check Auto Scale Min/Max"

    pipe off

  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 "CHECKAUTOSCALE" then
    res layshape_a1 reg.plot_layshape.layers.world.a1
    res laywms_a1   reg.plot_laywms.layers.wms.a1
    res layimage_a1 reg.plot_layimage.layers.sunset.a1
    res layveh_a1   reg.plot_layveh.layers.vehicle.a1
    res laydraw_a1  reg.plot_laydraw.layers.venn.a1
    res layshape_a2 reg.plot_layshape.layers.world.a2
    res laywms_a2   reg.plot_laywms.layers.wms.a2
    res layimage_a2 reg.plot_layimage.layers.sunset.a2
    res layveh_a2   reg.plot_layveh.layers.vehicle.a2
    res laydraw_a2   reg.plot_laydraw.layers.venn.a2
    assert/text="^reg.main.name: LayerShape auto scale min"             layshape_a1 eqt -90
    ! Since NeXtMidas 3.8.0 - The world.dbf and world.shp have changed so the values are slighly different- set tolerance to accept slighly different values.
    assert/text="^reg.main.name: LayerShape auto scale max"/tol=1e-3    layshape_a2 eqt  83.6022
    if alttestenv isFalse then
      assert/text="^reg.main.name: LayerWMS auto scale min"               laywms_a1   eqt -90
      assert/text="^reg.main.name: LayerWMS auto scale max"               laywms_a2   eqt  90.05
    else
      info "/ALTTESTENV so skipping LayerWMS auto scale min & max test"
    endif
    assert/text="^reg.main.name: LayerImage auto scale min"             layimage_a1 eqt -0.999167
    assert/text="^reg.main.name: LayerImage auto scale max"             layimage_a2 eqt  0.000833333
    assert/text="^reg.main.name: LayerVehicle auto scale min"           layveh_a1   eqt -90
    assert/text="^reg.main.name: LayerVehicle auto scale max"           layveh_a2   eqt  90
    assert/text="^reg.main.name: LayerDrawable auto scale min"          laydraw_a1  eqt  250000
    assert/text="^reg.main.name: LayerDrawable auto scale max"          laydraw_a2  eqt  1750000

    
  elseif msg.name eqs "ZOOM" THEN

  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^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

!************************************************************************************!
!
procedure setupLayerVehicle
  ! These are just good test cases. The first one is for DirecTV, the second
  ! is for Sirius. These are just approximate numbers that I found on the web,
  ! not anything useful beyond this test. The SPACEWAY-1 is a good test case
  ! where the horizon line crosses 180/-180 lat. The Radiosat 1 is a good test
  ! case that is not on the equator.

  set myveh1 {POINTS={}}
  set myveh1.attributes     {COLOR=Yellow,Symbol=Cross}
  set myveh1.location       {LABEL="SPACEWAY-1",POS={ALT=3.49E7,LAT=0,LON=-102.8}}
  set myveh1.location.ATTRIBUTES {SYMBOL=DIAMOND}
  set myveh1.site           {LABEL="El Segundo, CA",POS={ALT=0,LAT=33'55'17,LON=-118'24'22}}
  set myveh1.points.UPLINK  {POS={ALT=0,LAT=33'55'17,LON=-118'24'22},CANGLE=0.4,LABEL=""}
  set myveh1.points.UPLINK.ATTRIBUTES {SYMBOL=Plus,Color=Purple}

  set myveh2 {POINTS={}}
  set myveh2.attributes     {COLOR=Cyan,Symbol=Cross}
  set myveh2.location       {LABEL="Radiosat 1",POS={ALT=1.79E7,LAT=63.8,LON=-100}}
  set myveh2.location.ATTRIBUTES {SYMBOL=DIAMOND}
  set myveh2.site           {LABEL="Vernon, NJ",POS={ALT=0,LAT=41.2,LON=-74.5}}
  set myveh2.points.UPLINK  {POS={ALT=0,LAT=41.2,LON=-74.5},CANGLE=0.4,LABEL=""}
  set myveh2.points.UPLINK.ATTRIBUTES {SYMBOL=Plus,Color=Blue}

  ! This one tests Bug 1280 with respect to a clipping problem near the poles
  set myveh3 {POINTS={}}
  set myveh3.attributes     {COLOR=Magenta,Symbol=Cross}
  set myveh3.location       {LABEL="BUG1280",POS={ALT=3.49E7,LAT=10,LON=0}}
  set myveh3.site           {ATTRIBUTES={VISIBLE=FALSE}}

  ! This one tests Bug 1404 with respect to symbol angle
  set myveh4 {POINTS={}}
  set myveh4.attributes     {COLOR=Orange,Symbol=Aero,SymbolSize=12,SymbolFill=TRUE}
  set myveh4.location       {LABEL="AIRPLANE",POS={ALT=0,LAT=0,LON=0}} ! StateVector set after layer is up
  set myveh4.site           {ATTRIBUTES={VISIBLE=FALSE}}

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
