!******************************************************************************!
! Test case to cover various issues with LayerT5 point selection
! Note: The rate at which the points are generated affects how long it takes
! for the "track" to overlap on itself.  It also affects how long it takes before
! points wrap in the LayerT5 points buffer.  To have this test work as intended,
! the rate, points buffer size, and time before the track should have overlapped
! are all tied together.
!
! @issue    NXM-3566, NXM-3568, NXM-3569 various point selection issues
! @since    3.9.4
! @author:  Jeanette McCall
!******************************************************************************!
startmacro
  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)

  call setDatDir

  res test_blue_file ellipseflagsmeta5000.tmp
  res csvFile        nxm.sys.test.ellipseflagsmeta5000.csv
  res grpTable       nxm.sys.test.ellipseflagsmeta5000grp.tbl
  call createBlue

  res L:diagRate 100
  calc L:diagPtsBuffer diagRate 5 *
  calc popupTime diagRate 20.0 /

  if interAct then
    set instructions "********         TOP PLOT              ********\n\n&
                      1) Ctrl click the 'Hello' ellipse\n&
                      => you should see a popup\n\n&
                      2) Click 'Point Visibility Time Window'\n&
                      => the popup should go away\n\n&
                      3) Ctrl click the 'Jeff' ellipse\n&
                      => the Jeff ellipse should turn pink\n   &
                         and you should see a popup\n\n&
                      4) Click the 'Clear all layers'\n&
                      => the popup and points should go away\n\n\n&
                      ******** BOTTOM PLOT ********\n\n&
                      1) Click 'Add a piped layer'\n&
                      => a yellow diagonal of piped data should plot\n\n&
                      2) Wait until popup saying to click appears and goes away\n\n&
                      3) Ctrl click on one of the points\n&
                      => the point should turn aqua\n&
                      => a popup should also appear\n\n&
                      3) In a second, the diagonal track will overlap itself\n&
                      => the overriding point will be yellow,\n   &
                          but the popup will still be there.\n\n&
                      4) In a couple more seconds,the point buffer will wrap and replace the point\n&
                      => the popup will then go away\n&
                      "


    set ellipseColumnNames "D:ALT,D:LAT,D:LON,D:ETILT,D:EMIN,D:EMAJ,NAME,D:TIME,B:F1,B:F2,B:F3,B:F4,B:F5,B:F6,B:F7,B:F8,Z:NOTE"
    set ellipseLayerCSV "LT=T5,TimeOrder=TRUE,Group=t5Group,NAME=CSV,SIZE=6,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=PINK,X=LON,Y=LAT,Z=ALT,LABEL=NAME,TIMELF=TIME,METAPOS=METADATA"
    set qualsEllipseCSV {HEADERROWS=L:1,COLUMNNAMES="^ellipseColumnNames",LAYER={^ellipseLayerCSV},TAG=CSVFILE}
    set ellipselayerPRM "LT=T5,TimeOrder=True,Group=t5Group,NAME=PRM,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=AQUA"
    set ellipselayerPIPE "LT=T5,TimeOrder=False,Group=t5Group2,NAME=DIAG,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=AQUA,SELECTMESSAGES=TRUE"
    set ellipselayerPIPE2 "LT=T5,TimeOrder=False,Group=t5Group2,NAME=QUAD,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=WHITE,SELECTMESSAGES=TRUE"

    set has1stLayer false
    set has2ndLayer false

    invoke booleanfalse java.lang.Boolean.valueOf("false")
    invoke booleantrue java.lang.Boolean.valueOf("true")
    invoke origT5Setting   nxm.sys.libg.LayerT5.getAutoAdjustLineColor()
    invoke origPolySetting nxm.sys.libg.LayerPoly.getAutoAdjustLineColor()
    invoke ,, nxm.sys.libg.LayerT5.setAutoAdjustLineColor(^booleantrue)
    invoke ,, nxm.sys.libg.LayerPoly.setAutoAdjustLineColor(^booleantrue)

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

      nxm.sys.test.plotellipsetester/DIAGONAL/COLOR=4/NOTEMODETYPE=4Z/RT/RATE=^diagRate/STARTTIME="2007:01:02::00:00:00" OUT=_t5pipe{SIZE=^diagPtsBuffer} ELEM=Inf
      nxm.sys.test.plotellipsetester/BQ/COLOR=3/RT/NOTEMODETYPE=VA/RATE=700 OUT=_t5pipe2{SIZE=10000} ELEM=Inf

      plot/id=timepts ^{datDir}.world.prm|{LT="Drawable",name=t5Group,listlayers=true}|nxm.sys.test.ellipseflagstime5000.csv^{qualsEllipseCSV}|&
                             ^test_blue_file{LAYER={^ellipselayerPRM},TAG=PRMFILE} readoutExt=+SHOW|-FEATURES|+POINTS &
                             readout=+OnAxis|+AlwaysDeltas view=latloncontinuous

      plot/id=timepipe/msgid=1 ^{datDir}.world.prm|{LT="Drawable",name=t5Group2,listlayers=true} readoutExt=+SHOW|+POINTS &
               readout=+OnAxis|+AlwaysDeltas view=latloncontinuous Y1=-90 Y2=90

      gcontrol/nc=1 button TIMEWINDOW "" "Point Visibility Time Window"
      gcontrol/nc=1 button clearLayers "" "Clear all layers" ""
      gcontrol/nc=1 button ADDLAYER "" "Add a piped Layer" ""

      FEATURE label=pipebox table={NAME=PIPEBOX,TYPE=BOX,COLOR=AQUA,ENABLE=1} plot=timepipe

      message send timepts NAME=ZOOM INFO=0 DATA={XMIN=25,XMAX=125,YMIN=-20,YMAX=80}
      message send timepipe NAME=ZOOM INFO=0 DATA={XMIN=-70,XMAX=30,YMIN=-25,YMAX=25}

    pipe off

    invoke ,, nxm.sys.libg.LayerT5.setAutoAdjustLineColor(^origT5Setting)
    invoke ,, nxm.sys.libg.LayerPoly.setAutoAdjustLineColor(^origPolySetting)

  endif

    erase test_blue_file
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 eqss "TIMEWINDOW" then
    invoke time_window_fixed nxm.sys.libg.LayerT5.WINDOW_FIXED
    invoke ,, reg.timepts.layers.t5group.TimeWindow(2.224470131139E9,2.224470131161E9,time_window_fixed)
    invoke time_window_rel_pt nxm.sys.libg.LayerT5.WINDOW_RELATIVE_POINT
    invoke time_window_rel_cl nxm.sys.libg.LayerT5.WINDOW_RELATIVE_CLOCK
    if mode EQSS P
      res mode time_window_rel_pt
    else
      res mode time_window_rel_cl
    endif
    res pointTimeout 2
    invoke ,, reg.timepipe.layers.t5group2.TimeWindow(^pointTimeout,-1,mode)
  elseif msg.name EQS "CLEARLAYERS" then
    invoke ,, reg.timepts.clearData()
    invoke ,, reg.timepipe.clearData()
  elseif msg.name eqs "ADDLAYER" then
    if has1stLayer isFalse then
      invoke ,, reg.timepipe.openFile(_t5pipe{LAYER={^ellipselayerPIPE},TAG=DIAGPIPE})
      keyword _t5pipe get name=VER value=blueVer
      assert/text="^reg.main.name: T5000 pipe with 'Z' 256 ASCII data is BLUE VER 1.2" blueVer EQS blueVer
      set has1stLayer true
      pause ^popupTime
      popup/id=ptclick/msgid=main Result=dontcare FUNC=OK message="Click yellow point" type=info
      pause 2
      message send ptclick ,, cancel
    elseif has2ndLayer isFalse then
      invoke ,, reg.timepipe.openFile(_t5pipe2{LAYER={^ellipselayerPIPE2},TAG=QUADPIPE})
      keyword _t5pipe get name=VER value=blueVer
      assert/text="^reg.main.name: T5000 pipe WITHOUT 'Z' data is BLUE VER 1.1" blueVer EQS blueVer
      set has2ndLayer true
    else
      say "no more layers to add"
    endif
  elseif msg.name eqs "POPUP" then
  elseif msg.fid eqs "TIMEPIPE"  then
    if msg.name eqs "DRAGBOX" then
      set pipebox.setBoundsFromTable(msg.data)
    endif
  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
res msg.name
  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 blue file for this test
!*****************************************************************************
procedure createBlue

  %CONVERT_CSV_TO_T5000 ^csvFile ^grpTable ^test_blue_file rf=ecr

return
