!******************************************************************************!
! The goal of this test case is to have a GUI that is more representative of
! the more current usage of NeXtMidas being embedded in a Java Swing GUI
!
! Arguments: throttle - "FULL" or "REAL"
! Note: This test case needs instructions and list of switches, including those for include file
!
! @bug      NXM-3213 Graphics severely degraded on CentOS 7
! @since    3.9.2 - improved version of test_plot_whframe
! @author:  Jeanette McCall
!******************************************************************************!
startmacro S:throttle["REAL"]
  switch INTERACTIVE  interAct     GET DEF=TRUE  SDEF=TRUE  ! for normal interactive test(s)
  switch COREGRAPHICS coreGraphics GET DEF=FALSE SDEF=TRUE  ! for a quick check of graphics - more extreme tests

  invoke z:jvmHangFix nxm.sys.lib.Shell.getJVMHangWorkaround()
  res z:mytrue true
  invoke ,, nxm.sys.lib.Shell.setJVMHangWorkaround(^mytrue)

  if interAct or coreGraphics then
    switch "SRATE" sr    get DEF=1e5
    switch "OVER"  over  get DEF=0.5
    switch "SFREQ" sfreq get DEF=sr/10
    switch "AMP"   amp   get DEF=1e4
    switch "NAVG"  navg  get DEF=4
    switch "NFFT"  nfft  get DEF=2k
    switch "SAVEFILE" saveFile DEF=FALSE SDEF=TRUE
    switch "LOGLEVEL" logLevel DEF=INFO  SDEF=INFO

    call setDatDir

    res oldLookAndFeel env.theme
    lookandfeel Ocean

    env get IOOptions origIOOptions
    !env set IOOptions +FixPipeRestartTimeLines - no longer needed in 3.9.4

    set format sf
    set sfreq sr/10
    set chirpamt sfreq/4
    set noiseamp 1e2
    set numelem 10e10
    set firstClick true
    invoke tcurObj nxm.sys.lib.Time.currentTime()
    invoke tcur tcurObj.getSec()
    invoke tstring tcurObj.toString(9)
    res S:freqStr ^sfreq

    res basewaveid         wave
    res basewavename      _wave
    res basefftid          fft
    res basefftname       _fft
    res baserplotid       "plot_raster_"
    res baselplotid       "plot_line_"
    res baserbox1id       rbox1
    res baserbox2id       rbox2
    res baserbox3id       rbox3
    res baselboxid        lbox
    res basercircleid     rcircle
    res baserelpsid       relps
    res basepipemonitorid pmon
    res basefilename      nxm3213psd
    res l:dragbox_cnt 0

    pipe on
      include "datasim.inc"

      panel/jsetup="nxm.sys.test.test_plot_wh_gui"/controls=gc
      !res /all

      ! psd plot goes on display 1
      invoke firstRaster baserplotid.concat("T1")
      invoke firstLine baselplotid.concat("T1")
      plot/theme=WoB/ID=^firstRaster _psdThin  TYPE=Raster
      plot/theme=WoB/ID=^firstLine _psdThin  TYPE=Line
      gcontrol/id=PMonT1 pipemon PMonT1 _psdThin

      ! fft plots go on displays 2-11
      call add_new_fft_plots T2 format numelem cos sfreq amp sr 0 nfft navg
      call add_new_fft_plots T3 format numelem cos sfreq amp sr chirpamt nfft navg
      call add_new_fft_plots T4 format numelem sin sfreq amp sr 0 nfft navg
      call add_new_fft_plots T5 format numelem sin sfreq amp sr chirpamt nfft navg
      call add_new_fft_plots T6 format numelem lrs sfreq amp sr 0 nfft navg
      call add_new_fft_plots B1 format numelem triangle sfreq amp sr 0 nfft navg
      call add_new_fft_plots B2 format numelem triangle sfreq amp sr chirpamt nfft navg
      call add_new_fft_plots B3 format numelem sawtooth sfreq amp sr 0 nfft navg
      call add_new_fft_plots B4 format numelem sawtooth sfreq amp sr chirpamt nfft navg
      call add_new_fft_plots B5 format numelem square sfreq amp sr 0 nfft navg
      call add_new_fft_plots B6 format numelem square sfreq amp sr chirpamt nfft navg

      plot/theme=WoB/ID=WORLD_MAP ^{datDir}.world.shp{LAYER={LABEL=NAME}} options=+crosshairs

      feature label=aircraft table={NAME="AIRCRAFT",TYPE="SYMBOL",X=15,Y=15,COLOR="GREEN",&
                                    SYMBOL="AERO",SYMBOLFILL="TRUE",SYMBOLHEADING=90,SYMBOLSIZE=15} plot=WORLD_MAP

      res gc.throttleField.value throttle 

      set aircraft reg.WORLD_MAP.features.aircraft
      call add_feature_ellipse WORLD_MAP
      set this.timers   1
      set this.timer(0) 0.5
    pipe off

    lookandfeel oldLookAndFeel
  endif

  invoke ,, nxm.sys.lib.Shell.setJVMHangWorkaround(^jvmHangFix)
