!******************************************************************************!
! Test that covers a lot of Jeremy's Gak option tree
! LayerDrawable has multiple LayerT5s and possibly other layers associated with it
! Features can be added
!
! @bug      NXM-3647 Feature: IconSymbol text drawn in wrong location
!           NXM-3650 Location and direction of track data
!           Multiple LayerT5 Point Selection / DeSelection issues
!           and more
! @since    4.0.0
! @author:  Jeanette McCall
!******************************************************************************!
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)

  call setDatDir

  set defaultFeatureText "Feature Title\nKEY1=first line\nKEY2=very long second line\nKEY3=line3"
  res shapefile "^{datDir}.world.shp" ! SET DEFAULT SHAPE FILE
  ! T5 snake files
  res snake1color "AQUA"
  res snake2color "MAGENTA"
  res snake1 "^snake1color SNAKE"
  res snake2 "^snake2color SNAKE"
  ! open / close file button states
  res openShape "ADD SHAPE"
  res closeShape "CLOSE SHAPE"
  res openSnake1 "ADD ^snake1"
  res closeSnake1 "CLOSE ^snake1"
  res openSnake2 "ADD ^snake2"
  res closeSnake2 "CLOSE ^snake2"
  res openDiagonal "ADD DIAGONAL"
  res closeDiagonal "CLOSE DIAGONAL"
  res openRandom "ADD RANDOM"
  res closeRandom "CLOSE RANDOM"
  ! open / close file default button states
  res openCloseShape openShape
  res openCloseSnake1 openSnake1
  res openCloseSnake2 openSnake2
  res openCloseDiagonal openDiagonal
  res openCloseRandom openRandom
  ! hide / show file button states
  res hideShape "HIDE SHAPE"
  res showShape "SHOW SHAPE"
  res hideSnake1 "HIDE ^snake1"
  res showSnake1 "SHOW ^snake1"
  res hideSnake2 "HIDE ^snake2"
  res showSnake2 "SHOW ^snake2"
  res hideDiagonal "HIDE DIAGONAL"
  res showDiagonal "SHOW DIAGONAL"
  res hideRandom "HIDE RANDOM"
  res showRandom "SHOW RANDOM"
  ! hide / show file default button states
  res hideShowShape hideShape
  res hideShowSnake1 hideSnake1
  res hideShowSnake2 hideSnake2
  res hideShowDiagonal hideDiagonal
  res hideShowRandom hideRandom

  res clusterDecluster "CLUSTER"

  if interAct then
    set instructions "* Demo/testing similar to GAK option tree *\n&
                      Test the following\n&
                      - pipe opening, closing, hiding, showing, clearing\n&
                      - proper point selection and deselection under those conditions\n&
                      - icon feature text at proper location, even when zoomed"

    set ellipseColumnNames "D:ALT,D:LAT,D:LON,D:ETILT,D:EMIN,D:EMAJ,NAME,D:TIME,B:F1,B:F2,B:F3,B:F4,B:F5,B:F6,B:F7,B:F8,Z:NOTE"
    set ellipseLayerCSV "LT=T5,TimeOrder=TRUE,Group=t5Group,NAME=CSV,SIZE=6,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=PINK,X=LON,Y=LAT,Z=ALT,LABEL=NAME,TIMELF=TIME,METAPOS=METADATA"
    set qualsEllipseCSV {HEADERROWS=L:1,COLUMNNAMES="^ellipseColumnNames",LAYER={^ellipseLayerCSV},TAG=CSVFILE}
    set ellipselayerPRM "LT=T5,TimeOrder=True,Group=t5Group,NAME=PRM,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=AQUA"
    set ellipselayerPIPE "LT=T5,TimeOrder=False,Group=drawablegrp,NAME=DIAG,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=AQUA,SELECTMESSAGES=TRUE"
    set ellipselayerPIPE2 "LT=T5,TimeOrder=False,Group=drawablegrp,NAME=QUAD,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=WHITE,SELECTMESSAGES=TRUE"
    set ellipselayerSNAKE1 "LT=T5,TimeOrder=False,Group=drawablegrp,NAME=SNAKE1,ColorFade=TRUE,ColorFadeFactor=1,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=BLUE,SELECTMESSAGES=TRUE"
    set ellipselayerSNAKE2 "LT=T5,TimeOrder=False,Group=drawablegrp,NAME=SNAKE2,ColorFade=TRUE,ColorFadeFactor=1,FadeColor=0x200000,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=BLUE,SELECTMESSAGES=TRUE"

    set has1stLayer false
    set has2ndLayer false

    invoke booleanfalse java.lang.Boolean.valueOf("false")
    invoke booleantrue java.lang.Boolean.valueOf("true")
    invoke origT5Setting   nxm.sys.libg.LayerT5.getAutoAdjustLineColor()
    invoke origPolySetting nxm.sys.libg.LayerPoly.getAutoAdjustLineColor()
    invoke ,, nxm.sys.libg.LayerT5.setAutoAdjustLineColor(^booleantrue)
    invoke ,, nxm.sys.libg.LayerPoly.setAutoAdjustLineColor(^booleantrue)

    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

      nxm.sys.test.plotellipsetester/DIAGONAL/DIRECTCOLOR=YELLOW/NOFLAGS/NOTEMODETYPE=4Z/RT/RATE=500/STARTTIME="2007:01:02::00:00:00" OUT=_t5pipe{SIZE=10000} ELEM=Inf
      nxm.sys.test.plotellipsetester/BQ/COLOR=3/DIRECTCOLOR=PURPLE/RT/NOTEMODETYPE=VA/RATE=700 OUT=_t5pipe2{SIZE=10000} ELEM=Inf

      plot/id=timepipe/msgid=main world|{LT="Drawable",name=drawablegrp,listlayers=true} readoutExt=+SHOW|+POINTS &
               readout=+OnAxis|+AlwaysDeltas view=latloncontinuous Y1=-90 Y2=90

      gcontrol line spaceFile "SomeWorthlessText" "BLUE" 7 ,, "CENTER"    ! Blue spacer
      gcontrol label fileSection "File / Pipe Control Section"
      gcontrol file shapefileselection "Shape File Selection" &
          ^shapefile "*.shp" "1|CWD|DAT" 60 10 /FLAGS="EXT"
      gcontrol button openClose "Open/Close File" &
          "^openCloseShape,^openCloseSnake1,^openCloseSnake2,^openCloseDiagonal,^openCloseRandom" ""
      gcontrol button hideShow "Hide/Show File" &
          "^hideShowShape,^hideShowSnake1,^hideShowSnake2,^hideShowDiagonal,^hideShowRandom" ""
      gcontrol button clearLayers "Clear Layers" "ALL,^snake1,^snake2,DIAGONAL,RANDOM" ""
      gcontrol/nc=1 button cluster "Turn clustering off/on" "^clusterDecluster"

      gcontrol line spaceFeat "SomeWorthlessText" "BLUE" 7 ,, "CENTER"    ! Blue spacer
      gcontrol label featSection "Feature Control Section"
      gcontrol prompt featTextPrompt "Update Feature Text here:" ""
      gcontrol text featureText "Feature Text" defaultFeatureText 24 4 /title
      gcontrol button featureButt "Add IconSymbol Feature with Text location:" "default,RTAG,TTAG,LTAG,BTAG,DTAG,CUSTOMTAG" ""
      gcontrol button customTagRef "Custom Tag Reference Location" "TR,TL,BR,BL,CTR" "TR"
      gcontrol lval tagx "Custom Tag X offset" 0 -50 50 1 /SLIDER
      gcontrol lval tagy "Custom Tag Y offset" 0 -50 50 1 /SLIDER
      FEATURE label=pipebox table={NAME=PIPEBOX,TYPE=BOX,COLOR=AQUA,ENABLE=1} plot=timepipe
      shellgui/id=idshellgui/attach/rows=15

    pipe off

    invoke ,, nxm.sys.libg.LayerT5.setAutoAdjustLineColor(^origT5Setting)
    invoke ,, nxm.sys.libg.LayerPoly.setAutoAdjustLineColor(^origPolySetting)

  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 "SELECT" OR msg.name EQS "DESELECT" then
    info "^msg.name MESSAGE"
    say "^msg.name ^msg.data"
  elseif msg.name eqs "FEATTEXTPROMPT" then
    say msg.data
    res gc.featureText.value "^msg.data"
  elseif msg.name eqss "SHAPEFILESELECTION" then
    res shapefile ^msg.data
  elseif msg.name EQS "OPENCLOSE" then
    if msg.data      EQS ^openShape then
      res openCloseShape ^closeShape
      invoke ,, reg.timepipe.openFile(^shapeFile{Layer={Group=drawablegrp,NAME=SHAPE}})
    elseif msg.data EQS ^closeShape then
      res openCloseShape ^openShape
      res done reg.timepipe.layers.drawablegrp.removeDrawable(SHAPE)
    elseif msg.data EQS ^openSnake1 then
      res openCloseSnake1 ^closeSnake1
      invoke snakeSpeed java.lang.Math.random()
      calc snakeRate 50 snakeSpeed /
      nxm.sys.test.plotellipsetester/SNAKE/DIRECTCOLOR=^snake1color/NOFLAGS/RT/NOTEMODETYPE=VA/RATE=^snakeRate OUT=_snake1{SIZE=10000} ELEM=Inf /bg
      invoke ,, reg.timepipe.openFile(_snake1{LAYER={^ellipselayerSNAKE1,FormRegions=true},TAG=SNAKE1PIPE})
    elseif msg.data EQS ^closeSnake1 then
      res openCloseSnake1 ^openSnake1
      res done reg.timepipe.layers.drawablegrp.removeDrawable(^snake1)
    elseif msg.data EQS ^openSnake2 then
      res openCloseSnake2 ^closeSnake2
      invoke snakeSpeed java.lang.Math.random()
      calc snakeRate 50 snakeSpeed /
      res snakeRate
      nxm.sys.test.plotellipsetester/SNAKE/DIRECTCOLOR=^snake2color/NOFLAGS/RT/NOTEMODETYPE=VA/RATE=^snakeRate OUT=_snake2{SIZE=10000} ELEM=Inf /bg
      invoke ,, reg.timepipe.openFile(_snake2{LAYER={^ellipselayerSNAKE2,FormRegions=true},TAG=SNAKE2PIPE})
    elseif msg.data EQS ^closeSnake2 then
      res openCloseSnake2 ^openSnake2
      res done reg.timepipe.layers.drawablegrp.removeDrawable(^snake2)
    elseif msg.data EQS ^openDiagonal then
      res openCloseDiagonal ^closeDiagonal
      invoke ,, reg.timepipe.openFile(_t5pipe{LAYER={^ellipselayerPIPE,FormRegions=true},TAG=DIAGPIPE})
    elseif msg.data EQS ^closeDiagonal then
      res openCloseDiagonal ^openDiagonal
      res done reg.timepipe.layers.drawablegrp.removeDrawable(DIAG)
    elseif msg.data EQS ^openRandom then
      res openCloseRandom ^closeRandom
      invoke ,, reg.timepipe.openFile(_t5pipe2{LAYER={^ellipselayerPIPE2,FormRegions=true},TAG=QUADPIPE})
    elseif msg.data EQS ^closeRandom then
      res openCloseRandom ^openRandom
      res done reg.timepipe.layers.drawablegrp.removeDrawable(QUAD)
    else
      say "OPENCLOSE unsupported for ^{msg.data}"
    endif
    invoke ,, gc.openclose.setItems("^openCloseShape,^openCloseSnake1,^openCloseSnake2,^openCloseDiagonal,^openCloseRandom")
  elseif msg.name EQS "HIDESHOW" then
    if msg.data      EQS ^hideShape then
      if openCloseShape EQS ^closeShape then
        res hideShowShape ^showShape
        res done reg.timepipe.layers.drawablegrp.getLayer(SHAPE).setEnable(-GLOBAL)
      endif
    elseif msg.data EQS ^showShape then
      res hideShowShape ^hideShape
      res done reg.timepipe.layers.drawablegrp.getLayer(SHAPE).setEnable(+GLOBAL)
    elseif msg.data EQS ^hideSnake1 then
      if openCloseSnake1 EQS ^closeSnake1 then
        res hideShowSnake1 ^showSnake1
        res done reg.timepipe.layers.drawablegrp.getLayer(SNAKE1).setEnable(-GLOBAL)
      endif
    elseif msg.data EQS ^showSnake1 then
      res hideShowSnake1 ^hideSnake1
      res done reg.timepipe.layers.drawablegrp.getLayer(SNAKE1).setEnable(+GLOBAL)
    elseif msg.data EQS ^hideSnake2 then
      if openCloseSnake2 EQS ^closeSnake2 then
        res hideShowSnake2 ^showSnake2
        res done reg.timepipe.layers.drawablegrp.getLayer(SNAKE2).setEnable(-GLOBAL)
      endif
    elseif msg.data EQS ^showSnake2 then
      res hideShowSnake2 ^hideSnake2
      res done reg.timepipe.layers.drawablegrp.getLayer(SNAKE2).setEnable(+GLOBAL)
    elseif msg.data EQS ^hideDiagonal then
      if openCloseDiagonal EQS ^closeDiagonal then
        res hideShowDiagonal ^showDiagonal
        res done reg.timepipe.layers.drawablegrp.getLayer(DIAG).setEnable(-GLOBAL)
      endif
    elseif msg.data EQS ^showDiagonal then
      res hideShowDiagonal ^hideDiagonal
      res done reg.timepipe.layers.drawablegrp.getLayer(DIAG).setEnable(+GLOBAL)
    elseif msg.data EQS ^hideRandom then
      if openCloseRandom EQS ^closeRandom then
        res hideShowRandom ^showRandom
        res done reg.timepipe.layers.drawablegrp.getLayer(QUAD).setEnable(-GLOBAL)
      endif
    elseif msg.data EQS ^showRandom then
      res hideShowRandom ^hideRandom
      res done reg.timepipe.layers.drawablegrp.getLayer(QUAD).setEnable(+GLOBAL)
    else
      say "HIDESHOW unsupported for ^{msg.data}"
    endif
    invoke ,, gc.hideShow.setItems("^hideShowShape,^hideShowSnake1,^hideShowSnake2,^hideShowDiagonal,^hideShowRandom")
  elseif msg.name EQS "CLEARLAYERS" then
    if msg.data      EQS "ALL" then
      invoke ,, reg.timepipe.clearData()
    elseif msg.data EQS ^snake1 then
      if openCloseSnake1 EQS ^closeSnake1 then
        res done reg.timepipe.layers.drawablegrp.getLayer(SNAKE1).clear()
      endif
    elseif msg.data EQS ^snake2 then
      if openCloseSnake2 EQS ^closeSnake2 then
        res done reg.timepipe.layers.drawablegrp.getLayer(SNAKE2).clear()
      endif
    elseif msg.data EQS "DIAGONAL" then
      if openCloseDiagonal EQS ^closeDiagonal then
        res done reg.timepipe.layers.drawablegrp.getLayer(DIAG).clear()
      endif
    elseif msg.data EQS "RANDOM" then
    if openCloseRandom EQS ^closeRandom then
        res done reg.timepipe.layers.drawablegrp.getLayer(QUAD).clear()
      endif
    else 
      say "CLEARLAYERS unsupported for ^{msg.data""
    endif
  elseif msg.name eqs "CLUSTER" then
    if clusterDecluster EQS "CLUSTER"
      res clusterDecluster "NO CLUSTERING"
      invoke ,, reg.timepipe.layers.drawablegrp.setShowClusters(^booleanTrue)
      invoke ,, gc.cluster.setItems("NO CLUSTERING")
    else
      res clusterDecluster "CLUSTER"
      invoke ,, reg.timepipe.layers.drawablegrp.setShowClusters(^booleanFalse)
      invoke ,, gc.cluster.setItems("CLUSTER")
    endif
  elseif msg.name eqs "FEATUREBUTT" then
    if msg.data EQS DEFAULT
      call addFeature ""
    elseif msg.data EQS CUSTOMTAG
      res tagx gc.tagx.value
      res i:tagy gc.tagy.value
      res tagref gc.customTagRef.value
      call addCustomFeature
    else
      call addFeature msg.data
    endif
  else

  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

