package de.simonscholz.junit4converter.converters;

import static de.simonscholz.junit4converter.converters.JTestCaseConverter.DBRULE_VARIABLENAME;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ASTVisitor;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Statement;
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;

public class RedirectCallsOfSuperClassToRuleMethodBodyVisitor extends
		ASTVisitor {
	private ASTRewrite rewriter;
	private List<String> toBeReplacedMethodCalls;

	public RedirectCallsOfSuperClassToRuleMethodBodyVisitor(ASTRewrite rewriter) {
		this.rewriter = rewriter;
		createListOfToBeReplacedMethods();
	}

	private void createListOfToBeReplacedMethods() {
		toBeReplacedMethodCalls = new ArrayList<>();
		toBeReplacedMethodCalls.add("getSession()");
		toBeReplacedMethodCalls.add("addImplementation(");
		toBeReplacedMethodCalls.add("createMockView(");
		toBeReplacedMethodCalls.add("addImplementation(");
		toBeReplacedMethodCalls.add("addDatatypeProvider(");
	}

	@Override
	public boolean visit(Block methodBody) {
		for (Statement currentStatement : getStatements(methodBody)) {
			String statement = currentStatement.toString();
			for (String toBeReplacedMethodCall : toBeReplacedMethodCalls) {
				if (statement.contains(toBeReplacedMethodCall)) {
					int startOfReplacement = statement
							.indexOf(toBeReplacedMethodCall);
					int previousIndex = startOfReplacement - 1;
					if (previousIndex >= 0) {
						char previousChar = statement.charAt(previousIndex);
						if (previousChar == '.') {
							continue;
						}
					}
					String newStatement = createReplacingStatement(statement,
							toBeReplacedMethodCall);
					ASTNode delegateToRule = rewriter.createStringPlaceholder(
							newStatement, ASTNode.EMPTY_STATEMENT);
					rewriter.replace(currentStatement, delegateToRule, null);
				}
			}
		}
		return true;
	}

	private String createReplacingStatement(String currentStatement,
			String toReplacingMethodCall) {
		String newMethodCall = DBRULE_VARIABLENAME + '.'
				+ toReplacingMethodCall;
		String newStatement = currentStatement.replace(toReplacingMethodCall,
				newMethodCall);
		return newStatement;
	}

	@SuppressWarnings("unchecked")
	private List<Statement> getStatements(Block methodBody) {
		return methodBody.statements();
	}

}

@@ -1385,256 +1385,69 @@
 %09%09%09i
-nt startOfReplacement = statement%0A%09%09%09%09%09%09%09.indexOf(toBeReplacedMethodCall);%0A%09%09%09%09%09int previousIndex = startOfReplacement - 1;%0A%09%09%09%09%09if (previousIndex %3E= 0) %7B%0A%09%09%09%09%09%09char previousChar = statement.charAt(previousIndex);%0A%09%09%09%09%09%09if (previousChar == '.') %7B%0A%09
+f (shouldNotBeReplaced(statement, toBeReplacedMethodCall)) %7B%0A
 %09%09%09%09
@@ -1462,16 +1462,8 @@
 ue;%0A
-%09%09%09%09%09%09%7D%0A
 %09%09%09%09
@@ -1749,16 +1749,16 @@
 %09%09%7D%0A%09%09%7D%0A
-
 %09%09return
@@ -1764,24 +1764,370 @@
 n true;%0A%09%7D%0A%0A
+%09private boolean shouldNotBeReplaced(String statement,%0A%09%09%09String toBeReplacedMethodCall) %7B%0A%09%09int startOfReplacement = statement.indexOf(toBeReplacedMethodCall);%0A%09%09int previousIndex = startOfReplacement - 1;%0A%09%09if (previousIndex %3E= 0) %7B%0A%09%09%09char previousChar = statement.charAt(previousIndex);%0A%09%09%09return previousChar == '.';%0A%09%09%7D%0A%09%09return false;%0A%09%7D%0A%0A
 %09private Str

extract logic into method