!******************************************************************************!
! Test the plotting of files for LayerGeo.
!
! Author:  J. Hallam / Neon Ngo
!******************************************************************************!
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)
  switch DISPLAY       displayMode   GET DEF=Front SDEF=Front
  switch ALTHZNDEBUG   althzndebug   GET DEF=FALSE SDEF=TRUE
  switch ALTHZNCLASSIC althznclassic GET DEF=FALSE SDEF=TRUE

  res z:mytrue  true
  res z:myfalse false

  if interAct then
    call setDatDir
    ! Horizon at Altitude prototype testing
    invoke ,, nxm.sys.lib.Transform.setHorizonAltDebug(althzndebug)
    invoke traditional nxm.sys.libg.LayerGeo.isHorizonAltTraditionalCode()
    if althznclassic then
      invoke ,, nxm.sys.libg.LayerGeo.useHorizonAltTraditionalCode(mytrue)
    else
      invoke ,, nxm.sys.libg.LayerGeo.useHorizonAltTraditionalCode(myfalse)
    endif

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

    ! Defaults
    set def {}
    set def.tdoaMode "Region"
    set def.tdoa 0
    set def.dtdoa 1e-3
  
    set def.fdoaMode "Region"
    set def.fdoa 0
    set def.dfdoa 0.01 !!! Changed from 0.1 on 05OCT06 so that the FDOA
                       !!! lines are visible...what I do not know is if
                       !!! I just did not notice they were not there or
                       !!! whether something else had changed that would
                       !!! have affected this.

    set def.targetLabel "Target"
    set def.siteLabel   "MYSITE"
    set def.svaLabel    "SVA"
    set def.svbLabel    "SVB"

    invoke def.gridsizex nxm.sys.libg.LayerGeo.DEFAULT_X_GRID_SIZE
    invoke def.gridsizey nxm.sys.libg.LayerGeo.DEFAULT_Y_GRID_SIZE

    set def.conic 1
    set def.targetPos {LAT=5,LON=-100}
    set def.pointPosA {LAT=-75,LON=-75}
    set def.pointPosB {LAT=-45,LON=-45}
    set def.coneangle 1

    set def.flagsStr "ALL"
    set def.predictRef "SITE"

    set def.horizonAlt 1000.0

    invoke colorObj nxm.sys.libg.LayerGeo.DEFAULT_HORIZON_COLOR
    invoke def.colorA      nxm.sys.libg.MColor.toString(colorObj)
    invoke def.colorB      nxm.sys.libg.MColor.toString(colorObj)

    invoke colorObj nxm.sys.libg.LayerGeo.DEFAULT_HORIZONALT_COLOR
    invoke def.colorAlt   nxm.sys.libg.MColor.toString(colorObj)

    invoke colorObj nxm.sys.libg.LayerGeo.DEFAULT_TARGET_COLOR
    invoke def.colorTarget nxm.sys.libg.MColor.toString(colorObj)

    invoke colorObj nxm.sys.libg.LayerGeo.DEFAULT_SITE_COLOR
    invoke def.colorSite   nxm.sys.libg.MColor.toString(colorObj)

    invoke colorObj nxm.sys.libg.LayerGeo.DEFAULT_TDOA_COLOR
    invoke def.colorTDOA  nxm.sys.libg.MColor.toString(colorObj)
    invoke colorObj nxm.sys.libg.LayerGeo.DEFAULT_FDOA_COLOR
    invoke def.colorFDOA  nxm.sys.libg.MColor.toString(colorObj)

    pipe init

      panel/msgid=MAIN/setup/controls=gc
      plot/id=idplot/msgid=MAIN ^{datDir}.world.prm view=latlon

      gcontrol label mainLab "MACRO" Open
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}
      gcontrol button addLayerGeo "" "Add Geo Layer"
      gcontrol text idtext "Instructions" "Initializing..." 24 18 /title


      gcontrol label layerGeoLab "Layer Geo Options" Open
      gcontrol button showSiteB "" "Show Site B" ""

      gcontrol choice lgTDOAMODE "TDOA Mode" "^tdoamodeList" def.tdoaMode
      gcontrol choice lgFDOAMODE "FDOA Mode" "^fdoamodeList" def.fdoaMode

      !jph -- Note, a negative value indicates number of contours ONLY in contour mode!!!
      gcontrol DVAL lgDTDOA "Delta TDOA (<0 = # contours)"  def.dtdoa -100 10000 1
      gcontrol DVAL lgDFDOA "Delta FDOA (<0 = # contours)"  def.dfdoa -100 10000 1

      gcontrol prompt lgTARGETLABEL "Target Label" "^def.targetLabel"
      gcontrol prompt lgSITELABEL "Site Label" "^def.siteLabel"

      gcontrol prompt lgSVALABEL "SVA Label" "^def.svaLabel"
      gcontrol prompt lgSVBLABEL "SVB Label" "^def.svbLabel"

      invoke predictReferenceList nxm.sys.libg.LayerGeo.predictReferenceList
      gcontrol choice lgPredictReference "Prediction Reference" "^predictReferenceList" def.predictRef

      invoke flagsList nxm.sys.libg.LayerGeo.flagsList
      gcontrol choice flags "Flags" "^flagsList" def.flagsStr /chkbox /toggle /nc=2

      gcontrol LVAL lgGRIDSIZEX "Grid Size X"  def.gridsizex 2 200 1
      gcontrol LVAL lgGRIDSIZEY "Grid Size Y"  def.gridsizey 2 200 1


      gcontrol label layerGeoLab2 "Position and Cone Options" Open
      gcontrol menu lgDrawConeToTarget "Draw Cone to Target" "On" "On" /toggle /chkbox /tleft
      gcontrol choice lgConic "Number of Cone sections to draw" "1,2,3,4,5,6,7,8,9,10" "^def.conic" /tleft

      gcontrol DVAL lgTargetPoslat "Target Pos - LAT" def.targetPos.lat -90  +90
      gcontrol DVAL lgTargetPoslon "Target Pos - LON" def.targetPos.lon -180 +180

      gcontrol DVAL lgPointPosAlat "Pointing Pos A - LAT" def.pointPosA.lat -90  +90
      gcontrol DVAL lgPointPosAlon "Pointing Pos A - LON" def.pointPosA.lon -180 +180
      gcontrol DVAL lgPointPosBlat "Pointing Pos B - LAT" def.pointPosB.lat -90  +90
      gcontrol DVAL lgPointPosBlon "Pointing Pos B - LON" def.pointPosB.lon -180 +180

      gcontrol DVAL lgConeAngle  "Cone Angle (A & B)" def.coneAngle 0 +360 1
      gcontrol DVAL lgConeAngleA "Cone Angle A"       def.coneAngle 0 +360 1
      gcontrol DVAL lgConeAngleB "Cone Angle B"       def.coneAngle 0 +360 1

      gcontrol label layerGeoLab3 "Color Options" Open
      set baseColorList "Red,Green,Blue,Orange,Yellow,Cyan,Purple"

      set colorList "^baseColorList,^def.colorA"
      gcontrol choice lgColorA "ALL SVA item colors" "^colorList" "^def.colorA" /tleft
      set colorList "^baseColorList,^def.colorB"
      gcontrol choice lgColorB "ALL SVB item colors" "^colorList" "^def.colorB" /tleft

      set colorList "^baseColorList,^def.colorA,Other"
      gcontrol choice lgFootPrintColorA "Footprint A Color" "^colorList" "^def.colorA" /tleft
      set colorList "^baseColorList,^def.colorB,Other"
      gcontrol choice lgFootPrintColorB "Footprint B Color" "^colorList" "^def.colorB" /tleft

      set colorList "^baseColorList,^def.colorA,Other"
      gcontrol choice lgHorizonColorA "Horizon A Color" "^colorList" "^def.colorA" /tleft
      set colorList "^baseColorList,^def.colorB,Other"
      gcontrol choice lgHorizonColorB "Horizon B Color" "^colorList" "^def.colorB" /tleft

      set colorList "^baseColorList,^def.colorTarget,Other"
      gcontrol choice lgTargetColor "Target Color" "^colorList" "^def.colorTarget" /tleft
      set colorList "^baseColorList,^def.colorSite,Other"
      gcontrol choice lgSiteColor   "Site Color" "^colorList" "^def.colorSite" /tleft

      set colorList "^baseColorList,^def.colorTDOA,Other"
      gcontrol choice lgTDOAColor "TDOA Line Color" "^colorList" "^def.colorTDOA" /tleft
      set colorList "^baseColorList,^def.colorFDOA,Other"
      gcontrol choice lgFDOAColor "FDOA Line Color" "^colorList" "^def.colorFDOA" /tleft

      gcontrol/ww=50 label layerGeoLab4 "Horizon At Alt Options" Open
      set colorList "^baseColorList,^def.colorAlt,Other"
      gcontrol menu lgHznAltEnabled "Draw Horizon at Altitude" "On" "On" /toggle /chkbox /tleft
      gcontrol DVAL lgHorizonAlt  "Horizon At Altitude" def.horizonAlt 0 -1 100 /tleft
      set colorList "^baseColorList,^def.colorAlt,Other"
      gcontrol choice lgColorAAlt "SVA Horizon Alt Color" "^colorList" "^def.colorAlt" /tleft
      gcontrol choice lgColorBAlt "SVB Horizon Alt Color" "^colorList" "^def.colorAlt" /tleft
      gcontrol menu lgCalcApproach "Calculation Approach" "Visual,Traditional" "Visual" /chkbox /tleft
      gcontrol text hznalttext "Instructions" "Try different altitudes\n&
            Note: Update another setting for 'Calculation Approach' to take effect" 8 3 /title


      ! Hide all options at startup
      foreach cont INTABLE gc
        if cont EQSS "LAYERGEOLAB"
          ! In the 2.5.x series (before 2.5.3) there was a bug that prevented hiding controls under
          ! label groups from working.
          set gc.^{cont}.show 0
        endif
      endfor
    pipe off

    invoke ,, nxm.sys.lib.Transform.setHorizonAltDebug(myfalse)
    invoke ,, nxm.sys.libg.LayerGeo.useHorizonAltTraditionalCode(traditional)
  endif

  ! Additional interactive tests, only run with /BASELINE
  if interAct and baseline then
    %test_plot_layergeo_tdoa_fdoa_extra_contours ! NXM-4002
    %test_plot_layergeo_timecode                 ! Bug 2503
  endif

