!******************************************************************************!
! Test case to confirm that LayerDrawable/LayerT5 can have a time window selected
!
! Argument: mode (either POINT or CLOCK)
!
! @bug      NXM-3515 Tests time visibility of points
! @since    3.9.3
! @author:  Jeanette McCall
!******************************************************************************!
startmacro s:mode[POINT]
  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

  if interAct then
    set instructions "********         BOTTOM PLOT              ********\n&
                      2 pipes of T5000 pts (red, yellow)\n\n&
                      ******** TOP PLOT has following ellipses: ********\n&
                      HELLO\n&
                      HAVE A TERRIFIC DAY EVERYONE!\n&
                      3 (JEREMY), a (JEFF), a (JAY), and an (ANDREW)\n\n&
                      Because of a time based size limit on points,\n&
                      both the JENNIFER and 1 of the JEREMY ellipses are missing\n\n&
                      ******* CLICK on the 'Time Window' button *******\n\n&
                      *******          BOTTOM PLOT              *******\n&
                      only limited history of pts kept\n\n&
                      ******* TOP PLOT: remaining ellipses are  *******\n&
                      A TERRIFIC DAY\n&
                      (JEFF) (JAY)\n\n&
                      Open the plot layer menu and turn off CSV or PRM.\n&
                      The associated ellipses should disappear.\n&
                      CSV: HELLO HAVE A TERRIFIC DAY EVERYONE!\n&
                      PRM: the name ellipses\n\n&
                      FYI: This tests time windowing and the removal of oldest points by time"

    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"
    set ellipseLayerCSV "LT=T5,TimeOrder=TRUE,Group=t5Group,NAME=CSV,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,SIZE=6,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=AQUA"
    set ellipselayerPIPE "LT=T5,TimeOrder=False,Group=t5Group2,NAME=DIAG,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=AQUA,SELECTMESSAGES=TRUE,ColorFade=true,ColorFadeFactor=2"
    set ellipselayerPIPE2 "LT=T5,TimeOrder=False,Group=t5Group2,NAME=QUAD,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=WHITE,SELECTMESSAGES=TRUE,ColorFade=true,ColorFadeFactor=5"
    
    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 30 /title

      nxm.sys.test.plotellipsetester/DIAGONAL/DIRECTCOLOR=YELLOW/NOFLAGS/RT/RATE=20/TL=1 OUT=_t5pipe{SIZE=500} ELEM=Inf
      nxm.sys.test.plotellipsetester/BQ/COLOR=3/DIRECTCOLOR=PURPLE/RT/RATE=50/TL=1 OUT=_t5pipe2{SIZE=500} ELEM=Inf

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

      plot/id=timepipe ^{datDir}.world.prm|{LT="Drawable",name=t5Group2,listlayers=true}|_t5pipe{LAYER={^ellipselayerPIPE},TAG=DIAGPIPE}|&
                              _t5pipe2{LAYER={^ellipselayerPIPE2},TAG=QUADPIPE} readoutExt=+SHOW|-FEATURES|+POINTS readout=+OnAxis view=latloncontinuous

      gcontrol/nc=1 button TIMEWINDOW "" "Point Visibility Time Window"
      gcontrol/nc=1 button clearLayers "" "Clear all layers" ""
      message send timepts NAME=ZOOM INFO=0 DATA={XMIN=25,XMAX=125,YMIN=-20,YMAX=80}

    pipe off

  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.timepipe.clearData()
  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
