package de.espend.idea.shopware.util;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.lang.psi.elements.Method;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;

public class HookSubscriberUtil {

    public static void collectHooks(Project project, HookVisitor hookVisitor) {

        Collection<PhpClass> phpClasses = new ArrayList<PhpClass>();
        VirtualFile virtualFile = VfsUtil.findRelativeFile(project.getBaseDir(), "engine", "core", "class");
        if(virtualFile != null) {
            for(VirtualFile coreClassFile: VfsUtil.getChildren(virtualFile)) {
                String name = coreClassFile.getName();
                if(name.contains(".")) name = name.substring(0, name.lastIndexOf('.'));

                phpClasses.addAll(PhpIndex.getInstance(project).getClassesByName(name));
            }
        }

        phpClasses.addAll(PhpIndex.getInstance(project).getDirectSubclasses("\\Enlight_Hook"));

        for(PhpClass phpClass: phpClasses) {
            for(Method method: phpClass.getMethods()) {
                if(!method.isStatic() && !method.isAbstract() && !method.getName().startsWith("_")) {
                    boolean returnValue = hookVisitor.visitHook(phpClass, method);
                    if(!returnValue) {
                        return;
                    }
                }
            }
        }

    }

    public static void collectDoctrineLifecycleHooks(Project project, DoctrineLifecycleHooksVisitor hookVisitor) {
        for(PhpClass phpClass: PhpIndex.getInstance(project).getAllSubclasses("\\Shopware\\Components\\Model\\ModelEntity")) {
            hookVisitor.visitLifecycleHooks(phpClass);
        }
    }

    public static interface DoctrineLifecycleHooksVisitor {
        public boolean visitLifecycleHooks(PhpClass phpClass);
    }

    public static interface HookVisitor {
        public boolean visitHook(PhpClass phpClass, Method method);
    }

}

@@ -421,233 +421,753 @@
 til.
-ArrayList;%0Aimport java.util.Collection;%0A%0Apublic class HookSubscriberUtil %7B%0A%0A    public static void collectHooks(Project project, HookVisitor hookVisitor) %7B%0A%0A        Collection%3CPhpClass%3E phpClasses = new ArrayList%3CPhpClass%3E();
+*;%0A%0Apublic class HookSubscriberUtil %7B%0A%0A    final private static Set%3CString%3E CORE_CLASSES = new HashSet%3CString%3E() %7B%7B%0A        addAll(Arrays.asList(%22sCms%22, %22sCore%22, %22sAdmin%22, %22sOrder%22, %22sBasket%22, %22sExport%22, %22sSystem%22, %22sArticles%22, %22sMarketing%22, %22sCategories%22, %22sCategories%22, %22sNewsletter%22, %22sConfigurator%22, %22sRewriteTable%22));%0A    %7D%7D;%0A%0A    public static void collectHooks(Project project, HookVisitor hookVisitor) %7B%0A%0A        Collection%3CPhpClass%3E phpClasses = new ArrayList%3CPhpClass%3E();%0A%0A        // directly use core classes%0A        PhpIndex phpIndex = PhpIndex.getInstance(project);%0A        for(String coreClass: CORE_CLASSES) %7B%0A            phpClasses.addAll(phpIndex.getClassesByName(coreClass));%0A        %7D%0A%0A        // fallback: search on directory
 %0A   
@@ -1524,25 +1524,79 @@
 exOf('.'));%0A
-%0A
+                if(!CORE_CLASSES.contains(name)) %7B%0A    
             
@@ -1613,33 +1613,33 @@
 sses.addAll(
-P
+p
 hpIndex.
 getInstance(
@@ -1626,37 +1626,16 @@
 hpIndex.
-getInstance(project).
 getClass
@@ -1647,24 +1647,42 @@
 ame(name));%0A
+                %7D%0A
             
@@ -1720,25 +1720,25 @@
 .addAll(
-P
+p
 hpIndex.
 getInsta
@@ -1729,37 +1729,16 @@
 hpIndex.
-getInstance(project).
 getDirec

add known hook class on core and provide directory based as fallback to support external library workflow