!******************************************************************************!
! Test case to confirm that LayerDrawable/LayerT5 can have a time window selected
!
! @bug      NXM-3515 Tests time visibility of points
!           NXM-3569 Incorrect point selected when track overlaps
!           NXM-3570 'Z' 256 ASCII Data Type
!           NXM-3577 Opening a pipe should not cause a repan
!           NXM-3578 Unable to zoom out after setting plot limits
!           NXM-3580 Plot onAxis readout sometimes disappears
!           NXM-3584 Plot of piped data flickering and disappearing when panning
! @since    3.9.3
! @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

  if interAct then
    set instructions "********         BOTTOM PLOT              ********\n&
                      1) Pan the lower plot to a new longitude range\n&
                      2) Click 'Add .. Layer' to get yellow diagonal points\n&
                      => the longitude range should stay the same\n&
                      3) Click 'Add .. Layer' again to get red scattered points\n&
                      4) Click 'HIDE' and then 'SHOW' for the diagonal pts\n&
                      5) Wait for the plot to fill up and then pan\n&
                      => Readouts and plot should not flicker or blank out\n&
                      6) Ctrl click 1 of the yellow points\n&
                      => should see pt selected and popup with 4Z format 'NOTE'\n&
                      ******** TOP PLOT has following ellipses: ********\n&
                      (HELLO) (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&
                      7)******* CLICK on the 'Time Window' button *******\n\n&
                      *******          BOTTOM PLOT              *******\n&
                      should see only limited # of red points kept\n\n&
                      ******* TOP PLOT: remaining ellipses are  *******\n&
                      (A) (TERRIFIC) (DAY) and JEFF JAY\n\n&
                      8) Open the plot layer menu and turn off CSV or PRM.\n&
                      => The associated ellipses should disappear.\n&
                      CSV: the name ellipses\n&
                      PRM: HELLO HAVE A TERRIFIC DAY EVERYONE!\n&
                      9) Right click on the top plot to zoom out\n\n&
                      FYI: The Maximize Map testing will be covered in &
                      a separate test, so you can skip that."

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

      nxm.sys.test.plotellipsetester/DIAGONAL/DIRECTCOLOR=YELLOW/NOFLAGS/NOTEMODETYPE=4Z/RT/RATE=500/STARTTIME="2007:01:02::00:00:00" OUT=_t5pipe{SIZE=10000} ELEM=Inf
      nxm.sys.test.plotellipsetester/BQ/COLOR=3/DIRECTCOLOR=PURPLE/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" ""
      gcontrol button showDiagonal   "Diagonal Points" "HIDE,SHOW" "SHOW" /tleft
      gcontrol button maxmap "Maximize Map on DRAGBOX zoom" "ON,OFF" "ON"
      gcontrol/nc=1 button cluster "" "Cluster"
      gcontrol/nc=1 button decluster "" "DeCluster"

      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}

    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.timepipe.clearData()
  elseif msg.name eqs "ADDLAYER" then
    if has1stLayer isFalse then
      invoke ,, reg.timepipe.openFile(_t5pipe{LAYER={^ellipselayerPIPE,FormRegions=true},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
    elseif has2ndLayer isFalse then
      invoke ,, reg.timepipe.openFile(_t5pipe2{LAYER={^ellipselayerPIPE2,FormRegions=true},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 "SHOWDIAGONAL" then
    if msg.data EQS "HIDE" then
      res reg.timepipe.layers.t5group2.getLayer(DIAG).setEnable(-GLOBAL)
    else
      res reg.timepipe.layers.t5group2.getLayer(DIAG).setEnable(+GLOBAL)
    endif
  elseif msg.name eqs "MAXMAP" then
    if msg.data EQS "ON" then
      invoke ,, reg.timepipe.MP.setMaxMapOnDragBox(^booleantrue)
    else
      invoke ,, reg.timepipe.MP.setMaxMapOnDragBox(^booleanfalse)
    endif
  elseif msg.fid eqs "TIMEPIPE"  then
    if msg.name eqs "DRAGBOX" then
      set pipebox.setBoundsFromTable(msg.data)
    endif
  elseif msg.name eqs "CLUSTER" then
    invoke ,, reg.timepipe.layers.t5group2.setShowClusters(^booleanTrue)
  elseif msg.name eqs "DECLUSTER" then
    invoke ,, reg.timepipe.layers.t5group2.setShowClusters(^booleanFalse)
  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

  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