endmacro


!******************************************************************************!
! Main process message for this macro. Should be right after the ENDMACRO.
! @param msg The message received.
!******************************************************************************!
procedure processMessage m:msg
  !say "Got message name=^msg.name info=^msg.info data=^msg.data fid=^msg.fid"
  if msg.name EQS "PASSBUTTON" then
    assert/text="^reg.main.name: User-indicated tests" 0 EQ 0
    pipe stop
  elseif msg.name EQS "FAILBUTTON" then
    assert/text="^reg.main.name: User-indicated tests" 1 eq 1
    pipe stop

  elseif msg.name EQS "MARK" then
    ! Info is 2 for MMB
    if ^msg.info EQ 2 then  
      new nxm.sys.libg.GMenu(reg.^{msg.fid}.MP,"Plot","Files,Layers,Features,Exit",0,0,reg.^{msg.fid})
    endif

  elseif msg.name EQS "TIMER" then
    calc aircraft.x aircraft.x 5 +
    if (aircraft.x >= 180.0) then
      calc aircraft.x -180.0
    endif

  elseif msg.name eqs "DRAGBOX"  then
    calc l:dragbox_cnt dragbox_cnt 3 MOD
    calc l:dragbox_cnt dragbox_cnt 1 + 
    if msg.fid eqss "PLOT_RASTER_" OR msg.fid eqss "PLOT_LINE_" then
      result tmin msg.data.tmin
      result tmax msg.data.tmax
      result fileid ^msg.fid
      ! Convert plot names to corresponding box name
      res boxid fileid.replace("PLOT_RASTER_","RBOX^{dragbox_cnt}")
      res boxid boxid.replace("PLOT_LINE_","LBOX")
      set ^{boxid}.setBoundsFromTable(msg.data)
      res gc.dragMinTime.value tmin
      res gc.dragMaxTime.value tmax
      res fmin msg.data.xmin
      res fmax msg.data.xmax
      res gc.dragMinFreq.value fmin
      res gc.dragMaxFreq.value fmax
      invoke clazz java.lang.Class.forName("nxm.sys.test.test_plot_wh_gui")
      invoke level java.util.logging.Level.^logLevel
      res myMessage "^{msg.fid} \n  times:^{tmin}-^tmax} \n  freqs:^{fmin}-^{fmax}"
      invoke ,, nxm.sys.lib.LogUtilities.log(clazz, level, myMessage)
    endif

  else
    !say "^reg.main.name: Unhandled message name=^msg.name info=^msg.info data=^msg.data"
  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

