package com.karateca;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.impl.DocumentImpl;
import com.intellij.openapi.editor.impl.EditorImpl;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.popup.AbstractPopup;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.Font;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author Andres Dominguez.
 */
public class DDescirberAction extends AnAction {

  protected Project project;
  protected EditorImpl editor;
  protected AbstractPopup popup;
  protected VirtualFile virtualFile;
  protected DocumentImpl document;

  private Font font;
  private EditorColorsScheme scheme;

  private AceFinder aceFinder;

  private HashMap<String, Integer> textAndOffsetHash = new HashMap<String, Integer>();


  @Override
  public void update(AnActionEvent e) {
    e.getPresentation().setEnabled(e.getData(PlatformDataKeys.EDITOR) != null);
  }

  public void actionPerformed(AnActionEvent actionEvent) {
    project = actionEvent.getData(PlatformDataKeys.PROJECT);
    editor = (EditorImpl) actionEvent.getData(PlatformDataKeys.EDITOR);
    virtualFile = actionEvent.getData(PlatformDataKeys.VIRTUAL_FILE);
    document = (DocumentImpl) editor.getDocument();

    scheme = EditorColorsManager.getInstance().getGlobalScheme();
    font = new Font(scheme.getEditorFontName(), Font.BOLD, scheme.getEditorFontSize());

    aceFinder = new AceFinder(project, document, editor, virtualFile);

    aceFinder.addResultsReadyListener(new ChangeListener() {
      @Override
      public void stateChanged(ChangeEvent changeEvent) {
        if (changeEvent.getSource().equals("LinesFound")) {
          showResultsFound();

        }
      }
    });
    aceFinder.findText("it\\(|describe\\(", true);
  }

  private void showResultsFound() {
    // Filter the hierarchy.
    int currentIndentation = Integer.MAX_VALUE;
    List<LineFindResult> hierarchy = new ArrayList<LineFindResult>();

    // Find all the parents from the current scope.
    for (LineFindResult line : aceFinder.getLineFindResults()) {
      if (line.getIndentation() < currentIndentation) {
        currentIndentation = line.getIndentation();
        hierarchy.add(line);
      }
    }

    System.out.println("");
  }
}

@@ -186,133 +186,8 @@
 ys;%0A
-import com.intellij.openapi.editor.colors.EditorColorsManager;%0Aimport com.intellij.openapi.editor.colors.EditorColorsScheme;%0A
 impo
@@ -381,52 +381,8 @@
 ile;
-%0Aimport com.intellij.ui.popup.AbstractPopup;
 %0A%0Aim
@@ -462,30 +462,8 @@
 er;%0A
-import java.awt.Font;%0A
 impo
@@ -490,34 +490,8 @@
 st;%0A
-import java.util.HashMap;%0A
 impo
@@ -661,41 +661,8 @@
 or;%0A
-  protected AbstractPopup popup;%0A
   pr
@@ -744,174 +744,27 @@
 ate 
-Font font;%0A  private EditorColorsScheme scheme;%0A%0A  private AceFinder aceFinder;%0A%0A  private HashMap%3CString, Integer%3E textAndOffsetHash = new HashMap%3CString, Integer%3E()
+AceFinder aceFinder
 ;%0A%0A%0A
@@ -1219,163 +1219,8 @@
 );%0A%0A
-    scheme = EditorColorsManager.getInstance().getGlobalScheme();%0A    font = new Font(scheme.getEditorFontName(), Font.BOLD, scheme.getEditorFontSize());%0A%0A
     

remove unused