! Tests the plotting of cones using both new code and legacy code
! Plots a set of cones 6 different ways by alternating views (latlon and geodetic)
! and conic levels (-1,1,20)
!
! Bug:    NXM-3753 GeodeticUtil.computeLLA giving bad values
! Since:  NeXtMidas 4.0.1
! Author: Jeanette McCall
!******************************************************************************!
startmacro
  switch INTERACTIVE interAct GET DEF=TRUE SDEF=TRUE  ! for normal interactive test(s)

  if interAct then
    res z:displayEdit false
    res currentView "LATLON"

    ! save off AUX.DAT and switch to DAT with all the test files
    call setDatDir
    res origDatAux "^{aux.dat}"
    invoke nm_root nxm.sys.lib.Shell.getNmRoot()
    res aux.dat "^{nm_root}^{datDirSlash}"

    ! Get original state of compare cone mode and cone list
    invoke z:wasComparingConeCode nxm.sys.lib.Transform.getCompareConeCode()
    invoke origConeList           nxm.sys.lib.Transform.getCompareConeList()

    ! Enable comparing of default code and legacy code
    invoke ,, nxm.sys.lib.Transform.setCompareConeCode(true)
    invoke ,, nxm.sys.lib.Transform.setCompareConeList(0x0F)

    call setMainInstructions
    call setConeData

    pipe init
      panel/msgid=MAIN/setup/controls=gc
      plot/id=idplotconicminus1/READOUT="+OnAxis|+DateString" view=currentView
      plot/id=idplotconic1/READOUT="+OnAxis|+DateString" view=currentView
      plot/id=idplotconic20/READOUT="+OnAxis|+DateString" view=currentView

      gcontrol label macro "MACRO"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}
      gcontrol text idtext "Instructions" mainInstructions 24 14 /title

      gcontrol/nc=3 button nextplot "TESTS" &
          "T5-Cone-A,T5-Cone-B,T5-Cone-C,T5-Filled-A,T5-Filled-B,T5-Filled-C,T5-EditCone,None" None /ttop

      gcontrol/nc=1 button toggleview "VIEWS" "LatLon,Geodetic" /ttop

      gcontrol/nc=1 button zoomCaseA2 "BEYONDHORIZON" "Click to Zoom" /ttop
      gcontrol/nc=1 button zoomCaseA1 "SOMEHORIZON" "Click to Zoom" /ttop
      gcontrol/nc=1 button zoomCaseB1 "MOREHORIZON" "Click to Zoom" /ttop
      gcontrol/nc=1 button zoomCaseC1 "PLACEHOLDER" "Click to Zoom" /ttop
      invoke ,, gc.zoomCaseA1.setShow(0)
      invoke ,, gc.zoomCaseA2.setShow(0)
      invoke ,, gc.zoomCaseB1.setShow(0)
      invoke ,, gc.zoomCaseC1.setShow(0)

      gcontrol/nc=1 button panGeodetic "PAN IN GEODETIC" "Pan" /ttop
      invoke ,, gc.panGeodetic.setShow(0)

      gcontrol DVAL posalt  "POS  ALT" 1E7 1E5 1E9   /slider /msg=no
      gcontrol DVAL poslat  "POS  LAT" 20 -90 90 1   /slider /msg=no
      gcontrol DVAL poslon  "POS  LON" 15 -180 180 1 /slider /msg=no
      gcontrol DVAL posplat "POSP LAT" 20 -90 90 1   /slider /msg=no
      gcontrol DVAL posplon "POSP LON" 50 -180 180 1 /slider /msg=no
      gcontrol DVAL angle   "1/2 ANGLE" 14 1 45 1    /slider /msg=no
      call hideEditControls
    pipe off

    res aux.dat "^origDatAux"  ! restore original AUX.DAT

    ! Restore cone mode and cone list to their original state
    invoke ,, nxm.sys.lib.Transform.setCompareConeCode(^wasComparingConeCode)
    invoke ,, nxm.sys.lib.Transform.setCompareConeList(^origConeList)

    %test_plot_cones_orbit ! NXM-3753
  endif
