package pl.projectspace.idea.plugins.php.atoum.actions;

import com.intellij.openapi.project.Project;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.PhpFile;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;

public class Utils {

    public static Boolean isClassAtoumTest(PhpClass checkedClass)
    {
        return checkedClass.getNamespaceName().contains(getTestsNamespaceSuffix());
    }

    @Nullable
    public static PhpClass locateTestClass(Project project, PhpClass testedClass) {
        String testClassname = testedClass.getNamespaceName() + getTestsNamespaceSuffix() + testedClass.getName();
        return locatePhpClass(project, testClassname);
    }

    @Nullable
    public static PhpClass locateTestedClass(Project project, PhpClass testClass) {
        String testClassNamespaceName = testClass.getNamespaceName();
        String testedClassname = testClassNamespaceName.replace(getTestsNamespaceSuffix(), "") + testClass.getName();
        return locatePhpClass(project, testedClassname);

    }

    @Nullable
    protected static PhpClass locatePhpClass(Project project, String name) {
        Collection<PhpClass> phpClasses = PhpIndex.getInstance(project).getAnyByFQN(name);
        if (phpClasses.size() != 1) {
            return null;
        }

        return (PhpClass)phpClasses.toArray()[0];
    }

    private static String getTestsNamespaceSuffix()
    {
        return "tests\\units\\";
    }

    //https://github.com/Haehnchen/idea-php-symfony2-plugin/blob/cb422db9779025d65fdf0ba5d26a38d401eca939/src/fr/adrienbrault/idea/symfony2plugin/util/PhpElementsUtil.java#L784
    @Nullable
    public static PhpClass getFirstClassFromFile(PhpFile phpFile) {
        Collection<PhpClass> phpClasses = PsiTreeUtil.collectElementsOfType(phpFile, PhpClass.class);
        return phpClasses.size() == 0 ? null : phpClasses.iterator().next();
    }
}

@@ -50,16 +50,56 @@
 tions;%0A%0A
+import com.google.common.collect.Lists;%0A
 import c
@@ -135,16 +135,16 @@
 roject;%0A
-
 import c
@@ -308,16 +308,60 @@
 pClass;%0A
+import org.apache.commons.lang.StringUtils;%0A
 import o
@@ -424,16 +424,39 @@
 lection;
+%0Aimport java.util.List;
 %0A%0Apublic
@@ -750,83 +750,373 @@
 ring
+%5B%5D namespaceParts =
  test
+ed
 Class
-name = testedClass.getNamespaceName() + getTestsNamespaceSuffix()
+.getNamespaceName().split(%22%5C%5C%5C%5C%22);%0A%0A        for(int i=namespaceParts.length; i%3E=1; i--)%7B%0A            List%3CString%3E foo = Lists.newArrayList(namespaceParts);%0A            foo.add(i, getTestsNamespaceSuffix().substring(0, getTestsNamespaceSuffix().length() - 1));%0A%0A            String possibleClassname = StringUtils.join(foo, %22%5C%5C%22) + %22%5C%5C%22
  + t
@@ -1137,38 +1137,111 @@
 Name();%0A        
+ 
-return
+   System.out.println(possibleClassname);%0A            PhpClass possibleClass =
  locatePhpClass(
@@ -1249,20 +1249,24 @@
 roject, 
-test
+possible
 Classnam
@@ -1265,24 +1265,149 @@
 Classname);%0A
+            if (null != possibleClass) %7B%0A                return possibleClass;%0A            %7D%0A        %7D%0A%0A        return null;%0A
     %7D%0A%0A    @

fix navigation to the test class if the suffix is not at the end of the namespace