!******************************************************************************!
! Test case to use the zoom feature.
!
! Arguments: None
!
! @bug      NXM-3131 PLOT: Zoomed in TLOPs (TDOA LOPs) sometimes not showing up,
!                    int saturation on zooms
! @since    NeXtMidas 3.9.1
! @author:  Kevin Esterheld
!******************************************************************************!
startmacro
  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 - since 3.8.1

  if interAct or coreGraphics then
    set plotview "LATLON"
    set currentview ""
    set currentfeature ""
    set def {}
    set testfiles {}

    call setDatDir

    invoke GEODETIC nxm.sys.lib.Position.GEODETIC
    invoke tdoamodeList nxm.sys.libg.LayerGeo.tdoamodeList
    invoke fdoamodeList nxm.sys.libg.LayerGeo.fdoamodeList

    set instructions "Please click through the buttons and test the zooms work correctly.\n&
                      Once complete, please switch to ViewGeo and run the same process."

    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 10 /title
      gcontrol/nc=2 button nextplot "TESTS" &
          "Cone Filled,Ellipse,Ellipse Filled,Cone,StateVector,TDOA/FDOA" None /ttop
      gcontrol button zoomcustom "" "ZOOM"
      gcontrol button zoomtdoa "" "ZOOMTDOA"
      gcontrol button zoomfdoa "" "ZOOMFDOA"
      gcontrol button changeview "" "ViewGeo,LatLon" 
      invoke gc.zoomfdoa.panel.setVisible(false)
      invoke gc.zoomtdoa.panel.setVisible(false)
      invoke gc.zoomcustom.panel.setVisible(false)

      plot/id=idplot/msgid=MAIN/PRECISION=15/mousewheelzoom/usemouseposition ^{datDir}.world.prm view=^plotview readout=+ONAXIS 
    pipe off
  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 eqs "NEXTPLOT" then
    ! hide all zoom buttons
    call disableZoomButtons
    !start from scratch
    call newplot ^{datDir}.world.prm

    if msg.data eqs "CONE FILLED" then
      set currentview "CONE FILLED" ! set view to determine where to zoom later
      invoke gc.zoomcustom.panel.setVisible(true)
      call test_plot_cone_filled
    elseif msg.data eqs "CONE" then
      set currentview "CONE" ! set view to determine where to zoom later
      invoke gc.zoomcustom.panel.setVisible(true)
      call test_plot_layerT5_cone
    elseif msg.data eqs "ELLIPSE" then
      set currentview "ELLIPSE" ! set view to determine where to zoom later
      invoke gc.zoomcustom.panel.setVisible(true)
      call test_plot_layert5_ellipse
    elseif msg.data eqs "ELLIPSE FILLED" then
      set currentview "ELLIPSE FILLED" ! set view to determine where to zoom later
      invoke gc.zoomcustom.panel.setVisible(true)
      call test_plot_layert5_ellipseflags
    elseif msg.data eqs "STATEVECTOR" then
      invoke gc.zoomcustom.panel.setVisible(true)
      set currentview "STATEVECTOR" ! set view to determine where to zoom later
      call test_statevector
    elseif msg.data eqs "TDOA/FDOA" then
      set currentview "TDOA/FDOA" ! set view to determine where to zoom later
      call plotTdoa
      invoke gc.zoomfdoa.panel.setVisible(true)
      invoke gc.zoomtdoa.panel.setVisible(true)
    endif
  elseif msg.name eqs "CHANGEVIEW" then
    if msg.data eqs "VIEWGEO" then
      set plotview "GEO"
      set instructions "Please click through the buttons and test the zooms work correctly.\n&
                        If both the LatLon tests and ViewGeo tests all passed, please click PASS."
      call newplot ^{datDir}.world.prm instructions
      invoke ,, reg.idplot.setView("GEO")
      invoke ,, gc.nextplot.setItems("Cone Filled,Ellipse,Ellipse Filled,Cone,StateVector")
      call disableZoomButtons
    elseif msg.data starts "LATLON" then
      set plotview "LATLON"
      set instructions "Please click through the buttons and test the zooms work correctly.\n&
                        If both the LatLon tests and ViewGeo tests all passed, please click PASS."
      call newplot ^{datDir}.world.prm instructions
      invoke ,, reg.idplot.setView("LATLON")
      invoke ,, gc.nextplot.setItems("Cone Filled,Ellipse,Ellipse Filled,Cone,StateVector,TDOA/FDOA")
      call disableZoomButtons
    endif
  elseif msg.name eqs "ZOOMCUSTOM" then
    if currentview eqs "STATEVECTOR" then
      if plotview starts "LATLON" then
        message sendw id=idplot name=ZOOM data={xmin=-38.940608675,xmax=-38.940608323,ymin=17.475918416,ymax=17.475918633}
      else
        message sendw id=idplot name=ZOOM data={xmin=1.175,xmax=1.183,ymin=-0.778,ymax=-0.773}
      endif
    elseif currentview eqs "CONE FILLED" then
      if plotview starts "LATLON" then
        message sendw id=idplot name=ZOOM data={xmin=36.579217826648154,xmax=36.579217826649455,ymin=16.59274878584185,ymax=16.59274878584266}
      else
        !message sendw id=idplot name=ZOOM data={xmin=0.6490417,xmax=0.6490419,ymin=0.316750175,ymax=0.316750275}
        !message sendw id=idplot name=ZOOM data={xmin=555.63074e-3,xmax=555.63079e-3,ymin=333.532130e-3,ymax=333.532160e-3}
        !message sendw id=idplot name=ZOOM data={xmin=555.63075e-3,xmax=555.63078e-3,ymin=333.532145e-3,ymax=333.532150e-3}
        !message sendw id=idplot name=ZOOM data={xmin=555.630765e-3,xmax=555.63077e-3,ymin=333.5321465e-3,ymax=333.532148e-3} ! grid line gone but both visible
        !message sendw id=idplot name=ZOOM data={xmin=555.630766e-3,xmax=555.630769e-3,ymin=333.5321467e-3,ymax=333.5321478e-3}
        !message sendw id=idplot name=ZOOM data={xmin=555.6307665e-3,xmax=555.630768e-3,ymin=333.5321469e-3,ymax=333.5321476e-3}
        !message sendw id=idplot name=ZOOM data={xmin=555.63076655e-3,xmax=555.6307675e-3,ymin=333.5321470e-3,ymax=333.5321475e-3}
        message sendw id=idplot name=ZOOM data={xmin=656.13756375e-3,xmax=656.13756525e-3,ymin=330.2633077e-3,ymax=330.2633083e-3}
      endif
    elseif currentview eqs "CONE" then
      if plotview starts "LATLON" then
        message sendw id=idplot name=ZOOM data={xmin=6.1411912505,xmax=6.14119125175,ymin=5.3796136472,ymax=5.3796136480}
      else
        !message sendw id=idplot name=ZOOM data={xmin=162.226105e-3,xmax=162.22612e-3,ymin=94.821302e-3,ymax=94.821315e-3}
        !message sendw id=idplot name=ZOOM data={xmin=162.226111e-3,xmax=162.226114e-3,ymin=94.821308e-3,ymax=94.821310e-3}
        !message sendw id=idplot name=ZOOM data={xmin=162.2261121e-3,xmax=162.2261126e-3,ymin=94.821306e-3,ymax=94.821312e-3}
        !message sendw id=idplot name=ZOOM data={xmin=162.2261122e-3,xmax=162.2261125e-3,ymin=94.821308e-3,ymax=94.821310e-3}
        !message sendw id=idplot name=ZOOM data={xmin=162.2261123e-3,xmax=162.2261124e-3,ymin=94.8213085e-3,ymax=94.821310e-3} !both there but grid gone
        !message sendw id=idplot name=ZOOM data={xmin=162.22611232e-3,xmax=162.22611238e-3,ymin=94.8213087e-3,ymax=94.8213098e-3}
        !message sendw id=idplot name=ZOOM data={xmin=162.22611234e-3,xmax=162.22611236e-3,ymin=94.8213090e-3,ymax=94.8213095e-3}
        !message sendw id=idplot name=ZOOM data={xmin=162.226112345e-3,xmax=162.226112355e-3,ymin=94.8213091e-3,ymax=94.8213094e-3}
        !message sendw id=idplot name=ZOOM data={xmin=162.22611235e-3,xmax=162.226112355e-3,ymin=94.8213092e-3,ymax=94.8213093e-3} ! good trying to center moer
        message sendw id=idplot name=ZOOM data={xmin=162.22611245e-3,xmax=162.226112365e-3,ymin=94.8213092e-3,ymax=94.8213093e-3}
      endif
    elseif currentview starts "ELLIPSE" then
      if plotview starts "LATLON" then
        message sendw id=idplot name=ZOOM data={xmin=44.78379030922901,xmax=44.78379031454282,ymin=2.511049778871838,ymax=2.511049782113593}
      else
        message sendw id=idplot name=ZOOM data={xmin=798.135473515e-3,xmax=798.135473535e-3,ymin=47.8514331e-3,ymax=47.8514338e-3}
      endif
    endif
  elseif msg.name eqs "ZOOMTDOA" then
    message sendw id=idplot name=ZOOM data={xmin=-54.886285286,xmax=-54.886282557,ymin=53.244489353,ymax=53.244491923}
  elseif msg.name eqs "ZOOMFDOA" then
    message sendw id=idplot name=ZOOM data={xmin=80.516136555,xmax=80.516136585,ymin=-9.510578643494,ymax=-9.510578625634}
  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