endmacro

!******************************************************************************!
! Main process message for this macro
!******************************************************************************!
procedure processMessage m:msg
  !say "got ^{msg.toString(NAME|DATA)}"

  if msg.name eqs "MAIN" then
    assert/text="Plot files tests" msg.data eqss "PASS"
    pipe stop
  elseif msg.name eqs "NEXTPLOT" then
    res displayEdit false
    res enableZoomCaseA1 false
    res enableZoomCaseA2 false
    res enableZoomCaseB1 false
    res enableZoomCaseC1 false
    if msg.data EQS "T5-CONE-A" then
      set t:cone5000_data cone5000_data_a
      call test_plot_layerT5_cone
      res enableZoomCaseA1 true
      res enableZoomCaseA2 true
    elseif msg.data EQS "T5-CONE-B" then
      set t:cone5000_data cone5000_data_b
      call test_plot_layerT5_cone
      res enableZoomCaseB1 true
    elseif msg.data EQS "T5-CONE-C" then
      set t:cone5000_data cone5000_data_c
      call test_plot_layerT5_cone
      res enableZoomCaseC1 true
    elseif msg.data EQS "T5-FILLED-A" then
      set t:cone5000_data cone5000_data_a
      call test_plot_layert5_filled
      res enableZoomCaseA1 true
      res enableZoomCaseA2 true
    elseif msg.data EQS "T5-FILLED-B" then
      set t:cone5000_data cone5000_data_b
      call test_plot_layert5_filled
      res enableZoomCaseB1 true
    elseif msg.data EQS "T5-FILLED-C" then
      set t:cone5000_data cone5000_data_c
      call test_plot_layert5_filled
      res enableZoomCaseC1 true
    elseif msg.data EQS "T5-EDITCONE" then
      set t:cone5000_data {}
      set t:cone5000_data.0 {POS=(^{gc.posalt.value},^{gc.poslat.value},^{gc.poslon.value}),&
                             POSP=(0,^{gc.posplat.value},^{gc.posplon.value}),&
                             CONE=(-1,^{gc.angle.value},^{gc.angle.value}),&
                             NAME="USERDEF",LABEL="USERDEF"}
      call test_plot_layert5_edit_cone
      res displayEdit true
    elseif msg.data EQS "NONE" then
      call test_plot_none
    else
      warning "Do not know ^msg.data layer"
    endif

    ! Toggle edit controls
    if displayEdit then
      call showEditControls
    else
      call hideEditControls
    endif

    ! Toggle zoom controls
    if enableZoomCaseA1 then
      invoke ,, gc.zoomCaseA1.setShow(1)
    else
      invoke ,, gc.zoomCaseA1.setShow(0)
    endif
    if enableZoomCaseA2 then
      invoke ,, gc.zoomCaseA2.setShow(1)
    else
      invoke ,, gc.zoomCaseA2.setShow(0)
    endif
    if enableZoomCaseB1 then
      invoke ,, gc.zoomCaseB1.setShow(1)
    else
      invoke ,, gc.zoomCaseB1.setShow(0)
    endif
    if enableZoomCaseC1 then
      invoke ,, gc.zoomCaseC1.setShow(1)
    else
      invoke ,, gc.zoomCaseC1.setShow(0)
    endif
  elseif msg.name eqs "TOGGLEVIEW" then
    set currentView msg.data
    message send idplotconicminus1,,view,,currentView
    message send idplotconic1     ,,view,,currentView
    message send idplotconic20    ,,view,,currentView
    if currentview EQS GEODETIC THEN
      invoke ,, gc.panGeodetic.setShow(1)
    else
      invoke ,, gc.panGeodetic.setShow(0)
    endif
  elseif msg.name eqs "PANGEODETIC" AND currentView  EQS GEODETIC then
    ! automate desired PANOBS message created by RMB drag
      ! To get values for msg.data, temporarily add these lines in your test case
      ! (likely where the view switches to GEODETIC)
      !   res z:myTrue true
      !   invoke ,, reg.<plot_name>.seePanGeoMsg(myTrue)
      ! Create double array containing msg.data for PANOBS
    ! Create double array containing msg.data for PANOBS
    invoke dblarray nxm.sys.test.TestUtil.createDoubleArray("15.0,11.875,6.0")
    ! Send PANOBS matching plot.processMessage PANOBS message
    message send IDPLOTCONICMINUS1 name=PANOBS info=-7  QUALS={ORIG_MSG_DATA={X=L:517,Y=L:257,W=L:-72,H=L:57}} DATA=dblarray
    message send IDPLOTCONIC1      name=PANOBS info=-7  QUALS={ORIG_MSG_DATA={X=L:517,Y=L:257,W=L:-72,H=L:57}} DATA=dblarray
    message send IDPLOTCONIC20     name=PANOBS info=-7  QUALS={ORIG_MSG_DATA={X=L:517,Y=L:257,W=L:-72,H=L:57}} DATA=dblarray
  elseif msg.name eqs "ZOOMCASEA1" then
    ! somehorizon
    IF currentView NEQS GEODETIC THEN 
      res t:zoomdata {X1=88,X2=92,Y1=40,Y2=43.5}
    ELSE
      res t:zoomdata {X1=730E-3,X2=800E-3,Y1=600E-3,Y2=690E-3}
    ENDIF
    message send IDPLOTCONICMINUS1 name=zoom DATA=^zoomdata
    message send IDPLOTCONIC1      name=zoom DATA=^zoomdata
    message send IDPLOTCONIC20     name=zoom DATA=^zoomdata
  elseif msg.name eqs "ZOOMCASEA2" then
    ! beyondhorizon
    IF currentView NEQS GEODETIC THEN 
      res t:zoomdata {X1=-106.5,X2=-102.5,Y1=77,Y2=81}
    ELSE
      res t:zoomdata {X1=-50E-3,X2=300E-3,Y1=-1.2,Y2=-0.7}
    ENDIF
    message send IDPLOTCONICMINUS1 name=zoom DATA=^zoomdata
    message send IDPLOTCONIC1      name=zoom DATA=^zoomdata
    message send IDPLOTCONIC20     name=zoom DATA=^zoomdata
  elseif msg.name eqs "ZOOMCASEB1" then
    ! morehorizon
    IF currentView NEQS GEODETIC THEN 
      res t:zoomdata {X1=84,X2=92,Y1=48.9,Y2=50.5}
    ELSE
      res t:zoomdata {X1=690E-3,X2=735E-3,Y1=630E-3,Y2=700E-3}
    ENDIF
    message send IDPLOTCONICMINUS1 name=zoom DATA=^zoomdata
    message send IDPLOTCONIC1      name=zoom DATA=^zoomdata
    message send IDPLOTCONIC20     name=zoom DATA=^zoomdata
  elseif msg.name eqs "ZOOMCASEC1" then
    IF currentView NEQS GEODETIC THEN 
      res t:zoomdata {X1=76,X2=109,Y1=44.5,Y2=51.2}
    ELSE
      res t:zoomdata {X1=690E-3,X2=735E-3,Y1=630E-3,Y2=700E-3}
    ENDIF
    message send IDPLOTCONICMINUS1 name=zoom DATA=^zoomdata
    message send IDPLOTCONIC1      name=zoom DATA=^zoomdata
    message send IDPLOTCONIC20     name=zoom DATA=^zoomdata
  else
    ! do nothing
    say "^reg.main.name: Unhandled message name: ^msg.name from fid: ^msg.fid 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

