!******************************************************************************!
! Test case to confirm that LayerT5 can handle type CSV files as well as Type 5000
!
!
! @bug      NXM-3500 Plot XYZ View - add support for brining in XYZ points via CSV files
!           NXM-3510 Tests wrapping of geodetic data points
!           NXM-3515 Tests time visibility of points
!           NXM-3682 Circular Code in call to MPlot/LayerT5 setTime(double)
!
! @since    3.9.3
! @author:  Kevin Esterheld / 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

  if interAct then
    set instructions "CSV plots (left) match the BLUE plots (right),\n&
                      EXCEPT BLUE is missing:\n&
                      JENNIFER ellipse\n&
                      1 of the JEREMY ellipses\n\n&
                      Ctrl-LMB on 1 of the points in the bottom plots to bring up a popup describing the point\n\n&
                      Click on the 'Time Window' button\n&
                      JEREMY & ANDREW ellipses should disappear\n\n&
                      FYI: This tests time windowing and the removal of oldest points by time"
    set qualsCone {HEADERROWS=L:1,COLUMNNAMES="D:X,D:Y,D:Z,D:POSPX,D:POSPY,D:POSPZ,D:CONEA,D:CONEB,D:CONEANGLE,NAME", LAYER={LT="T5"}} x1=-25, x2=125
    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,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=PINK,X=LON,Y=LAT,Z=ALT,LABEL=NAME,TIMELF=TIME"
    set qualsEllipseCSV {HEADERROWS=L:1,COLUMNNAMES="^ellipseColumnNames",LAYER={^ellipseLayerCSV}}
!    invoke ,, nxm.sys.libg.LayerT5.setFixedSetSize("true") ! no longer needed with latest changes 5/15/20
    set ellipselayerPRM "LT=T5,TimeOrder=True,SIZE=6,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,,SELECTCOLOR=AQUA"
    
    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 14 /title
      plot/id=conecsv ^{datDir}.world.prm|nxm.sys.test.cone5000.csv^{qualsCone}
      plot/id=coneprm ^{datDir}.world.prm|^{datDir}.cone5000.prm
      plot/id=elllipcsv ^{datDir}.world.prm|nxm.sys.test.ellipseflagstime5000.csv^{qualsEllipseCSV}
      plot/id=elllipprm ^{datDir}.world.prm|nxm.sys.test.ellipseflagstime5000.prm{LAYER={^ellipselayerPRM}}
      gcontrol/nc=1 button TIMEWINDOW "" "Point Visibility Time Window"
      gcontrol/nc=1 button SETTIME "" "Set Time on CSV File Test"
      message send conecsv NAME=ZOOM INFO=0 DATA={XMIN=2.5,XMAX=10,YMIN=-1,YMAX=8}
      message send coneprm NAME=ZOOM INFO=0 DATA={XMIN=2.5,XMAX=10,YMIN=-1,YMAX=8}
      message send elllipcsv NAME=ZOOM INFO=0 DATA={XMIN=25,XMAX=125,YMIN=-40,YMAX=40}
      message send elllipprm NAME=ZOOM INFO=0 DATA={XMIN=25,XMAX=125,YMIN=-40,YMAX=40}

      res conecsv    reg.conecsv.layers.cone5000
      res ellipsecsv reg.elllipcsv.layers.ellipseflagstime5000
    pipe off
!    invoke ,, nxm.sys.libg.LayerT5.setFixedSetSize("false")

  endif
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.elllipcsv.layers.ellipseflagstime5000.TimeWindow(2.224470131139E9,2.224470131161E9,time_window_fixed)
    invoke ,, reg.elllipprm.layers.ellipseflagstime5000.TimeWindow(2.224470131139E9,2.224470131161E9,time_window_fixed)
  elseif msg.name eqss "SETTIME" then
    res d:jaytime 2.22447013116E9 ! tests 1950 time - value > (86400 * 100)
    set ellipsecsv.time jaytime
    set conecsv.time 0            ! test seconds since midnight time
    invoke ellipsetime ellipsecsv.time
    assert/text="^reg.main.name: setTime() on CSV file doesn't result in NPE or circular code" jaytime EQ ellipsetime
  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