endmacro

!******************************************************************************!
procedure processMessage m:msg

if /verbose then say "^reg.main.name: msg.name ^msg.name, info ^msg.info, data ^msg.data"

if msg.name eqs "MAIN" then
  assert/text="^reg.main.name: User indicated tests" msg.data eqss "PASS"
  pipe stop

elseif msg.fid eqs "IDPLOT" then
  if msg.name EQS "MARK"
    if geolayer REXISTS then
      set geolayer.pos msg.data
    endif
  else
    !!!say "^reg.main.name: Unhandled PLOT message ^msg.name, from ^msg.fid data: ^msg.data"
  endif

elseif msg.name eqs "ADDLAYERGEO" then
  ! Hide all options at startup
  foreach cont INTABLE gc
    if cont EQSS "LAYERGEOLAB"
      set gc.^{cont}.show 1
    endif
  endfor
  set gc.addlayerGeo.show 0
  call initializeLayerGeo

elseif msg.name eqss "LG" then
  ! Layer Geo Messages
  call handleLayerGeoOption

elseif msg.name eqs "FLAGS" then
  if geoLayer REXISTS then
    invoke xxx geoLayer.setFlags(^msg.info)
    set geoLayer.flags ^msg.info
  endif

elseif msg.name eqs "SHOWSITEB" then
  if posSiteB NREXISTS then
    new nxm.sys.lib.Position(GEODETIC) posSiteB
    set posSiteB.lat 30
    set posSiteB.lon 30
    set posSiteB.alt 0
    set geoLayer.siteB posSiteB
    set geoLayer.siteLabelB "MYSITEB"

  else
    beep 1
    warning "^reg.main.name: site B should already be showing"
  endif

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

