!******************************************************************************!
! Tests highlight filter on display of LayerT5 points
!
! @bug      NXM-3862 Need highlight filtering on which LayerT5 points Are displayed
!           NXM-3914 Support for highlight filter that is separate from visibility filter
!           NXM-3947 Support for unlimited number of highlight filters and colors
! @since    4.1.1
! @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)

  if interAct then
    call setDatDir

    ! Process the csv file
    res test_blue_file ellipseflagsmeta5000.tmp
    res csvFile        nxm.sys.test.ellipseflagsmeta5000.csv
    res grpTable       nxm.sys.test.ellipseflagsmeta5000grp.tbl
    call createBlue

    ! Create the starting highlight filters
    res t:defaults {csv={firstHS={key="NAME",pred="STARTS",val="J",color="PINK"},&
                         secondHS={key="POS.LON",pred="LE",val=50,color="WHITE"}},&
                    snake1={firstHS={key="POS.LON",pred="GT",val=-90,color="PINK"},&
                            secondHS={key="POS.LON",pred="LE",val=-90,color="ORANGE"}},&
                    snake2={firstHS={key="POS.LON",pred="GT",val=90,color="PINK"},&
                            secondHS={key="POS.LON",pred="LE",val=90,color="ORANGE"}}}
    call createHighlightFilters useDefaults=true

    ! Set the attributes for each of the snake plots
    res snake1color "YELLOW"
    res snake2color "MAGENTA"
    res snake1 "^snake1color SNAKE"
    res snake2 "^snake2color SNAKE"
    res snakerate 30
    invoke ,, nxm.sys.test.t5pathgenerator.setVaryNote(true)

    ! Set the attributes for each LayerT5
    invoke L:symbolsField nxm.sys.libg.LayerT5.SYMBOLS
    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"
    set ellipseLayerCSV "LT=T5,TimeOrder=TRUE,Group=t5Group,NAME=CSV,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=PINK,X=LON,Y=LAT,Z=ALT,LABEL=NAME,TIMELF=TIME,FILTERDATAFIELDS=ALL"
    set qualsEllipseCSV {HEADERROWS=L:1,COLUMNNAMES="^ellipseColumnNames",LAYER={^ellipseLayerCSV},TAG=CSVFILE}
    set layerSNAKE1 "LT=T5,TimeOrder=False,Group=t5Group2,NAME=SNAKE1,ColorFade=TRUE,ColorFadeFactor=1,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=BLUE,SELECTMESSAGES=TRUE,METADATAFIELDS=NAME|NOTE,FILTERDATAFIELDS=ALL,FIELD=^symbolsField"
    set layerSNAKE2 "LT=T5,TimeOrder=False,Group=t5Group2,NAME=SNAKE2,ColorFade=TRUE,ColorFadeFactor=1,FadeColor=0x200000,AutoselectEnable=CLICK,AutoSelectMode=PIXEL,SELECTCOLOR=BLUE,SELECTMESSAGES=TRUE,METADATAFIELDS=NAME|NOTE,FILTERDATAFIELDS=ALL,FIELD=^symbolsField"

    ! Set the lists of choices
    set keyCSVChoices    "POS.LAT,POS.LON,NAME"
    set keySnakeChoices  "POS.LAT,POS.LON,NAME,NOTE"
    set predCSVChoices   "EQ,NE,GT,LT,GE,LE,EQS,SEQS,STARTS,ENDS,SUBS,CONTAINS/S"
    set predSnakeChoices "ANY,EQ,NE,GT,LT,GE,LE,EQS,SEQS,STARTS,ENDS,SUBS,CONTAINS/S"

    ! Set the test instructions
    set instructions "Applying a highlight filter will trigger only points passing the filter to be highlighted.\n\n&
                      1) Click 'Apply Highlight Filter'.\n   &
                         a) The top plot should have:\n      &
                            - The HI and JEREMY points white\n      &
                            - The JEFF, JAY, and JENNIFER points pink\n      &
                            - The ANDREW point still red\n   &
                         b) The bottom plot should have:\n      &
                            - The yellow snake be orange for points to the left of -90 degrees LON, and then pink for all other points.\n      &
                            - The magenta snake be orange for points to the left of 90 degrees LON, and then pink for all other points.\n\n&
                      2) Adjust the Filters and try applying them."

    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 16 /title

      nxm.sys.test.t5pathgenerator/TRAJECTORY/NAME="SNAKE1"/LAT_START=-15/LON_START=-150/LAT_DEST=60/LON_DEST=-15/LON_SHIFT=0.1/LAT_SHIFT=0.02/DIRECTCOLOR=^snake1color/NOFLAGS/RT/NOTEMODETYPE=QA/RATE=^snakeRate OUT=_snake1{SIZE=10000} ELEM=Inf /bg
      nxm.sys.test.t5pathgenerator/FLIGHT/NAME="SNAKE2"/LAT_START=45/LON_START=15/LAT_DEST=-45/LON_DEST=165/LON_SHIFT=0/LAT_SHIFT=0/DIRECTCOLOR=^snake2color/NOFLAGS/RT/NOTEMODETYPE=VA/RATE=^snakeRate OUT=_snake2{SIZE=10000} ELEM=Inf /bg

      plot/id=timepts ^{datDir}.world.prm|{LT="Drawable",name=t5Group,listlayers=true}|nxm.sys.test.ellipseflagstime5000.csv^{qualsEllipseCSV} &
                    readoutExt=+SHOW|-FEATURES|+POINTS readout=+OnAxis

      plot/id=timepipe ^{datDir}.world.prm|{LT="Drawable",name=t5Group2,listlayers=true}|_snake1{LAYER={^layerSnake1},TAG=SNAKE1}|&
                              _snake2{LAYER={^layerSnake2},TAG=SNAKE2} readoutExt=+SHOW|-FEATURES|+POINTS readout=+OnAxis view=latloncontinuous

      gcontrol label macro "Global Controls"
      gcontrol/nc=1 button HIGHLIGHTFILTER "" "Apply Highlight Filter"
      gcontrol/nc=1 button NULLFILTER      "" "Null Highlight Filter"
      gcontrol label macro "Top CSV Plot"
       gcontrol/nomsg choice keyCSVFirstHS        "Column/Key First HS"      "^{keyCSVChoices}"  "^{defaults.csv.firstHS.key}"
       gcontrol       choice predicateCSVFirstHS  "Predicate First HS"       "^{predCSVChoices}" "^{defaults.csv.firstHS.pred}"
       gcontrol/nomsg prompt valueCSVFirstHS      "DVAL or String First HS"                      "^{defaults.csv.firstHS.val}"
       gcontrol/nomsg prompt colorCSVFirstHS      "Color First HS"                               "^{defaults.csv.firstHS.color}"
       gcontrol/nomsg choice keyCSVSecondHS       "Column/Key Second HS"     "^{keyCSVChoices}"  "^{defaults.csv.secondHS.key}"
       gcontrol       choice predicateCSVSecondHS "Predicate Second HS"      "^{predCSVChoices}" "^{defaults.csv.secondHS.pred}"
       gcontrol/nomsg prompt valueCSVSecondHS     "DVAL or String Second HS"                     "^{defaults.csv.secondHS.val}"
       gcontrol/nomsg prompt colorCSVSecondHS     "Color Second HS"                              "^{defaults.csv.secondHS.color}"
      gcontrol label macro "Bottom Plot ^{snake1}"
       gcontrol/nomsg choice keySnake1FirstHS        "Column/Key First HS"      "^{keySnakeChoices}"  "^{defaults.snake1.firstHS.key}"
       gcontrol       choice predicateSnake1FirstHS  "Predicate First HS"       "^{predSnakeChoices}" "^{defaults.snake1.firstHS.pred}"
       gcontrol/nomsg prompt valueSnake1FirstHS      "DVAL or String First HS"                        "^{defaults.snake1.firstHS.val}"
       gcontrol/nomsg prompt colorSnake1FirstHS      "Color First HS"                                 "^{defaults.snake1.firstHS.color}"
       gcontrol/nomsg choice keySnake1SecondHS       "Column/Key Second HS"     "^{keySnakeChoices}"  "^{defaults.snake1.secondHS.key}"
       gcontrol       choice predicateSnake1SecondHS "Predicate Second HS"      "^{predSnakeChoices}" "^{defaults.snake1.secondHS.pred}"
       gcontrol/nomsg prompt valueSnake1SecondHS     "DVAL or String Second HS"                       "^{defaults.snake1.secondHS.val}"
       gcontrol/nomsg prompt colorSnake1SecondHS     "Color Second HS"                                "^{defaults.snake1.secondHS.color}"
      gcontrol label macro "Bottom Plot ^{snake2}"
       gcontrol/nomsg choice keySnake2FirstHS        "Column/Key First HS"      "^{keySnakeChoices}"  "^{defaults.snake2.firstHS.key}"
       gcontrol       choice predicateSnake2FirstHS  "Predicate First HS"       "^{predSnakeChoices}" "^{defaults.snake2.firstHS.pred}"
       gcontrol/nomsg prompt valueSnake2FirstHS      "DVAL or String First HS"                        "^{defaults.snake2.firstHS.val}"
       gcontrol/nomsg prompt colorSnake2FirstHS      "Color First HS"                                 "^{defaults.snake2.firstHS.color}"
       gcontrol/nomsg choice keySnake2SecondHS       "Column/Key Second HS"     "^{keySnakeChoices}"  "^{defaults.snake2.secondHS.key}"
       gcontrol       choice predicateSnake2SecondHS "Predicate Second HS"      "^{predSnakeChoices}" "^{defaults.snake2.secondHS.pred}"
       gcontrol/nomsg prompt valueSnake2SecondHS     "DVAL or String Second HS"                       "^{defaults.snake2.secondHS.val}"
       gcontrol/nomsg prompt colorSnake2SecondHS     "Color Second HS"                                "^{defaults.snake2.secondHS.color}"

      message send timepts NAME=ZOOM INFO=0 DATA={XMIN=25,XMAX=125,YMIN=-20,YMAX=80}

      invoke ptsCSV    reg.timepts.layers.t5group.getLayer(CSV)
      invoke ptsSnake1 reg.timepipe.layers.t5group2.getLayer(SNAKE1)
      invoke ptsSnake2 reg.timepipe.layers.t5group2.getLayer(SNAKE2)
    pipe off

  endif
  erase test_blue_file
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 starts "predicate" then
    invoke valueField msg.name.replace("PREDICATE","VALUE")
    if msg.data eqs "ANY" then
      invoke gc.^{valueField}.panel.setVisible(false)
    else
      invoke gc.^{valueField}.panel.setVisible(true)
    endif
  elseif msg.name eqss "HIGHLIGHTFILTER" then
    call createHighlightFilters
    ! CSV
    invoke ,, ptsCSV.putHighlightSetting("csvFirstHS",csvFirstHSMapPT,"^{gc.colorCSVFirstHS.value}")
    invoke ,, ptsCSV.putHighlightSetting("csvSecondHS",csvSecondHSMapPT,"^{gc.colorCSVSecondHS.value}")
    ! Snake 1
    invoke ,, ptsSnake1.putHighlightSetting("snake1FirstHS",snake1FirstHSMapPT,"^{gc.colorSnake1FirstHS.value}")
    invoke ,, ptsSnake1.putHighlightSetting("snake1SecondHS",snake1SecondHSMapPT,"^{gc.colorSnake1SecondHS.value}")
    ! Snake 2
    invoke ,, ptsSnake2.putHighlightSetting("snake2FirstHS",snake2FirstHSMapPT,"^{gc.colorSnake2FirstHS.value}")
    invoke ,, ptsSnake2.putHighlightSetting("snake2SecondHS",snake2SecondHSMapPT,"^{gc.colorSnake2SecondHS.value}")
  elseif msg.name eqss "NULLFILTER" then
    invoke ,, ptsCSV.clearHighlightSettings()
    invoke ,, ptsSnake1.clearHighlightSettings()
    invoke ,, ptsSnake2.clearHighlightSettings()
  else
    say "Unhandled message name: ^msg.name from fid: ^msg.fid data: ^msg.data"
  endif
