/*
 * Copyright 2000-2017 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.debugger.streams.ui.impl;

import com.intellij.debugger.streams.ui.ElementChooser;
import com.intellij.debugger.streams.ui.ChooserOption;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.markup.HighlighterLayer;
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.ui.popup.JBPopupAdapter;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.LightweightWindowEvent;
import com.intellij.ui.components.JBList;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Vitaliy.Bibaev
 */
public class ElementChooserImpl<T extends ChooserOption> implements ElementChooser<T> {
  private static final int HIGHLIGHT_LAYER = HighlighterLayer.SELECTION - 1;
  private final Set<RangeHighlighter> myRangeHighlighters = new HashSet<>();
  private final Editor myEditor;
  private final TextAttributes myAttributes =
    EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);

  public ElementChooserImpl(@NotNull Editor editor) {
    myEditor = editor;
  }

  @Override
  public void show(@NotNull List<T> options, @NotNull CallBack<T> callBack) {
    final DefaultListModel<T> model = new DefaultListModel<>();
    for (final T option : options) {
      model.addElement(option);
    }

    final JBList<T> list = new JBList<>(model);
    list.setCellRenderer(new DefaultListCellRenderer() {
      @Override
      public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        //noinspection unchecked
        setText(((T)value).getText());
        return this;
      }
    });

    list.addListSelectionListener(e -> {
      final T selectedValue = list.getSelectedValue();
      if (selectedValue == null) return;
      dropHighlighters();
      selectedValue.rangeStream().forEach(x -> {
        final RangeHighlighter highlighter = myEditor.getMarkupModel()
          .addRangeHighlighter(x.getStartOffset(), x.getEndOffset(), HIGHLIGHT_LAYER, myAttributes, HighlighterTargetArea.EXACT_RANGE);
        myRangeHighlighters.add(highlighter);
      });
    });

    JBPopupFactory.getInstance().createListPopupBuilder(list)
      .setTitle("Multiple chains found")
      .setMovable(true)
      .setResizable(false)
      .setRequestFocus(true)
      .setItemChoosenCallback(() -> callBack.chosen(list.getSelectedValue()))
      .addListener(new JBPopupAdapter() {
        @Override
        public void onClosed(LightweightWindowEvent event) {
          dropHighlighters();
        }
      })
      .createPopup().showInBestPositionFor(myEditor);
  }

  private void dropHighlighters() {
    myRangeHighlighters.forEach(RangeMarker::dispose);
    myRangeHighlighters.clear();
  }
}

@@ -1009,207 +1009,9 @@
 kup.
-HighlighterLayer;%0Aimport com.intellij.openapi.editor.markup.HighlighterTargetArea;%0Aimport com.intellij.openapi.editor.markup.RangeHighlighter;%0Aimport com.intellij.openapi.editor.markup.TextAttributes
+*
 ;%0Aim
@@ -1567,17 +1567,17 @@
 LECTION 
--
++
  1;%0A  pr
@@ -1729,11 +1729,140 @@
 utes
- =%0A
+;%0A%0A  public ElementChooserImpl(@NotNull Editor editor) %7B%0A    myEditor = editor;%0A    final TextAttributes searchResultAttributes =%0A  
     
@@ -1971,84 +1971,302 @@
 S);%0A
-%0A
   
-public ElementChooserImpl(@NotNull Editor editor) %7B%0A    myEditor = editor
+  final Color foreground = editor.getColorsScheme().getDefaultForeground();%0A    myAttributes = new TextAttributes(foreground, searchResultAttributes.getBackgroundColor(), null,%0A                                      searchResultAttributes.getEffectType(), searchResultAttributes.getFontType())
 ;%0A  

Update color of the foreground in the stream chain highlighting