!******************************************************************************!
! Tests new continuous lat/lon mode for LayerShape with LayerT5
! Additional/Alternate testing of the following layers can be enabled via argument,
! switch, or button: LayerPoly (argument), LayerGeo (button),
! and LayerKML, LayerVehicle, & LayerImage via switch
! NOTE: Support for LayerKML objects spanning boundaries has NOT yet been added
!
! Arguments: file_ext optional way to specify file extension of world fiel used
!                      (either "prm" or "shp", where "shp" is the default
!
! Switches: /ADDKML     optional way to add/test LayerKML
!           /ADDVEHICLE optional way to add/test LayerVehicle
!           /ADDIMAGE   optional way to add/test LayerImage
!
! @bug      NXM-3284 Need Continuous Lat/Lon Plot
! @since    3.9.0
! @author:  Jeanette McCall
!******************************************************************************!
startmacro S:file_ext["shp"] 
  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 COREGRAPHICS coreGraphics GET DEF=FALSE SDEF=TRUE  ! for a quick check of graphics - more extreme tests - since 3.8.1
  switch ADDKML       addKML       GET DEF=FALSE SDEF=TRUE
  switch ADDVEHICLE   addVehicle   GET DEF=FALSE SDEF=TRUE
  switch ADDIMAGE     addImage     GET DEF=FALSE SDEF=TRUE

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

  if interAct or coreGraphics then
    call setDatDir

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

    ! Option Layers to Plot
    res kmlLayer     ""
    res vehicleLayer ""
    res imageLayer   ""
    if addKML then
      ! NXM-3845 since NeXtMidas 4.1.1
      if ^{env.NMROOT} EQS "classpath:" then
        invoke writeAux Shell.getAuxPath(WRITE)
        set httpservDir "^{writeAux}"
        set kmlLinkTest "nxm.sys.test.kmlLinkTestJar.kml"
        file copy/f nxm.sys.test.kmlTest.kml "^{writeAux}kmlTest.kml"
        file copy/f nxm.sys.test.kmlNetTest  "^{writeAux}kmlNetTest.kml" ! extension-less kml does not work from inside a jar file
        info "^reg.main.name: SKIPPING extension-less kml since does not work when running from a jar file, &
               copying kmlNetTest to kmlNetTest.kml for this test."
      else
        set httpservDir "nxm/sys/test"
        set kmlLinkTest "nxm.sys.test.kmlLinkTest.kml"
      endif
      res kmlLayer "|^{kmlLinkTest}{LAYER={ICONSIZE=10,ICONZOOMSIZE=48,COLORBLENDRATIO=0.8,ALLOWPOPUPS=FALSE}}"
    endif
    if addVehicle then
      res vehicleLayer "|{LT=Vehicle,CONEFILL=TRUE}|{LT="nxm.sys.test.test_plot_layervehicle$LayerVehicleFixedPosp",CONEFILL=TRUE}"
    endif
    if addImage then
      res imageLayer "|^{datDir}.Sunset.jpg{xs=-10,ys=20,xd=0.03,yd=0.04}"
    endif

    ! Set Defaults
    call setGeoDefaults
    call defineVehicleSettings
    call defineFeatureDefaults

    set instructions "Should see a plot of the world with various layers and with a longitude of -180 to 180:\n\n&
                      1) Use the middle mouse button to pan through the continuous longitude.\n\n&
                      2) Click the 'add geo' button to add that layer and pan left and right.\n\n&
                      3) Pan over and DRAGBOX zoom to and area near Alaska spanning the meridian boundary.\n\n&
                      4) Click 'Save Plot Limits' and type in the name of your limits file, click 'Enter' to save the name, &
                         and then click 'Submit' to save your limits.\n\n&
                      5) Click 'ZOOM ALL THE WAY OUT' and pan to a different section of the map.\n\n&
                      6) Click 'Load Plot Limits' and select your limits file and then click 'Submit'.\n\n&
                      7) The plot should move back to your saved limits.\n\n"

    pipe init
      if addKml then httpserv 9080 DIR="^{httpservDir}" NAME="test"
  
      ! Change the keyMap to custom settings
      env set KEYMAP {PLOT={LEFT="L",RIGHT="R",in=i,down=d,up=u,out=o}}

      panel/setup/controls=gc
      !/usemouseposition
      plot/msgid=MAIN/id=idplot/mousewheelzoom/USEMOUSEPOSITION/READOUT="+OnAxis"/zoomadj=.1/DBLCLICKZOOM &
              ^{datDir}.world.^file_ext|^{datDir}.capital_cities.prm^{kmlLayer}^{vehicleLayer}^{imageLayer} view=latloncontinuous

      call addFeatures

      if addVehicle then
        call initializeLayerVehicle
      endif

      gcontrol label mainLabel "Main"
      gcontrol menu main "Exit with Status = " "PASS,FAIL" /tleft
      set gc.main.itemColors {PASS=GREEN,FAIL=RED}
      gcontrol text idtext "Instructions" instructions 46 28 /title

      gcontrol button saveLimits "" "Save Plot Limits" /tleft
      gcontrol button loadLimits "" "Load Plot Limits" /tleft

      gcontrol/nc=1 button zoomallout "" "ZOOM ALL THE WAY OUT"
      gcontrol/nc=1 button changelimits "" "change plot limits"
      gcontrol/nc=1 button addgeo "" "add geo"

      gcontrol button dblclick "DOUBLE CLICK ZOOMS" "DISABLE,ENABLE" "ENABLE" /ChkBox
    pipe off
  endif

  ! Cleanup test files
  if addKml AND ^{env.NMROOT} EQS "classpath:" then files/e "^{writeAux}*kml*Test*.kml"

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

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

  !say "msg.name ^msg.name, data ^msg.data"

  if msg.name eqs "MAIN" then
    assert/text="Plot ZOOM tests" msg.data eqss "PASS" 
    ! Set keyMap back to default
    res t:km "nxm.sys.cfg.keyMap.tbl"
    env set keymap ^km
    pipe stop
  elseif msg.name eqs "ZOOMALLOUT" then
    message FUNC=sendw ID=IDPLOT NAME=ZOOMOUT DATA=-1
  elseif msg.name eqs "DBLCLICK" then
    if msg.data eqs "DISABLE" then
      message FUNC=sendw ID=IDPLOT NAME=DBLCLICK DATA=FALSE
    elseif msg.data eqs "ENABLE" then
      message func=sendw id=IDPLOT name=DBLCLICK data=TRUE 
    endif 
  elseif msg.name eqs "SAVELIMITS" then
    message FUNC=send ID=idplot MSG="LIMITS" NAME="LIMITS" INFO=0 DATA="SAVE"
  elseif msg.name eqs "LOADLIMITS" then
    message FUNC=send ID=idplot MSG="LIMITS" NAME="LIMITS" INFO=0 DATA="LOAD"
  elseif msg.name eqs "CHANGELIMITS" then
    say "change limits"
    invoke ,, idplot.setScale("FIX")
    message send idplot name=openfile data="^{datDir}.world.^file_ext" quals={outerlimits={xmin=90,xmax=270,ymin=0,ymax=85},viewlimits={xmin=100,xmax=200,ymin=0,ymax=85}}
  elseif msg.name eqs "ADDGEO" then
    call initializeLayerGeo
  elseif msg.name eqs "ADDVEHICLE" then
    call initializeLayerVehicle
  elseif msg.fid eqs "IDPLOT"  then
    if msg.name eqs "DRAGBOX" then
      set box1.setBoundsFromTable(msg.data)
    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
  ! Automatically called
