!******************************************************************************!
! Tests:
! 1) the counting of points of layerT5s within LayerDrawable LayerDrawable
! 2) the clustering of points and region groups
! 3) Feature TEXT placement on IconSymbols
!
! @bug      NXM-3512 Count points in an area on map
!           NXM-2968 Support for mapping points as clusters
! @since    3.9.3 / 3.9.4
! @author:  Kevin Esterheld / Jeanette McCall
!******************************************************************************!

startmacro

call setDatDir

switch INTERACTIVE interAct          GET DEF=TRUE  SDEF=TRUE
switch BASELINE     baseline         GET DEF=FALSE SDEF=TRUE
switch KEEP         keep             GET DEF=FALSE SDEF=TRUE

set expPointCount  8
set expEllipsePointCount 9
invoke L:ellipseField nxm.sys.libg.LayerT5.ELLIPSES
invoke L:symbolsField nxm.sys.libg.LayerT5.SYMBOLS
calc l:symbols_and_ellipses symbolsField ellipseField +

res test_blue_file pointsflags5000num.tmp
res csvFile        nxm.sys.test.pointsflags5000num.csv
res grpTable       nxm.sys.test.pointsflags5000numgrp.tbl
call createBlue

if interAct then

  set qualspoints1 {HEADERROWS=L:1,COLUMNNAMES="D:ALT,D:LAT,D:LON,D:ETILT,D:EMIN,D:EMAJ,NAME,B:F1,B:F2,B:F3,B:F4,B:F5,B:F6,B:F7,B:F8", LAYER={LT="T5",X="LON",Y="LAT",Z="ALT",LABEL="NAME",NAME=name_ellipses,group=t5Group}}
  set qualspoints2 {HEADERROWS=L:1,COLUMNNAMES="D:ALT,D:LAT,D:LON,D:ETILT,D:EMIN,D:EMAJ,NAME,COLOR,NOTE", LAYER={LT="T5",X="LON",Y="LAT",Z="ALT",LABEL="NAME",NAME=num_ellipses,group=t5Group}}
  set prmLayerInfo "LT=T5,Group=t5Group,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=WHITE"
  set instructions "Top Left Plot:    6 features, 30 points\n\n&
                    Bottom Left Plot: 4 features, 29 points\n\n&
                    Top Right Plot:   3 features\n\n&
                    Top Left Plot:    3 features, labels still\n                  &
                                      near Arrow and Network\n\n& 
                    ---- NOW CLICK 'Cluster' -----------\n\n&
                    None of the plots have point readouts\n\n&
                    Top Left Plot:    yellow 18 cluster\n\n&
                    Bottom Left Plot: blue 7 cluster, yellow 11 cluster\n\n&
                    Top Right Plot:   blue 9 cluster\n\n&
                    Top Left Plot:    blue 7 cluster, points\n                  &
                                      '8' + '9' are drawn\n\n& 
                    ---- NOW CLICK 'Decluster' ----------\n\n&
                    Points should be back to original state."

  res z:boolTrue  true
  res z:boolFalse false

  pipe on
    panel/msgid=MAIN/setup/controls=gc
    plot/mousewheelzoom/usemouseposition/id=fixed ^{datDir}.world.prm|{LT="Drawable",name=t5Group,listlayers=true}|&
                                                ^{datDir}.coneflags5000.prm{Layer={NAME=cones,group=t5Group}}|&
                                                ^{datDir}.ellipseflags5000.prm{Layer={NAME=ellipses,group=t5Group}}|&
                                                nxm.sys.test.pointsflags5000.csv^qualsPoints1|&
                                                nxm.sys.test.pointsflags5000num.csv^qualsPoints2 &
                                                 x1=-180 x2=180 y1=-90 y2=90 options="-BStore" &
                                                 readoutExt=+SHOW|+FEATURE|+POINTS readout=+OnAxis view=LATLONCONTINUOUS
    plot/mousewheelzoom/usemouseposition/id=fixedzoom ^{datDir}.world.prm|{LT="Drawable",name=t5Group,listlayers=true}|&
                                                ^{datDir}.coneflags5000.prm{Layer={NAME=cones,group=t5Group}}|&
                                                ^{datDir}.ellipseflags5000.prm{Layer={NAME=ellipses,group=t5Group}}|&
                                                nxm.sys.test.pointsflags5000.csv^qualsPoints1|&
                                                nxm.sys.test.pointsflags5000num.csv^qualsPoints2 &
                                                 x1=10 x2=120 y1=-35 y2=50 options="-BStore" &
                                                 readoutExt=+SHOW|+FEATURE|+POINTS readout=+OnAxis  view=LATLONCONTINUOUS
    plot/mousewheelzoom/usemouseposition/id=t5only ^{datDir}.world.prm|&
                                                 test_blue_file{LAYER={^prmLayerInfo,NAME=t5only}} &
                                                 readoutExt=+SHOW|+FEATURE readout=+OnAxis view=LATLONCONTINUOUS
    plot/mousewheelzoom/usemouseposition/id=t5onlyzoom ^{datDir}.world.prm|&
                                                 test_blue_file{LAYER={^prmLayerInfo,NAME=t5zoom}} &
                                                 x1=10 x2=120 y1=-35 y2=50 options="-BStore" &
                                                 readoutExt=+SHOW|+FEATURE readout=+OnAxis view=LATLONCONTINUOUS

    gcontrol label macro "MACRO"
    gcontrol menu    main "Exit with Status = " "PASS,FAIL" /tleft
    set gc.main.itemColors {PASS=GREEN,FAIL=RED}
    gcontrol text idtext "Instructions" ^instructions 24 30 /title

    invoke drawableLayer reg.fixed.layers.T5GROUP
    invoke drawableLayerzoom reg.fixedzoom.layers.T5GROUP
    res t5LayerOnly reg.t5only.layers.t5only
    res t5LayerOnlyZoom reg.t5onlyzoom.layers.t5zoom
    invoke conesLayer reg.fixed.layers.T5GROUP.getDrawable(cones)
    invoke ellipsesLayer reg.fixed.layers.T5GROUP.getDrawable(ellipses)

    gcontrol/nc=1 button cluster "" "Cluster"
    gcontrol/nc=1 button decluster "" "DeCluster"
    gcontrol/nc=1 button addpoint "" "Add Point to Bottom Right Plot"
    gcontrol/nc=1 button symbols "Symbols Only - Bottom Right Plot" "TRUE,FALSE" "FALSE" /tleft
   ! gcontrol/nc=1 button  symbols   main "Exit with Status = " "PASS,FAIL" /tleft
    call add_feature_TRIANGLE

  pipe off