!******************************************************************************!
! Set main/default instructions
!******************************************************************************!
procedure setMainInstructions
  ! Get available cone list masks
  invoke coneDefaultMask nxm.sys.lib.Transform.CONE_DEFAULT
  invoke coneLegacyMask  nxm.sys.lib.Transform.CONE_LEGACY
  invoke coneSphereMask  nxm.sys.lib.Transform.CONE_SPHERE

  ! Determine whether or not each mask is being used
  invoke coneDefaultEnabled nxm.sys.lib.Transform.isOnCompareList(^coneDefaultMask)
  invoke coneLegacyEnabled  nxm.sys.lib.Transform.isOnCompareList(^coneLegacyMask)
  invoke coneSphereEnabled  nxm.sys.lib.Transform.isOnCompareList(^coneSphereMask)

  res firstLineNewLine ""
  res goldenrodGreenNote ""
  res lastCompareConeCodeEnabled ""

  ! Enable instructions for cone codes that are enabled
  res noteDefault ""
  if coneDefaultEnabled then
    set firstLineNewLine "\n"
    set noteDefault "- PURPLE Andrew's oblate earth"
    set lastCompareConeCodeEnabled "DEFAULT"
  endif
  res noteLegacy  ""
  if coneLegacyEnabled then
    set firstLineNewLine "\n"
    set noteLegacy "- RED pure Jeff's code"
    set lastCompareConeCodeEnabled "LEGACY"
  endif
  res noteSphere  ""
  if coneSphereEnabled then
    set firstLineNewLine "\n"
    set noteSphere "- GOLDENROD Jeff's + Andrew's spherical earth"
    set lastCompareConeCodeEnabled "SPHERE"
  endif

  ! Append a newline to any line that isn't the last line listed
  if coneDefaultEnabled and lastCompareConeCodeEnabled neqs "DEFAULT" then set noteDefault "^{noteDefault}\n"
  if coneLegacyEnabled  and lastCompareConeCodeEnabled neqs "LEGACY"  then set noteLegacy  "^{noteLegacy}\n"
  if coneSphereEnabled  and lastCompareConeCodeEnabled neqs "SPHERE"  then set noteSphere  "^{noteSphere}\n"

  ! Set goldenrod-green note
  if coneSphereEnabled then set goldenrodGreenNote "\n\nNote: goldenrod matches the previous patch code"

  ! Order note
  res orderNote "\n\nIMPORTANT:\n* Do all tests in LatLon mode first. Come back to Geodetic.\n&
                * In Geodetic, do T5-Cone-A and T5-Filled-A BEYONDHORIZON tests\n&
                * Click 'Pan' and do the rest of the Geodetic tests"

  res mainInstructions "Expected: world plots with CONIC=-1,1,20^{firstLineNewLine}&
                        ^{noteDefault}^{noteLegacy}^{noteSphere}^{goldenrodGreenNote}^{orderNote}"