!**************************************************************
! Disable all zoom buttons
!**************************************************************
procedure disableZoomButtons
  invoke gc.zoomfdoa.panel.setVisible(false)
  invoke gc.zoomtdoa.panel.setVisible(false)
  invoke gc.zoomcustom.panel.setVisible(false)
return

!******************************************************************************
procedure plotTdoa 
  set def.tdoaMode "Region"
  set def.tdoa 0
  set def.dtdoa 1e-3
  new nxm.sys.lib.Position(GEODETIC) site
  set site.lat 10
  set site.lon 10
  set site.alt 0
  layer "Geo" geoLayer {NAME=TDOA/FDOA,SVA=^{datDir}.sv1.prm,SVB=^{datDir}.sv2.prm,&
                        TDOA=^def.tdoa,&
                        DTDOA=^def.dtdoa} idplot
  call setInstructions "Please click on both 'ZOOMTDOA' and 'ZOOMFDOA' and confirm that &
                        the corresponding lines are drawn. If they are not drawn, pleae &
                        fail the test"
return

!******************************************************************************!
procedure newplot s:plotstring s:instructions
  !message FUNC=send ID=IDPLOT NAME=CLOSEFILE DATA="*"
  set reg.idplot.axis   "DEF"
  if resetAxisIndex eqs TRUE then
    set reg.idplot.axisindex "FALSE"
    set resetAxisIndex false
  endif
  foreach item intable testfiles
   if testfiles.^item nFEXISTS then
    warning "Could not generate/find ^testfiles.^item"
    return
   endif
  endfor
  call setInstructions instructions

  ! passing info=-1 to close all previous files
  message FUNC=sendw ID=IDPLOT NAME=OPENFILE INFO=-1 DATA=plotstring /wait=2