endif
erase test_blue_file
endmacro

!******************************************************************************!
procedure processMessage m:msg
  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name: Interactive tests" msg.data eqss "PASS"
    pipe stop 
  elseif msg.name eqs "ZOOMRIGHT" then
    message sendw id=pipe name=ZOOM data={xmin=-79,xmax=-72,ymin=36,ymax=44}
  elseif msg.name eqs "CLUSTER" then
    invoke ,, drawableLayer.setShowClusters(boolTrue)
    invoke ,, drawableLayerzoom.setShowClusters(boolTrue)
    invoke ,, t5LayerOnly.setShowClusters(boolTrue)
    invoke ,, t5LayerOnlyZoom.setShowClusters(boolTrue)
  elseif msg.name eqs "DECLUSTER" then
    invoke ,, drawableLayer.setShowClusters(boolFalse)
    invoke ,, drawableLayerzoom.setShowClusters(boolFalse)
    invoke ,, t5LayerOnly.setShowClusters(boolFalse)
    invoke ,, t5LayerOnlyZoom.setShowClusters(boolFalse)
  elseif msg.name eqs "ADDPOINT" then
    call add_point
  elseif msg.name eqs "SYMBOLS" then
    res z:symbolsOnly msg.data
    if symbolsOnly then
      res l:field symbolsField
    else
      res l:field symbols_and_ellipses
    endif
    invoke ,, t5LayerOnlyZoom.setField(field)
  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

!******************************************************************************
procedure testCitiesPlot
  message sendw id=cities name=ZOOM data={xmin=-79,xmax=-72,ymin=36,ymax=44}
  invoke pointCount reg.cities.layers.T5GROUP.getNumVisiblePoints()
  assert/text="^reg.main.name: point count should equal ^{expPointCount}" expPointCount eq pointCount
  invoke reg.cities.zoomOut(-1)
  invoke citiesLayer.setEnable(-Global)
  pause .25 !need to let the layer draw again. give a pause for the test
  invoke pointCount reg.cities.layers.T5GROUP.getNumVisiblePoints()
  assert/text="^reg.main.name: point count should equal ^{expEllipsePointCount}" expEllipsePointCount eq pointCount
  invoke citiesLayer.setEnable(+Global)
return
!******************************************************************************!
procedure setInstructions s:msgString
  set reg.panel.controls.idtext.value msgString
