package com.cedricziel.idea.typo3.container;

import com.cedricziel.idea.typo3.domain.TYPO3RouteDefinition;
import com.cedricziel.idea.typo3.psi.visitor.RouteDefinitionParserVisitor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.intellij.psi.search.FileTypeIndex;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.indexing.FileBasedIndex;
import com.jetbrains.php.lang.PhpFileType;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * Traverses the known places for route definitions and collects them.
 */
public class RouteProvider {
    private HashMap<String, List<TYPO3RouteDefinition>> routes;
    private HashMap<String, List<TYPO3RouteDefinition>> ajaxRoutes;

    public RouteProvider() {
        routes = new HashMap<>();
        ajaxRoutes = new HashMap<>();
    }

    private void collectRoutes(Project project) {
        FileBasedIndex index = FileBasedIndex.getInstance();
        Collection<VirtualFile> containingFiles = index.getContainingFiles(
                FileTypeIndex.NAME,
                PhpFileType.INSTANCE,
                GlobalSearchScope.allScope(project)
        );
        containingFiles.removeIf(virtualFile -> {
            return !(virtualFile.getName().contains("Routes.php") || virtualFile.getName().contains("AjaxRoutes.php"));
        });

        for (VirtualFile projectFile : containingFiles) {
            PsiFile psiFile = PsiManager.getInstance(project).findFile(projectFile);
            if (psiFile != null) {
                psiFile.accept(new RouteDefinitionParserVisitor(routes, ajaxRoutes));
            }
        }
    }

    public void collect(Project project) {
        collectRoutes(project);
    }

    public Boolean has(Project project, String routeName) {
        return null;
    }

    public List<TYPO3RouteDefinition> resolve(String routeName) {
        return null;
    }

    public List<TYPO3RouteDefinition> all() {
        List<TYPO3RouteDefinition> list = new ArrayList<>();
        ajaxRoutes.values().forEach(ajaxRouteDefs -> ajaxRouteDefs.forEach(list::add));
        routes.values().forEach(routeDefs -> routeDefs.forEach(list::add));

        return list;
    }
}

@@ -2315,24 +2315,249 @@
 st::add));%0A%0A
+        return list;%0A    %7D%0A%0A    public List%3CTYPO3RouteDefinition%3E ajax() %7B%0A        List%3CTYPO3RouteDefinition%3E list = new ArrayList%3C%3E();%0A        ajaxRoutes.values().forEach(ajaxRouteDefs -%3E ajaxRouteDefs.forEach(list::add));%0A%0A
         retu

Add accessor for ajax routes