!******************************************************************************!
! Tests the new default behavior of maximizing map area when doing DRAGBOX zooms
! Also tests setting of OUTER and VIEW limits via message, such that the UNZOOM
! can keep the plot centered about the zoom area and the plot bounds match the
! point / full latitude range, rather than just the range of world.prm
!
! @bug      NXM-3581 DRAGBOX zoom leaves excess margin and then pan causes map to jump
!           NXM-3610 PLOT/MPlot: Need to be able to set plot limits via message
!           NOTE: Without setting the full latitude range, the map will either need
!           to resize/jump when the diagonal points (now full -90 to 90 range)
!           are added of the if LayerT5 had not been modified to avoid
!           constant resizing, the map would flicker
! @since    3.9.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 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 "********     INSTRUCTIONS FOR TOP PLOTS    ********\n\n&
                      Try the following once with 'Maximize Map' ON and then again with it OFF\n\n&
                      1) DRAGBOX ZOOM along a border, including some margin area\n   &
                         LEFT plot  - right or left border\n   &
                         RIGHT plot - top or bottom border\n&
                      => EXPECTED results based on 'Maximize Map' setting:\n   &
                         ON  => should display only map area (if possible)\n   &
                         OFF => zoomed area will be centered about zoom box,\n          &
                                and will likely include margin\n\n\n&
                      2) Pan the direction listed below:\n   &
                         LEFT plot  - up or down\n   &
                         RIGHT plot - left or right\n&
                      => Panning should be smooth and the map should not jump\n\n&
                      3) Fully UNZOOM the plots between the ON and OFF test\n\n\n&
                      ********   INSTRUCTIONS FOR BOTTOM PLOTS   ********\n\n&
                      => map should be zoomed in centered to the right of Australia\n\n&
                      1) Right click to unzoom the map\n&
                      => The new map bounds should be {X1=0,X2=360,Y1=-90,Y2=90}\n\n&
                      2) Click 'Add a piped Layer' twice to add yellow and red pts\n&
                      => Diagonal points should cover the full -90 to 90 lat range\n&
                      => The map should render as expected\n   &
                         (should not flicker or suddenly resize)\n&
                      "

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

      nxm.sys.test.plotellipsetester/DIAGONAL/COLOR=4/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/RT/NOTEMODETYPE=VA/RATE=700/STARTTIME="2007:01:02::00:00:00" OUT=_t5pipe2{SIZE=10000} ELEM=Inf

      plot/id=ymargin ^{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=xmargin ^{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=latlon

      plot/id=timepipe/msgid=1 ^{datDir}.world.prm|{LT="Drawable",name=t5Group2,listlayers=true} readoutExt=+SHOW|+POINTS &
               readout=+OnAxis|+AlwaysDeltas view=latloncontinuous

      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"

      FEATURE label=pipebox table={NAME=PIPEBOX,TYPE=BOX,COLOR=AQUA,ENABLE=1} plot=timepipe

      message send timepipe NAME=LIMITS INFO=0 DATA=OUTER QUALS={X1=0,X2=360,Y1=-90,Y2=90}
      message send timepipe NAME=LIMITS INFO=0 DATA=VIEW QUALS={X1=110,X2=250,Y1=-70,Y2=0}
!      message send timepipe NAME=ZOOM INFO=0 DATA={XMIN=160,XMAX=200,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 EQS "CLEARLAYERS" then
    invoke ,, reg.timepipe.clearData()
  elseif msg.name eqs "ADDLAYER" then
    if has1stLayer isFalse then
      invoke ,, reg.timepipe.openFile(_t5pipe{LAYER={^ellipselayerPIPE},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},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
  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