return
!******************************************************************************!
procedure add_feature_TRIANGLE
  feature label=ftriangle1 table={NAME="TRIANGLE1",TYPE=SYMBOL,X=-100,Y=-40,&
                                  COLOR=GREEN,LINE={SYMBOL="UTRI"}} plot=fixed
  feature label=ftriangle2 table={NAME="TRIANGLE2",TYPE=SYMBOL,X=-100,Y=-20,&
                                  COLOR=RED,LINE={SYMBOL="LTRI"}} plot=fixed
  feature label=ftriangle3 table={NAME="TRIANGLE3",TYPE=SYMBOL,X=20,Y=15,&
                                  COLOR=YELLOW,LINE={SYMBOL="DTRI"}} plot=fixed
  feature label=ftriangle4 table={NAME="TRIANGLE4",TYPE=SYMBOL,X=20,Y=10,&
                                  COLOR=AQUA,LINE={SYMBOL="RTRI"}} plot=fixed
  feature label=ftriangle5 table={NAME="TRIANGLE5",TYPE=SYMBOL,X=20,Y=5,&
                                  COLOR=WHITE,LINE={SYMBOL="TRI"}} plot=fixed
  feature label=ftriangle1 table={NAME="TRIANGLE1",TYPE=SYMBOL,X=-100,Y=-40,&
                                  COLOR=GREEN,LINE={SYMBOL="UTRI"}} plot=fixedzoom
  feature label=ftriangle2 table={NAME="TRIANGLE2",TYPE=SYMBOL,X=-100,Y=-20,&
                                  COLOR=RED,LINE={SYMBOL="LTRI"}} plot=fixedzoom
  feature label=ftriangle3 table={NAME="TRIANGLE3",TYPE=SYMBOL,X=20,Y=15,&
                                  COLOR=YELLOW,LINE={SYMBOL="DTRI"}} plot=fixedzoom
  feature label=ftriangle4 table={NAME="TRIANGLE4",TYPE=SYMBOL,X=20,Y=10,&
                                  COLOR=AQUA,LINE={SYMBOL="RTRI"}} plot=fixedzoom
  feature label=ftriangle5 table={NAME="TRIANGLE5",TYPE=SYMBOL,X=20,Y=5,&
                                  COLOR=WHITE,LINE={SYMBOL="TRI"}} plot=fixedzoom
  feature label=diagline   table={NAME="DLINE",TYPE=DLINE,X=30,Y=-30,&
                                  DX=20,DY=30,COLOR=WHITE} plot=fixed
  feature label=diagline   table={NAME="DLINE",TYPE=DLINE,X=30,Y=-30,&
                                  DX=20,DY=30,COLOR=WHITE} plot=fixedzoom
  feature label=diagline   table={NAME="DLINE",TYPE=DLINE,X=25,Y=10,&
                                  DX=-20,DY=30,COLOR=AQUA} plot=t5only
  feature label=diagline   table={NAME="DLINE",TYPE=DLINE,X=25,Y=10,&
                                  DX=-20,DY=30,COLOR=AQUA} plot=t5onlyzoom
  feature label=icon       table={NAME="TOP",TYPE=SYMBOL|TEXT|TTAG,X=0,Y=20,&
                                  LINE={SYMBOL="^{datDir}.Arrow.png"}} &
                                  plot=t5only
  feature label=icon       table={NAME="TOP",TYPE=SYMBOL|TEXT|TTAG,X=0,Y=20,&
                                  LINE={SYMBOL="^{datDir}.Arrow.png"}} &
                                  plot=t5onlyzoom
  feature label=iconcustom table={NAME="TR",TYPE=SYMBOL|TEXT|CUSTOMTAG,X=125,Y=-25,&
                                  LINE={SYMBOL="^{datDir}.network.png"}} &
                                  plot=t5only
  feature label=iconcustom table={NAME="TR",TYPE=SYMBOL|TEXT|CUSTOMTAG,X=125,Y=-25,&
                                  LINE={SYMBOL="^{datDir}.network.png"}} &
                                  plot=t5onlyzoom
return

!******************************************************************************!
procedure add_point

  res t:pntTable {POS={ALT=0.0,LAT=-26.1,LON=70.0},ELPS=(300000.0,70000.0,0),NAME=ADDED_PT,COLOR=ORANGE,NOTE=E}
  !res t:pntTable {POS={ALT=0.0,LAT=-25.4,LON=65.0},ELPS=(106638538.6571,84324501.3325,0),NAME=ADDED_PT,COLOR=ORANGE,NOTE=E}
  invoke ,, t5LayerOnlyZoom.setPoint(-1,pntTable)

return

!*****************************************************************************
! create blue file for this test
!*****************************************************************************
procedure createBlue

  %CONVERT_CSV_TO_T5000 ^csvFile ^grpTable ^test_blue_file rf=ecr

return