return

!******************************************************************************!
! If an unexpected exception is caught, mark test as failed, unless it is expected
!******************************************************************************!
procedure processException m:emsg
res msg.name
  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

!*****************************************************************************
! create highlight filters for this test
!*****************************************************************************
procedure createHighlightFilters z:useDefaults[false]
  invoke csvFirstHSMapPT     nxm.sys.lib.PredicateType.getNewPredicateTypeMap()
  invoke csvSecondHSMapPT    nxm.sys.lib.PredicateType.getNewPredicateTypeMap()
  invoke snake1FirstHSMapPT  nxm.sys.lib.PredicateType.getNewPredicateTypeMap()
  invoke snake1SecondHSMapPT nxm.sys.lib.PredicateType.getNewPredicateTypeMap()
  invoke snake2FirstHSMapPT  nxm.sys.lib.PredicateType.getNewPredicateTypeMap()
  invoke snake2SecondHSMapPT nxm.sys.lib.PredicateType.getNewPredicateTypeMap()

  if useDefaults isFalse then
    invoke ,, nxm.sys.lib.PredicateType.addKeyFunction(csvFirstHSMapPT,^{gc.keyCSVFirstHS.value},"^{gc.predicateCSVFirstHS.value} ^{gc.valueCSVFirstHS.value}")
    invoke ,, nxm.sys.lib.PredicateType.addKeyFunction(csvSecondHSMapPT,^{gc.keyCSVSecondHS.value},"^{gc.predicateCSVSecondHS.value} ^{gc.valueCSVSecondHS.value}")
    invoke ,, nxm.sys.lib.PredicateType.addKeyFunction(snake1FirstHSMapPT,^{gc.keySnake1FirstHS.value},"^{gc.predicateSnake1FirstHS.value} ^{gc.valueSnake1FirstHS.value}")
    invoke ,, nxm.sys.lib.PredicateType.addKeyFunction(snake1SecondHSMapPT,^{gc.keySnake1SecondHS.value},"^{gc.predicateSnake1SecondHS.value} ^{gc.valueSnake1SecondHS.value}")
    invoke ,, nxm.sys.lib.PredicateType.addKeyFunction(snake2FirstHSMapPT,^{gc.keysnake2FirstHS.value},"^{gc.predicatesnake2FirstHS.value} ^{gc.valuesnake2FirstHS.value}")
    invoke ,, nxm.sys.lib.PredicateType.addKeyFunction(snake2SecondHSMapPT,^{gc.keysnake2SecondHS.value},"^{gc.predicatesnake2SecondHS.value} ^{gc.valuesnake2SecondHS.value}")
  else
    invoke ,, nxm.sys.lib.PredicateType.addKeyFunction(csvFirstHSMapPT,^{defaults.csv.firstHS.key},"^{defaults.csv.firstHS.pred} ^{defaults.csv.firstHS.val}")
    invoke ,, nxm.sys.lib.PredicateType.addKeyFunction(csvSecondHSMapPT,^{defaults.csv.secondHS.key},"^{defaults.csv.secondHS.pred} ^{defaults.csv.secondHS.val}")
    invoke ,, nxm.sys.lib.PredicateType.addKeyFunction(snake1FirstHSMapPT,^{defaults.snake1.firstHS.key},"^{defaults.snake1.firstHS.pred} ^{defaults.snake1.firstHS.val}")
    invoke ,, nxm.sys.lib.PredicateType.addKeyFunction(snake1SecondHSMapPT,^{defaults.snake1.secondHS.key},"^{defaults.snake1.secondHS.pred} ^{defaults.snake1.secondHS.val}")
    invoke ,, nxm.sys.lib.PredicateType.addKeyFunction(snake2FirstHSMapPT,^{defaults.snake2.firstHS.key},"^{defaults.snake2.firstHS.pred} ^{defaults.snake2.firstHS.val}")
    invoke ,, nxm.sys.lib.PredicateType.addKeyFunction(snake2SecondHSMapPT,^{defaults.snake2.secondHS.key},"^{defaults.snake2.secondHS.pred} ^{defaults.snake2.secondHS.val}")
  endif
return