!******************************************************************************!
! Tests Range expansion on LayerT5 Points with XY view
!
! Arguments: None
!
! Issue:   NXM-3913 LayerT5 XY Support
!          NXM-3921 LayerT5/LayerDrawable SELECT and DESELECT messages
!                   need to be consistent
!          NXM-4115 LayerT5 symbols on raster plot issue
! Since:   NeXtMidas 4.1.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 raster_file "xyz_raster.tmp"
  noop apenny raster_file
  headermod raster_file xs=3.8657833e7
  invoke doubleMax java.lang.Double.MAX_VALUE
  res ptTime 3.8657840e7
  res timeInc 10
  res ptAlt 5
  res altInc 1
  res symbolUp 0
  res symbolOver 6
  timex 2023:08:26::00:00:25 wsec=startTime
  res frametime startTime
  res jaynum 1


  set t:ellipse5000_data {}
  set t:ellipse5000_data.0 {POS=(0,0,0),POSP=(0.0,0,0),NAME="TEMP"}
  file open/n out ram:xyz_starter.tmp{COMPONENTS=(POS/VD,POSP/VD,NAME/9A),SIZE=1}
    invoke ,, out.setData(0,ellipse5000_data.0)
  file close out

  if interAct then
    set instructions "Click 'Manually Add First Points'\n&
                      then\n&
                      Click 'Add Another Point' multiple times\n\n&
                      Expected:\n&
                      1) The plot should be full and NOT slammed to either side.\n&
                      2) The plot should have new points added across the bottom from left to right.\n&
                      3) The plot should NOT expand range once new points reach the right side. &
                      Any additional points added should get cut off.\n\n&
                      Next IN APENNY PLOT:\n&
                      1) select a point\n&
                      Expected: a SELECT message in the SHELLGUI\n&
                      2) select a different point\n&
                      Expected:\n&
                      1) a DESELECT message for the previous point\n&
                      2) and a SELECT message for the new point."


    set layerXYZ    "LT=T5,NAME=XYZDATA,DATATYPE="XYZ",IgnoreRange=true,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=BLUE,SELECTMESSAGES=TRUE"

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

      plot/id=xyzplot/msgid=main raster_file|xyz_starter.tmp{Layer={^layerXYZ}}  readoutExt=+SHOW|-FEATURES|+POINTS readout=+OnAxis|-Scientific|+DateString

      gcontrol label macro "Global Controls"
      gcontrol/nc=1 button SETPOINTS "" "Manually Add First Points"
      gcontrol/nc=1 button ADDPOINT "" "Add Another Point"
      shellgui/id=idshellgui/attach/rows=15

    pipe off

  endif
  erase raster_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 "MARK"
  elseif msg.name eqss "SETPOINTS"
    call addPoint3DSymbols
  elseif msg.name eqss "ADDPOINT"
    call addNextSquarePoint
  elseif msg.name EQS "SELECT" OR msg.name EQS "DESELECT" then
   info "^msg.name MESSAGE"
   say "^msg.name ^msg.data"
  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

!*****************************************************************************
! add Point3D symbols to symbols layer
!*****************************************************************************
procedure addPoint3DSymbols
  res t:labels {POS={X="Time",Y="FREQ",Z=""},POSP={X="",Y="",Z=""}}
  res xyzLay reg.xyzplot.layers.xyzdata
  !set Point Margin
  set reg.xyzplot.xunits TIME
  set reg.xyzplot.yunits ALT
  set reg.xyzplot.xaxislabel "Time"
  invoke ,, xyzLay.setXYPointYMargin(20)
  invoke ,, xyzLay.setXYPointXMargin(0.00000001)
  ! delete "starter" point
  invoke ,, xyzLay.clear()
  invoke ,, xyzLay.setPopupLabels(labels)

  ! add symbol points
  calc pospX ptTime symbolOver +
  res t:symTable {POS={Y=^{ptAlt},X=^{ptTime}},POSP={Y=^{ptAlt},X=^{pospX}},NAME="HI",SYMBOL="Square"}
  invoke ,, xyzLay.setPoint(-1,symTable)
  invoke firstPoint xyzLay.getPoint(0)
  res firstPoint.name "SquareCopy"
  invoke ,, xyzLay.setPoint(-1,firstPoint)
  ! TODO: ADD TEST that grabs points 0 & 1 and makes sure POS & POSP are as expected
  ! also should test case where just icons without POSP
  call addNextSquarePoint

return

!*****************************************************************************
! Add next square point
!*****************************************************************************
procedure addNextSquarePoint

  calc ptAlt ptAlt altInc +
  calc ptTime ptTime timeInc +
  calc pospX ptTime symbolOver +
  calc pospY ptAlt symbolUp +
  res t:symTable {POS={Y=^{ptAlt},X=^{ptTime}},POSP={Y=^{pospY},X=^{pospX}},COLOR=AQUA,NAME="Jay^{jaynum}",SYMBOL="Square"}
  invoke ,, xyzLay.setPoint(-1,symTable)
  !invoke listPts xyzLay.getPointsAsList()
  !res listPts
  calc jaynum jaynum 1 +

return