return

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

  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,&
                        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
  
return

!******************************************************************************!
procedure setGeoDefaults

  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"

  set def.gridsizex   32
  set def.gridsizey   22

  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"

  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_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)

return

!******************************************************************************!
procedure initializeLayerVehicle

  set lay   reg.idplot.layers.vehicle
  set layfp reg.idplot.layers.VEHICLEFIXEDPOSP

  set lay.vehicle("MYVEH1") myveh1
  set lay.vehicle("MYVEH2") myveh2
  set lay.vehicle("MYVEH3") myveh3
  set lay.vehicle("MYVEH4") myveh4


  invoke ,, layfp.setFixedPosp(0,-45,-45)
  set layfp.vehicle("MYVEH1") myveh1
  invoke mv1 layfp.getVehicle("MYVEH1")
  invoke nxm.sys.test.test_plot_layervehicle.setSampleFootprint(mv1,"UPLINK")

  new nxm.sys.lib.StateVector sv4
  invoke ,, sv4.setGeo(0,51,0)
  invoke ,, sv4.setHeadingSpeed(315,500)
  set lay.vehicles.myveh4.location.sv sv4

return

!******************************************************************************!
procedure defineVehicleSettings

  set myveh1 {POINTS={}}
  set myveh1.attributes     {COLOR=Yellow,Symbol=Cross}
  set myveh1.location       {LABEL="SPACEWAY-1",POS={ALT=3.49E7,LAT=0,LON=-102.8}}
  set myveh1.location.ATTRIBUTES {SYMBOL=DIAMOND}
  set myveh1.site           {LABEL="El Segundo, CA",POS={ALT=0,LAT=33'55'17,LON=-118'24'22}}
  set myveh1.points.UPLINK  {POS={ALT=0,LAT=33'55'17,LON=-118'24'22},CANGLE=0.4,LABEL=""}
  set myveh1.points.UPLINK.ATTRIBUTES {SYMBOL=Plus,Color=Purple}

  set myveh2 {POINTS={}}
  set myveh2.attributes     {COLOR=Cyan,Symbol=Cross}
  set myveh2.location       {LABEL="Radiosat 1",POS={ALT=1.79E7,LAT=63.8,LON=-100}}
  set myveh2.location.ATTRIBUTES {SYMBOL=DIAMOND}
  set myveh2.site           {LABEL="Vernon, NJ",POS={ALT=0,LAT=41.2,LON=-74.5}}
  set myveh2.points.UPLINK  {POS={ALT=0,LAT=41.2,LON=-74.5},CANGLE=0.4,LABEL=""}
  set myveh2.points.UPLINK.ATTRIBUTES {SYMBOL=Plus,Color=Blue}

  ! This one tests Bug 1280 with respect to a clipping problem near the poles
  set myveh3 {POINTS={}}
  set myveh3.attributes     {COLOR=Magenta,Symbol=Cross}
  set myveh3.location       {LABEL="BUG1280",POS={ALT=3.49E7,LAT=10,LON=0}}
  set myveh3.site           {ATTRIBUTES={VISIBLE=FALSE}}

  ! This one tests Bug 1404 with respect to symbol angle
  set myveh4 {POINTS={}}
  set myveh4.attributes     {COLOR=Orange,Symbol=Aero,SymbolSize=12,SymbolFill=TRUE}
  set myveh4.location       {LABEL="AIRPLANE",POS={ALT=0,LAT=0,LON=0}} ! StateVector set after layer is up
  set myveh4.site           {ATTRIBUTES={VISIBLE=FALSE}}

return

!******************************************************************************!
procedure defineFeatureDefaults

  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

return

!******************************************************************************!
procedure addFeatures

  feature label=fellipse table={NAME="ELLIPSE",TYPE="ELPS",X=0,Y=7.5,&
                                SEMIMAJOR=semiMaj,SEMIMINOR=semiMin,TILT=90,&
                                COLOR="GREEN"} plot=idplot

  ! 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=idplot

  feature label=rellipse table={NAME="RIGHT",TYPE="ELPS",X=175,Y=7.5,&
                                SEMIMAJOR=semiMaj,SEMIMINOR=semiMin,TILT=90,&
                                COLOR="YELLOW"} plot=idplot

  FEATURE/id=pinktri label=pinktriangle table={NAME=pinktriangle,TYPE=TRI|FILLED,X=-180,Y=-20,DX=20,COLOR=PINK,ENABLE=1} plot=idplot

  FEATURE/id=redCircle label=redCircle table={NAME=redCircle,TYPE=CIRCLE|FILLED,X=-180,Y=-60,DX=20,COLOR=RED,ENABLE=1} plot=idplot

  FEATURE label=box1 table={NAME=BOX1,TYPE=BOX,COLOR=WHITE,ENABLE=1} plot=idplot  ! add the feature

return
