package com.karateca.ddescriber;

import com.intellij.find.FindManager;
import com.intellij.find.FindModel;
import com.intellij.find.FindResult;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.util.EventDispatcher;

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

/**
 * Used to find suites and unit tests in a JsUnit JavaScript file.
 */
public class JasmineFinder {

  private static final String FIND_REGEXP = "iit\\(|ddescribe\\(|it\\(|describe\\(";
  private final Project project;
  private final Document document;
  public List<FindResult> findResults;

  private final EventDispatcher<ChangeListener> myEventDispatcher = EventDispatcher.create(ChangeListener.class);

  public JasmineFinder(Project project, Document document) {
    this.project = project;
    this.document = document;
  }

  FindModel createFindModel(FindManager findManager) {
    FindModel clone = (FindModel) findManager.getFindInFileModel().clone();
    clone.setFindAll(true);
    clone.setFromCursor(true);
    clone.setForward(true);
    clone.setWholeWordsOnly(false);
    clone.setCaseSensitive(true);
    clone.setRegularExpressions(true);
    clone.setWholeWordsOnly(true);
    clone.setStringToFind(FIND_REGEXP);

    return clone;
  }

  public void findText() {
    ApplicationManager.getApplication().runReadAction(new Runnable() {
      @Override
      public void run() {
        findAll();
        if (findResults.size() > 0) {
          myEventDispatcher.getMulticaster().stateChanged(new ChangeEvent("LinesFound"));
        }
      }
    });
  }

  public void findAll() {
    FindManager findManager = FindManager.getInstance(project);
    FindModel findModel = createFindModel(findManager);


    findResults = new ArrayList<FindResult>();

    CharSequence text = document.getCharsSequence();
    int offset = 0;

    while (true) {
      FindResult result = findManager.findString(text, offset, findModel);

      if (!result.isStringFound()) {
        return;
      }

      offset = result.getEndOffset();

      findResults.add(result);
    }
  }

  /**
   * Register for change events.
   *
   * @param changeListener The listener to be added.
   */
  public void addResultsReadyListener(ChangeListener changeListener) {
    myEventDispatcher.addListener(changeListener);
  }

  public List<FindResult> getFindResults() {
    return findResults;
  }
}

@@ -764,123 +764,8 @@
 s;%0A%0A
-  private final EventDispatcher%3CChangeListener%3E myEventDispatcher = EventDispatcher.create(ChangeListener.class);%0A%0A
   pu
@@ -817,24 +817,24 @@
 document) %7B%0A
+
     this.pro
@@ -1314,326 +1314,8 @@
  %7D%0A%0A
-  public void findText() %7B%0A    ApplicationManager.getApplication().runReadAction(new Runnable() %7B%0A      @Override%0A      public void run() %7B%0A        findAll();%0A        if (findResults.size() %3E 0) %7B%0A          myEventDispatcher.getMulticaster().stateChanged(new ChangeEvent(%22LinesFound%22));%0A        %7D%0A      %7D%0A    %7D);%0A  %7D%0A%0A
   pu
@@ -1457,17 +1457,16 @@
 ager);%0A%0A
-%0A
     find
@@ -1802,24 +1802,24 @@
 dd(result);%0A
+
     %7D%0A  %7D%0A%0A 
@@ -1821,238 +1821,8 @@
  %7D%0A%0A
-  /**%0A   * Register for change events.%0A   *%0A   * @param changeListener The listener to be added.%0A   */%0A  public void addResultsReadyListener(ChangeListener changeListener) %7B%0A    myEventDispatcher.addListener(changeListener);%0A  %7D%0A%0A
   pu

remove unused