return

!******************************************************************************!
! Set instructions
!******************************************************************************!
procedure setInstructions s:msgString
  if msgString eqs "" then
    set reg.panel.controls.idtext.value "^{mainInstructions}"
  else
    set reg.panel.controls.idtext.value "^{mainInstructions}\n\n^{msgString}"
  endif
return

!******************************************************************************!
! Show/display edit controls
!******************************************************************************!
procedure showEditControls
  invoke ,, gc.posalt.setShow(1)
  invoke ,, gc.poslat.setShow(1)
  invoke ,, gc.poslon.setShow(1)
  invoke ,, gc.posplat.setShow(1)
  invoke ,, gc.posplon.setShow(1)
  invoke ,, gc.angle.setShow(1)
return

!******************************************************************************!
! Hide edit controls
!******************************************************************************!
procedure hideEditControls
  invoke ,, gc.posalt.setShow(0)
  invoke ,, gc.poslat.setShow(0)
  invoke ,, gc.poslon.setShow(0)
  invoke ,, gc.posplat.setShow(0)
  invoke ,, gc.posplon.setShow(0)
  invoke ,, gc.angle.setShow(0)
return

!******************************************************************************!
! Set Cone Data
!******************************************************************************!
procedure  setConeData

    ! NOTE: POS and POSP get reversed if FLAG data is contained in the file
    ! cone data for A-plots
    set t:cone5000_data_a   {}
    set t:cone5000_data_a.0 {POS=(0,50,20),POSP=(3.7E7,15,20),CONE=(-1,14.75,14.75),NAME="BEYONDHORIZON",LABEL="BEYONDHORIZON",FLAG=(13,0,2,2,-9,1,4,4)}
    set t:cone5000_data_a.1 {POS=(0.006568699999999999,-25,140),POSP=(3.481502E7,-45,123),CONE=(-1,0.6332,0.6332),NAME="ONSURFACE",LABEL="ONSURFACE",FLAG=(9,0,4,2,-9,1,4,4)}
    set t:cone5000_data_a.2 {POS=(0.1997,34.3982,67.0013),POSP=(34815020,-3.9757,16.7008),CONE=(-1,0.6332,0.6332),NAME="SOMEHORIZON",LABEL="SOMEHORIZON",FLAG=(11,0,3,2,0,1,2,4)}
    res z:enableZoomCaseA1 false
    res z:enableZoomCaseA2 false

    ! cone data for B-plots
    set t:cone5000_data_b   {}
    set t:cone5000_data_b.0 {POS=(0.1997,34.3982,67.0013),POSP=(34815020,-3.9757,16.7008),CONE=(-1,4,4),NAME="MOREHORIZON",LABEL="MOREHORIZON",FLAG=(11,0,2,2,-9,1,4,4)}
    res z:enableZoomCaseB1 false

    ! cone data for C-plots
    set t:cone5000_data_c   {}
    set t:cone5000_data_c.0 {POS=(0,50,63),POSP=(10E6,20,15),CONE=(-1,14,14),NAME="PLACEHOLDER",LABEL="PLACEHOLDER",FLAG=(11,0,2,2,-9,1,4,4)}
    res z:enableZoomCaseC1 false
