!******************************************************************************!
! Test the plotting of KML (.kml) files.
!
! This macro plots the world.shp file and a large KML sample file. The sample
!  file contains several kml examples from Google as well as several custom
!  examples. The Google examples are generally small (buildings) several zooms
!  are required before you can actually see the objects.
!
! Author:  J. Bauer
!******************************************************************************!
startmacro/auto
  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 ALTTESTENV  alttestenv  GET DEF=FALSE SDEF=TRUE

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

  ! NXM-3845 since NeXtMidas 4.1.2
  if interAct then
    if ^{env.NMROOT} EQS "classpath:" then
      invoke writeAux Shell.getAuxPath(WRITE)
      set httpservDir "^{writeAux}"
      set kmlLinkTest "kmlLinkTestJar"
      file copy/f nxm.sys.test.kmlTest.kml      "^{writeAux}kmlTest.kml"
      file copy/f nxm.sys.test.kmlTestLeft.kml  "^{writeAux}kmlTestLeft.kml"
      file copy/f nxm.sys.test.kmlTestRight.kml "^{writeAux}kmlTestRight.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 "kmlLinkTest"
    endif

    call setDatDir

    set defaultInstructions ""
    set overlayText ""
    set campusText ""
    set pointText ""
    set pyramidText ""
    set linesText ""
    set redBoxText ""
    set httpsText ""
    set blueAirplaneText ""
    set refreshText ""
    call setText
    !Need to make sure the original file is the one in use - could get out of state because of refresh tests
    invoke ,, nxm.sys.test.LayerKmlTestHelper.setupLayerKmlTest

    pipe init

      httpserv 9080 DIR="^{httpservDir}" NAME="test"

      INFO "^reg.main.name You should see two (and ONLY two) WARNs and two INFOs for files at 'nxmtestbogusaddress.com' "

      panel/msgid=MAIN/setup/controls=gc
      plot/id=idplot/READOUT="+OnAxis"   ^{datDir}.world.shp|&
                                         nxm.sys.test.^{kmlLinkTest}.kml&
                                         {LAYER={ICONSIZE=10,ICONZOOMSIZE=48,COLORBLENDRATIO=0.8,ALLOWPOPUPS=FALSE}}|&
                                         nxm.sys.test.kmlStyledDescription.kml&
                                         {LAYER={ICONSIZE=10,ICONZOOMSIZE=48,COLORBLENDRATIO=0.8,ALLOWPOPUPS=FALSE}} &
                                         view=latloncontinuous

    
      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" "^defaultInstructions" 40 11 /title

      gcontrol button bOverlay ""         "Ground Overlay"
      gcontrol button bCampus ""          "Colored Buildings"
      gcontrol button bPoint ""           "Aeroplane Icon"
      gcontrol button bPyramid ""         "Pyramid"
      gcontrol button bLines ""           "Lines and Buildings"
      gcontrol button bRectangle ""       "Red Rectangle"
      gcontrol button bTriangle ""        "Blue Triangle"    ! Bug 2237
      gcontrol button bHttps ""           "HTTPS Link"       ! Bug 2243
      gcontrol button bStyle ""           "Blue Airplane Bahamas to NY"
      gcontrol button bRefresh ""         "Refresh Test"
      gcontrol button bView "Change View" "Lat/Lon,Geodetic" /tleft
      gcontrol button bForceRefresh "Force Refresh" "True,False" /tleft
      gcontrol button allowPopup "Allow Popup" "True,False" "False" /tleft
      gcontrol button clearLayers "Clear/Populate" "Clear,Populate"

    !!!! Updates to Contrast, Brightness, and Greyscale planned for future
    !!!!
    !!!!
      !gcontrol LABEL optionsLabel "Options"
       !gcontrol menu borderColor "" "BorderColor"  0 /tright /nodepress
       !gcontrol choice coloringMode "Coloring Mode" "^coloringModes" coloringMode /tleft
       ! slider FLAG serves to test Bug 1498
       !gcontrol lval opaqueNess   "OpaqueNess " 50 0 100 1 /tleft /slider
       !gcontrol lval alpha        "Alpha      " 127 0 255 1 /tleft /slider
       !gcontrol lval contrast     "Contrast   " 50 0 100 1 /tleft /slider
       !gcontrol lval brightness   "Brightness " 50 0 100 1 /tleft /slider


    pipe off
    invoke ,, nxm.sys.test.LayerKmlTestHelper.setRunning(false)
  endif

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

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

  ! Additional interactive tests, only run with /BASELINE
  if interAct and baseline then
    %test_plot_layerkml_bad                            ! Bug 2016
    if alttestenv isFalse then %test_plot_layerkml_kmz ! Bug 2339
    %test_plot_layerkml_small                          ! Bug 2256
    %test_plot_layerkml_true_false                     ! NXM-3701
  endif