!******************************************************************************!
procedure open
  set moreInstructions ""
  if displayMode eqs "Maximized" then ! instructions for BUG 2551
    set moreInstructions "\nWindow should be open as maximized with /DISPLAY=Maximized switch"
  endif
  call setInstructions "Click 'Add Geo Layer' button to display layer &
                        and layer options. ^{moreInstructions}."
return

!******************************************************************************!
procedure handleLayerGeoOption
  sedit msg.name attribute "SUBS" "LG" ""
  if attribute EQSS "POS" then
    new nxm.sys.lib.Position(GEODETIC) p
    set p.alt 0.0
    if attribute EQS "POSLAT" then
      set p.lat ^msg.data
      set p.lon ^geolayer.pos.lon
    else
      set p.lat ^geolayer.pos.lat
      set p.lon ^msg.data
    endif

    set geoLayer.pos p
    assert/text="LayerGeo: Target Position" geoLayer.pos.lat EQ p.lat AND &
                                            geoLayer.pos.lon EQ p.lon
  elseif attribute EQSS "TARGETPOS" then
    new nxm.sys.lib.Position(GEODETIC) p
    set p.alt 0.0
    if "LAT" SUBS attribute then
      set p.lat ^msg.data
      set p.lon ^geolayer.pos.lon
    else
      set p.lat ^geolayer.pos.lat
      set p.lon ^msg.data
    endif
    set geoLayer.pos p

  elseif attribute EQSS "POINTPOS" then
    new nxm.sys.lib.Position(GEODETIC) p
    set p.alt 0.0
    if attribute EQSS "POINTPOSA"
      set letter "A"
    else
      set letter "B"
    endif
    if "LAT" SUBS attribute then
      set p.lat ^msg.data
      set p.lon ^geolayer.pointpos^{letter}.lon
    else
      set p.lat ^geolayer.pointpos^{letter}.lat
      set p.lon ^msg.data
    endif
    set geoLayer.pointPos^{letter} p

  elseif attribute EQSS "CONEANGLE" then
    if attribute EQS "CONEANGLE"
      set geoLayer.coneAngle ^msg.data
    else
      if attribute EQSS "CONEANGLEA"
        set letter "A"
      else
        set letter "B"
      endif
      set geoLayer.coneAngle^{letter} ^msg.data
    endif

  elseif attribute EQS "DRAWCONETOTARGET" then
    if msg.info EQ 1
      info "^reg.main.name: setting draw cone to target..."
      invoke xxx geoLayer.drawConeToTarget()
    else
      info "^reg.main.name: setting draw cone to pointing positions..."
      invoke xxx geoLayer.drawConeToPointPos()
    endif
      gcontrol menu lgHznAltEnabled "Draw Horizon at Altitude" "On" "On" /toggle /chkbox /tleft
      gcontrol DVAL lgHorizonAlt  "Horizon At Altitude" def.horizonAlt 0 -1 100 /tleft

  elseif attribute EQS "HZNALTENABLED" then
    if msg.info EQ 1
      info "^reg.main.name: enabling horizon at altitiude ..."
      invoke ,, geolayer.setFlags("+HORIZONALT")
    else
      invoke ,, geolayer.setFlags("-HORIZONALT")
    endif
  elseif attribute EQS "HORIZONALT" then
    invoke ,, geolayer.setHorizonAlt(msg.data)
  elseif attribute EQS "CALCAPPROACH" then
    if msg.data EQS VISUAL then
      res z:uselegacy false
    else
      res z:uselegacy true
    endif
    invoke ,, nxm.sys.libg.LayerGeo.useHorizonAltTraditionalCode(uselegacy)
  else
    if "COLOR" SUBS attribute then
      sedit attribute attribute "SUBS" "COLOR" ""
      if attribute EQS "A" or attribute EQS "B" then
        set geolayer.color^attribute ^msg.data
      elseif attribute EQS "AALT" then
        table clrtbl create {HZNATALTA=^msg.data}
        invoke ,, geolayer.setColorTable(^clrtbl)
      elseif attribute EQS "BALT" then
        table clrtbl create {HZNATALTB=^msg.data}
        invoke ,, geolayer.setColorTable(^clrtbl)
      else
        if "^msg.data" EQS "OTHER" then
          ! Popup a COLOR chooser if "OTHER" is given fo the color
          invoke colorTab geoLayer.getColorTable(true) ! get as objects
          set colorRGB colorTab.^{attribute}.getRGB
          say "Default ^attribute color ^colorTab.^{attribute}, RGB = ^colorRGB"
          popup/blocking=MACRO/title="Set ^attribute" result=color FUNC=COLOR MESSAGE="" DEFAULTS=colorRGB
          set geolayer.colorTable {^attribute=color}
        else
          set geolayer.colorTable {^attribute=^msg.data}
        endif
      endif
    else
      set geolayer.^attribute ^msg.data
    endif
  endif