return

!******************************************************************************!
! Clear all plots
!******************************************************************************!
procedure test_plot_none
  result testfiles {}
  ! NTN: Added instructions to test fix for Bug 1945
  call setInstructions ""
  message FUNC=sendw ID=IDPLOTCONICMINUS1 NAME=OPENFILE INFO=-1 DATA="" /wait=2
  message FUNC=sendw ID=IDPLOTCONIC1      NAME=OPENFILE INFO=-1 DATA="" /wait=2
  message FUNC=sendw ID=IDPLOTCONIC20     NAME=OPENFILE INFO=-1 DATA="" /wait=2
return

!******************************************************************************!
! Plot cone data for entries in cone5000_data table
!******************************************************************************!
procedure test_plot_layerT5_cone
  result testfiles {}
  result testfiles.world "world.prm"

  call file_from_table

  ! conic=-1
  result testfiles.cone "ram:temp_cone5000{LAYER={LT="T5",CONIC=-1,FIELD=+Labels,FLAGS=+ConeFlagHoriz}}"
  message FUNC=sendw ID=IDPLOTCONICMINUS1 NAME=OPENFILE INFO=-1 DATA="^{testfiles.world}|^{testfiles.cone}" /wait=2
  message send idplotconicminus1,,view,,currentView

  ! conic=1
  result testfiles.cone "ram:temp_cone5000{LAYER={LT="T5",CONIC=1,FIELD=+Labels,FLAGS=+ConeFlagHoriz}}"
  message FUNC=sendw ID=IDPLOTCONIC1 NAME=OPENFILE INFO=-1 DATA="^{testfiles.world}|^{testfiles.cone}" /wait=2
  message send idplotconic1,,view,,currentView

  ! conic=20
  result testfiles.cone "ram:temp_cone5000{LAYER={LT="T5",CONIC=20,FIELD=+Labels,FLAGS=+ConeFlagHoriz}}"
  message FUNC=sendw ID=IDPLOTCONIC20 NAME=OPENFILE INFO=-1 DATA="^{testfiles.world}|^{testfiles.cone}" /wait=2
  message send idplotconic20,,view,,currentView