endmacro

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

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

  call setInstructions "^defaultInstructions"

  if msg.name eqs "MAIN" then
    assert/text="^reg.main.name user indicated" msg.data eqss "PASS"
    pipe stop
  elseif msg.name EQS "OPAQUENESS" then
    set  kmlLayer.opaqueness ^msg.data
    set  gc.alpha.value shapeLayer.alpha
  elseif msg.name EQS "CONTRAST" then
    set  kmlLayer.contrast ^msg.data
  elseif msg.name EQS "BRIGHTNESS" then
    set  kmlLayer.brightness ^msg.data
  elseif msg.name EQS "ALPHA" then
    set kmlLayer.alpha ^msg.data
    set gc.opaqueness.value ^shapeLayer.opaqueness
  elseif msg.name EQS "COLORINGMODE" then
    !set shapeLayer.coloringMode ^msg.data
    !if msg.data EQS "ONSELECT" then
    !  invoke ,, shapeLayer.setSelectedValue(CODE,US,TRUE)
    !  call setInstructions &
    !    "Should see a plot of the world with the nations of the world. All nations &
    !     should be colored black, except the US which should be colored blue."
   ! endif

  !elseif msg.name EQS "COLORPOINTS" then
  !  set pointsLayer.color ^msg.data

  elseif msg.name EQS "BORDERCOLOR" then
  !  popup/blocking=MACRO/title="Set ^attribute" result=borderColor FUNC=COLOR DEFAULTS=^borderColor
  !  set shapeLayer.colorTable {border=^borderColor}

  elseif msg.data EQS "SHAPE-OPAQ" then
    call test_plot_shape_opaq

  elseif msg.data EQS "SHAPE-ONSELECT" then
    call test_plot_shape_onselect

  elseif msg.name eqs "BOVERLAY" then
    message send idplot,,view,,latlon
    message send ID=idplot NAME="ZOOM" DATA={X1=14,X2=16,Y1=36.5,Y2=38.5}
    set gc.idtext.value "^overlayText"
  elseif msg.name eqs "BCAMPUS" then
    message send idplot,,view,,latlon
    message send ID=idplot NAME="ZOOM" DATA={X1=-122.087,X2=-122.082,Y1=37.42,Y2=37.424}
    set gc.idtext.value "^campusText"
  elseif msg.name eqs "BPOINT" then
    message send idplot,,view,,latlon
    message send ID=idplot NAME="ZOOM" DATA={X1=-110,X2=-90,Y1=30,Y2=45}
    set gc.idtext.value "^pointText"
  elseif msg.name eqs "BPYRAMID" then
    message send idplot,,view,,latlon
    message send ID=idplot NAME="ZOOM" DATA={X1=15,X2=65,Y1=0,Y2=50}
    set gc.idtext.value "^pyramidText"
  elseif msg.name eqs "BLINES" then
    message send idplot,,view,,latlon
    message send ID=idplot NAME="ZOOM" DATA={X1=-112.28,X2=-112.24,Y1=36.07,Y2=36.11}
    set gc.idtext.value "^linesText"
  elseif msg.name eqs "BRECTANGLE" then
    message send idplot,,view,,latlon
    message send ID=idplot NAME="ZOOM" DATA={X1=160,X2=190,Y1=-20,Y2=40}
    set gc.idtext.value "^redBoxText"
  elseif msg.name eqs "BTRIANGLE" then
    message send idplot,,view,,latlon
    message send ID=idplot NAME="ZOOM" DATA={X1=-25,X2=25,Y1=-25,Y2=25}
    set gc.idtext.value "^blueTriangleText"
  elseif msg.name eqs "BHTTPS" then
    message send idplot,,view,,latlon
    message send ID=idplot NAME="ZOOM" DATA={X1=-15,X2=15,Y1=-45,Y2=-55}
    set gc.idtext.value "^httpsText"
  elseif msg.name eqs "BSTYLE" then
    message send idplot,,view,,latlon
    message send ID=idplot NAME="ZOOM" DATA={X1=-90,X2=-65,Y1=15,Y2=45}
    set gc.idtext.value "^blueAirplaneText"
  elseif msg.name eqs "BREFRESH" then
    invoke ,, nxm.sys.test.LayerKmlTestHelper.startSwapping()
    set gc.idtext.value "^refreshTest"
  elseif msg.name eqs "BVIEW" then
    if msg.data eqs "GEODETIC" then
     message send idplot,,view,,geo
    else
      message send idplot,,view,,latlon
    endif
  elseif msg.name eqs "BFORCEREFRESH" then
    set gc.idtext.value "^refreshText"
    set reg.idplot.layers.^{kmlLinkTest}.forceRefresh ^msg.data
  elseif msg.name eqs "ALLOWPOPUP" then
    set gc.idtext.value "When Allow Popup is TRUE, you can hold the left mouse button &
                         down for 1/2 a second the mouse over a point or KML object &
                         and a small popup will appear.\n\nWhen FALSE, &
                         the popup does not appear. Only that point is effected by the button."
    set reg.idplot.layers.kmlnettest.allowpopups ^msg.data
    set reg.idplot.layers.^{kmlLinkTest}.allowpopups ^msg.data
    set reg.idplot.layers.kmlStyledDescription.allowpopups ^msg.data
  elseif msg.name eqs "CLEARLAYERS" then
    if msg.data eqs "CLEAR" then
      invoke ,, reg.idplot.clearData()
    else
      message FUNC=send ID=IDPLOT NAME=OPENFILE DATA="nxm.sys.test.^{kmlLinkTest}.kml{LAYER={ICONSIZE=10,ICONZOOMSIZE=48,COLORBLENDRATIO=0.8,ALLOWPOPUPS=FALSE}}"
    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 setInstructions s:msgString
  set reg.panel.controls.idtext.value msgString
