package com.karateca.ddescriber.model;

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

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

  private static final String REMOVE_START_OF_LINE = "\\s*([xd]?describe\\(|[xi]?it\\()[\"\'](\\S+)";
  private static final String REMOVE_END_OF_LINE = "(\\S+)([\"\'])(\\s*[,+]\\s*.*$)";
  private final int indentation;
  private final boolean isDescribe;
  private final int endOffset;
  private final int startOffset;
  private final int lineNumber;
  private String testText;
  private final TestState testState;

  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));
    isDescribe = lineText.contains("describe(");

    if (lineText.contains("ddescribe") || lineText.contains("iit")) {
      testState = TestState.Included;
    } else if (lineText.contains("xdescribe") || lineText.contains("xit")) {
      testState = TestState.Excluded;
    } else {
      testState = TestState.NotModified;
    }

    // Leave the Test text.
    // TODO: improve this regular expression.
    testText = lineText.replaceAll(REMOVE_END_OF_LINE, "$1");
    testText = testText.replaceAll(REMOVE_START_OF_LINE, "$2");

    indentation = startOffset - startOfLine;
  }

  public int getIndentation() {
    return indentation;
  }

  public boolean isDescribe() {
    return isDescribe;
  }

  public int getEndOffset() {
    return endOffset;
  }

  public int getStartOffset() {
    return startOffset;
  }

  public int getLineNumber() {
    return lineNumber;
  }

  public String getTestText() {
    return testText;
  }

  public TestState getTestState() {
    return testState;
  }

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

@@ -618,22 +618,16 @@
 private 
-final 
 TestStat
@@ -628,24 +628,64 @@
 stState test
+State;%0A  private TestState pendingChange
 State;%0A%0A  pu
@@ -2131,32 +2131,119 @@
 testState;%0A  %7D%0A%0A
+  public void setTestState(TestState testState) %7B%0A    this.testState = testState;%0A  %7D%0A%0A
   public String 
@@ -2255,18 +2255,16 @@
 ing() %7B%0A
-//
     retu
@@ -2270,95 +2270,227 @@
 urn 
-String.format(%22%25s (line: %25d)%22, testText.trim(), lineNumber);%0A    return testText.trim()
+testText.trim();%0A  %7D%0A%0A  public TestState getPendingChangeState() %7B%0A    return pendingChangeState;%0A  %7D%0A%0A  public void setPendingChangeState(TestState pendingChangeState) %7B%0A    this.pendingChangeState = pendingChangeState
 ;%0A  

add pending state