return

!******************************************************************************!
! Plot filled cone data for entries in cone5000_data table
!******************************************************************************!
procedure test_plot_layerT5_filled
  result testfiles {}
  result testfiles.world "world.prm"

  call file_from_table

  ! conic=-1
  result testfiles.cone "ram:temp_cone5000{LAYER={LT="T5",CONIC=-1,FLAGS=+fillcones|+ConeFlagHoriz}}"
  message FUNC=sendw ID=IDPLOTCONICMINUS1 NAME=OPENFILE INFO=-1 DATA="^{testfiles.world}|^{testfiles.cone}" /wait=2
  message send idplotconicminus1,,view,,currentView

  ! conic=1
  result testfiles.cone "ram:temp_cone5000{LAYER={LT="T5",CONIC=1,FLAGS=+fillcones|+ConeFlagHoriz}}"
  message FUNC=sendw ID=IDPLOTCONIC1 NAME=OPENFILE INFO=-1 DATA="^{testfiles.world}|^{testfiles.cone}" /wait=2
  message send idplotconic1,,view,,currentView

  ! conic=20
  result testfiles.cone "ram:temp_cone5000{LAYER={LT="T5",CONIC=20,FLAGS=+fillcones|+ConeFlagHoriz}}"
  message FUNC=sendw ID=IDPLOTCONIC20 NAME=OPENFILE INFO=-1 DATA="^{testfiles.world}|^{testfiles.cone}" /wait=2
  message send idplotconic20,,view,,currentView
return

!******************************************************************************!
! Plot edit cone data for entries in cone5000_data table
!******************************************************************************!
procedure test_plot_layert5_edit_cone
  result testfiles {}
  result testfiles.world "world.prm"

  call file_from_table

  ! conic=-1
  result testfiles.cones  "ram:temp_cone5000{LAYER={LT="T5",CONIC=-1}}"
  message FUNC=sendw ID=IDPLOTCONICMINUS1 NAME=OPENFILE INFO=-1 DATA="^{testfiles.world}|^{testfiles.cones}" /wait=2
  message send idplotconicminus1,,view,,currentView

  ! conic=1
  result testfiles.cones  "ram:temp_cone5000{LAYER={LT="T5",CONIC=1}}"
  message FUNC=sendw ID=IDPLOTCONIC1 NAME=OPENFILE INFO=-1 DATA="^{testfiles.world}|^{testfiles.cones}" /wait=2
  message send idplotconic1,,view,,currentView

  ! conic=20
  result testfiles.cones  "ram:temp_cone5000{LAYER={LT="T5",CONIC=20}}"
  message FUNC=sendw ID=IDPLOTCONIC20 NAME=OPENFILE INFO=-1 DATA="^{testfiles.world}|^{testfiles.cones}" /wait=2
  message send idplotconic20,,view,,currentView
return

!******************************************************************************!
! Generate temporary Blue file for cone data entries
!******************************************************************************!
procedure file_from_table
  invoke tblSize cone5000_data.getSize()
  set lastTableEntry tblSize-1

  if cone5000_data.0 CONTAINS/K FLAG
    file open/n out ram:temp_cone5000{COMPONENTS=(POS/VD,POSP/VD,CONE/VD,NAME/9A,LABEL/9A,FLAG/8B),SIZE=tblSize}
  else
    file open/n out ram:temp_cone5000{COMPONENTS=(POS/VD,POSP/VD,CONE/VD,NAME/9A,LABEL/9A),SIZE=tblSize}
  endif 
  do i 0 lastTableEntry 1
    invoke ,, out.setData(^i,cone5000_data.^i)
  enddo
  file close out
return