return

!******************************************************************************!
procedure setText
  set defaultInstructions &
      "Should see a plot of the world with the nations of the world. There &
      are several different KML objects plotted on the map. Click the buttons &
      or Zoom in on specific objects."

  set overlayText "Should see a ground overlay &
                   image of a portion of Sicily, Italy."

  set campusText "You should see four buildings colored blue, red, green &
      and yellow. The buildings are 3d when in Geodetic view"

  set pointText "Should see a point using an Airplane icon pointing SouthEast, with red text reading 'Point (Image)', &
      and a point using a semi-transparent grey 'Aero' symbol, pointing NorthEast.\n&
      Left Click (hold for ~1 sec then release) on the SE Point (Image) should produce a popup with html-formatted text.\n&
      Left Click... on the NE facing Aero Symbol should *NOT* show a popup. "

  set pyramidText "In Egypt, you should see a large (Square) Pyramid and a &
      semi-transparent cone around it, projected from space. In Geodetic view, &
      this is more apparent."

  set linesText "You should see a thick yellow/green line joined to a purple &
      line joined to a thick black line. In Geodetic mode, these extrude."

  set redBoxText "You should see half a semi-transparent red rectangle and &
      half an airplane image that each cross the international date line. In &
      Geodetic view, the rectangle and plane should be whole."

  set refreshText "Click TRUE and within 5 seconds (the poll rate), you should &
      see the plot 'blink' as it refreshes. Click FALSE and the 'blinking' &
      should stop."

  set blueTriangleText "You should see a light blue triangle. "
  
  set blueAirplaneText "Make sure 'Allow Popup' is 'True' and then click and hold on the blue airplane.\n\n&
                        A stylized popup with a large description should appear"

  set httpsText "(Requires Internet access). You should see the Google logo image reversed."

  set refreshTest "(Requires Internet access) You should see the google image changing positions. &
                   It could take up to 5 seconds to notice any change."
return