return

!******************************************************************************!
procedure initializeLayerGeo

  call setInstructions "Should see\n&
                        - 2 RED ovals\n&
                        - 2 yellow lines with the word ^def.targetLabel in between\n&
                        - 2 green footprints that cover the target\n&
                        - label ^def.svaLabel near (-145,0)\n&
                        - label ^def.svbLabel near (-110,-6)\n&
                        - label ^def.siteLabel near (10,10)\n\n&
                        Click on:\n&
                        - various Layer Geo options\n&
                        - map to change the TDOA/FDOA lines\n&
                        - 'Show Site B' -> should see MYSITEB at (30,30)\n\n&
                        Change Grid Size X to 10 -> results are:\n&
                        -> TDOA Region and Contour lines are NOT as smooth, but\n&
                        still connect for region and span the map for contour."

  new nxm.sys.lib.Position(GEODETIC) site
  set site.lat 10
  set site.lon 10
  set site.alt 0
  layer "Geo" geoLayer {NAME=GL,SVA=^{datDir}.sv1.prm,SVB=^{datDir}.sv2.prm,&
                        SVALABEL="^def.svaLabel",SVBLABEL="^def.svbLabel",&
                        SITE=site,SITELABEL="^def.siteLabel",&
                        TARGETLABEL="^def.targetLabel",&
                        CONIC=def.conic,TDOAMODE=def.tdoaMode,FDOAMODE=def.fdoaMode,&
                        FLINKT=1e6,FLINKA=-1e6,FLINKB=-1e6,HORIZONALT=10000,&
                        FLAGS=^def.flagsStr,PREDICTREFERENCE=def.predictRef,TDOA=^def.tdoa,FDOA=^def.fdoa,&
                        DTDOA=^def.dtdoa,DFDOA=^def.dfdoa} idplot

  timex ssm t
  set geolayer.time ^t

  ! Set the target position
  new nxm.sys.lib.Position(GEODETIC) tPos
  set tPos.lat ^def.targetPos.lat
  set tPos.lon ^def.targetPos.lon
  set geolayer.pos tPos

  ! Set the pointing position A
  new nxm.sys.lib.Position(GEODETIC) pA
  set pA.lat ^def.pointPosA.lat
  set pA.lon ^def.pointPosA.lon
  set geolayer.pointposA pA

  ! Set the pointing position B
  new nxm.sys.lib.Position(GEODETIC) pB
  set pB.lat ^def.pointPosB.lat
  set pB.lon ^def.pointPosB.lon
  set geolayer.pointposB pB

  assert/text="LayerGeo: SVA, SVB labels" geoLayer.svalabel EQS "^def.svaLabel" AND &
                                          geoLayer.svblabel EQS "^def.svbLabel"
  assert/text="LayerGeo: SITE and TARGET labels" geoLayer.sitelabel EQS "^def.siteLabel" AND &
                                                 geoLayer.targetlabel EQS "TARGET"
  assert/text="LayerGeo: TDOA/FDOA Modes" geoLayer.TDOAMODE EQS "REGION" AND &
                                          geoLayer.FDOAMODE EQS "REGION"
  assert/text="LayerGeo: Target Position" geoLayer.pos.lat EQ def.targetPos.lat AND &
                                          geoLayer.pos.lon EQ def.targetPos.lon

  assert/text="LayerGeo: Pointing Position A" geoLayer.pointposA.lat EQ def.pointposA.lat AND &
                                              geoLayer.pointposA.lon EQ def.pointposA.lon

  assert/text="LayerGeo: Pointing Position B" geoLayer.pointposB.lat EQ def.pointposB.lat AND &
                                              geoLayer.pointposB.lon EQ def.pointposB.lon

return


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