return

!***********************************************************************************
procedure test_statevector 
  result testfiles {}
  result testfiles.world    "^{datDir}.world.prm"
  result testfiles.sv3 "^{datDir}.sv3.prm"
  set svinstructions "Please click on the zoom button. &
               If the line is not visible, please fail the test" 
  if plotview eqs "GEO" then
    set svinstructions "^svinstructions \n\n\nThis does not test high precision, &
                      it tests that in view geo mode that the lines are still visible &
                      even with minimal zoom"
  endif
  call newplot "^{testfiles.world}|^{testfiles.sv3}" "^svinstructions" 
return

!******************************************************************************!
procedure test_plot_layert5_ellipseflags
  result testfiles {}
  result testfiles.world    "^{datDir}.world.prm"
  result testfiles.ellipse  "^{datDir}.ellipseflags5000.prm{layer={flags=+fillcones}}"

  call newplot "^{testfiles.world}|^{testfiles.ellipse}" &
               "Please click on the zoom button. If the line is not visible, or the area &
               to the right is not filled, please fail the test"

return


!******************************************************************************!
procedure test_plot_layert5_ellipse
  result testfiles {}
  result testfiles.world    "^{datDir}.world.prm"
  result testfiles.ellipse  "^{datDir}.ellipse5000.prm"

  call newplot "^{testfiles.world}|^{testfiles.ellipse}" &
              "Please click on the zoom button. If the line is not visible, please &
              fail the test"

return

!******************************************************************************!
procedure test_plot_layerT5_cone
  result testfiles {}
  result testfiles.world    "^{datDir}.world.prm"
  result testfiles.capitals "^{datDir}.capital_cities.prm"
  result testfiles.cone     "^{datDir}.cone5000.prm"

  call newplot "^{testfiles.world}|^{testfiles.cone}{Layer={symbol=star,color=red}}" &
               "Please click on the zoom button. If the line is not visible, please &
               fail the test."
return

!******************************************************************************!
procedure test_plot_cone_filled
  
  result testfiles.world    "^{datDir}.world.prm"
  result testfiles.coneflags "^{datDir}.coneflags5000.prm{layer={flags=+fillcones}}"
  call newplot "^{testfiles.world}|^{testfiles.coneflags}" "Please click the zoom button. \n&
               If the line is not drawn or the top left is not filled, please fail the test."

return

!******************************************************************************!
procedure setInstructions s:msgString
  set reg.panel.controls.idtext.value msgString
return