!*****************************************************************************
! create blue file for this test
!*****************************************************************************
procedure createBlue

  %CONVERT_CSV_TO_T5000 ^csvFile ^grpTable ^test_blue_file rf=ecr

return

!*****************************************************************************
! add Feature
!*****************************************************************************
procedure addFeature s:tagtype
res tagtype
  res addon ""
  if tagtype NEQS "" then
    res addon "|^tagtype"
  endif
  res addon
  invoke xvalue nxm.sys.test.plotellipsetester.randomLon()
  invoke yvalue nxm.sys.test.plotellipsetester.randomLat()
  res name "^gc.featuretext.value"
  feature label=icon       table={NAME="^name",TYPE=SYMBOL|TEXT^{addOn},X=^xvalue,Y=^yvalue,&
                                  LINE={SYMBOL="^{datDir}.Arrow.png"}} &
                                  plot=timepipe
return

!*****************************************************************************
procedure addCustomFeature

  invoke xvalue nxm.sys.test.plotellipsetester.randomLon()
  invoke yvalue nxm.sys.test.plotellipsetester.randomLat()
  feature label=icon       table={NAME="^name",TYPE=SYMBOL|TEXT|CUSTOMTAG,X=^xvalue,Y=^yvalue,&
                                  CUSTOMTAGXPIX=^tagx,CUSTOMTAGYPIX=^tagy,CUSTOMTAGREF=^tagref,LINE={SYMBOL="^{datDir}.Arrow.png"}} &
                                  plot=timepipe
return