!******************************************************************************!
! Form new waveform/fft/plot combination where the
! 1) Raster plot will be named RasterDisplay{^plotnum}
! 2) TODO: Line plot will be named Layer1dDisplay{^plotnum}
! 3) The dragbox associated with the raster plots is called BOX{^plotnum}
!******************************************************************************!
procedure add_new_fft_plots S:plotnum S:format L:numelem S:wshape D:sfreq D:amp D:sr D:chirpamt L:fsize L:navg

  ! Form the ids and names based on plot number
  res waveid        basewaveid.concat(^plotnum)
  res wavename      basewavename.concat(^plotnum)
  res fftid         basefftid.concat(^plotnum)
  res fftname       basefftname.concat(^plotnum)
  res rplotid       baserplotid.concat(^plotnum)
  res lplotid       baselplotid.concat(^plotnum)
  res rbox1id       baserbox1id.concat(^plotnum)
  res rbox2id       baserbox2id.concat(^plotnum)
  res rbox3id       baserbox3id.concat(^plotnum)
  res lboxid        baselboxid.concat(^plotnum)
  res pipemonitorid basepipemonitorid.concat(^plotnum)
  res psdfilename   basefilename.concat(^{plotnum}.tmp)
  !say "raster plot name: ^rplotid line plot name: ^lplotid and rboxid ^rboxid and lboxid ^lboxid"

  ! waverform -> fft -> plot
  waveform/id=^wavename/throttle=^throttle/tl=4k ^wavename format elem=numelem shape=wshape freq=sfreq amp=amp delta=1/sr chirp=^chirpamt start=^tcur
  fft/dbf/id=^fftname ^wavename ^fftname{form=sf} nfft=^fsize over=^over navg=^navg
  plot/theme=WoB/id=^rplotid/msgid=MAIN/READOUT=Show|OnAxis|T|dT ^fftname view=iYX buttonaction={MMB=MARK} options=+crosshairs
  plot/theme=WoB/id=^lplotid/msgid=MAIN/READOUT=Show|OnAxis|T|dT ^fftname  TYPE=Line options=+crosshairs
  if saveFile then
    noop ^fftname ^psdfilename
  endif

  !add box feature to plot for creating a user selectable dragbox on the raster plot
  FEATURE/id=^rbox1id label=^rbox1id table={NAME=^rbox1id,TYPE=BOX,COLOR=WHITE,ENABLE=1} plot=^rplotid
  FEATURE/id=^lboxid label=^lboxid table={NAME=^lboxid,TYPE=BOX,COLOR=RED,ENABLE=1}   plot=^lplotid

  FEATURE/id=^rbox2id label=^rbox2id table={NAME=^rbox2id,TYPE=BOX,COLOR=YELLOW,ENABLE=1} plot=^rplotid
  FEATURE/id=^rbox3id label=^rbox3id table={NAME=^rbox3id,TYPE=BOX,COLOR=RED,ENABLE=1} plot=^rplotid

  gcontrol/id=^pipemonitorid pipemon ^pipemonitorid ^wavename

return

!******************************************************************************!
procedure add_feature_ellipse S:plotid
  invoke pi nxm.sys.inc.Constants.PI
  invoke radiusEarth nxm.sys.inc.Constants.EARTH_EQUATORIAL_RADIUS
  calc earthCircumference 2 pi * radiusEarth * 360 /
  calc semiMaj earthCircumference 15 * ! 15 degrees
  calc semiMin semiMaj 2 /            ! 7.5 degrees
  set tilt 0
  ! add ellipse to plot
  feature label=fellipse table={NAME="ELLIPSE",TYPE="ELPS",X=15,Y=7.5,&
                                SEMIMAJOR=semiMaj,SEMIMINOR=semiMin,TILT=90,&
                                COLOR="GREEN"} plot=^plotid

  ! The RED and YELLOW ellipses test the fix for Bug 1338
  feature label=lellipse table={NAME="LEFT",TYPE="ELPS",X=-175,Y=-7.5,&
                                SEMIMAJOR=semiMaj,SEMIMINOR=semiMin,TILT=90,&
                                COLOR="RED"} plot=^plotid

  feature label=rellipse table={NAME="RIGHT",TYPE="ELPS",X=175,Y=7.5,&
                                SEMIMAJOR=semiMaj,SEMIMINOR=semiMin,TILT=90,&
                                COLOR="YELLOW"} plot=^plotid
  res num 1
  do xindex -150 150 50
    do yindex -100 100 20 

      feature label=wellipse table={NAME="WELPS^{num}",TYPE="ELPS",X=^xindex,Y=^yindex,&
                                  SEMIMAJOR=semiMaj,SEMIMINOR=semiMin,TILT=90,&
                                  COLOR="WHITE"} plot=^plotid
      calc num num 1 +

    enddo
  enddo
    

  ! add 2nd ellipse and circle to plot
  calc semiMaj2 earthCircumference 5 * ! 5 degrees
  calc semiMin2 semiMaj2 2 /         ! 2.5 degrees
  feature label=fellipse2 table={NAME="ELLIPSECIRC",TYPE="ELPS|CIRC",X=15,Y=7.5,DX=5,&
                                 SEMIMAJOR=semiMaj2,SEMIMINOR=semiMin2,TILT=45,&
                                 COLOR="WHITE"} plot=^plotid

  ! add circle to plot
  ! feature label=fcircler table={NAME="CIRCLE",TYPE="CIRC",X=15,Y=7.5,DX=5,COLOR="RED"} plot=^plotid

  ! add circle to plot
  ! feature label=fcircley table={NAME="CIRCLEY",TYPE="CIRC",X=55,Y=7.5,DX=20,COLOR="YELLOW"} plot=^plotid

  ! add circle to plot
  ! feature label=fcircleg table={NAME="CIRCLEG",TYPE="CIRC",X=-55,Y=7.5,DX=20,COLOR="GREEN"} plot=^plotid
return