package com.karateca.ddescriber;

import com.intellij.find.FindResult;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.util.TextRange;

/**
 * @author Andres Dominguez.
 */
public class TestFindResult {

  public static final String REMOVE_END_OF_LINE = "([\"\'])(\\s*[,+]\\s*.*$)";
  private final int indentation;
  private final boolean isDescribe;
  private final boolean markedForRun;
  private final int endOffset;
  private final int startOffset;
  private final int lineNumber;
  private final String testText;
  private boolean excluded;

  public TestFindResult(Document document, FindResult findResult) {
    startOffset = findResult.getStartOffset();
    endOffset = findResult.getEndOffset();

    int lineNumber = document.getLineNumber(endOffset);
    int startOfLine = document.getLineStartOffset(lineNumber);
    int endOfLine = document.getLineEndOffset(lineNumber);

    this.lineNumber = lineNumber + 1;
    String lineText = document.getText(new TextRange(startOfLine, endOfLine));
    testText = lineText.replaceAll(REMOVE_END_OF_LINE, "$1");

    indentation = startOffset - startOfLine;

    isDescribe = lineText.contains("describe(");
    markedForRun = lineText.contains("ddescribe(") || lineText.contains("iit(");
  }

  public int getIndentation() {
    return indentation;
  }

  public boolean isDescribe() {
    return isDescribe;
  }

  public boolean isMarkedForRun() {
    return markedForRun;
  }

  public int getEndOffset() {
    return endOffset;
  }

  public int getStartOffset() {
    return startOffset;
  }

  public int getLineNumber() {
    return lineNumber;
  }

  public String getTestText() {
    return testText;
  }

  public boolean isExcluded() {
    return excluded;
  }

  public void setExcluded(boolean excluded) {
    this.excluded = excluded;
  }

  @Override
  public String toString() {
    return String.format("line: %5d: %s", lineNumber, testText);
  }
}

@@ -1897,22 +1897,39 @@
 at(%22
+%25s (
 line: %25
-5d: %25s%22
+d)%22, testText.trim()
 , li
@@ -1936,26 +1936,16 @@
 neNumber
-, testText
 );%0A  %7D%0A%7D

move line number last