package fr.adrienbrault.idea.symfony2plugin.routing.inspection;

import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiRecursiveElementWalkingVisitor;
import com.intellij.psi.search.GlobalSearchScope;
import com.intellij.util.indexing.FileBasedIndex;
import com.jetbrains.php.lang.PhpFileType;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import fr.adrienbrault.idea.symfony2plugin.Settings;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.routing.PhpRouteReferenceContributor;
import fr.adrienbrault.idea.symfony2plugin.routing.Route;
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.AnnotationRoutesStubIndex;
import fr.adrienbrault.idea.symfony2plugin.stubs.indexes.YamlRoutesStubIndex;
import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.yaml.YAMLFileType;

import java.util.Collection;
import java.util.Map;

abstract public class AbstractPhpRouteInspection extends LocalInspectionTool {

    @NotNull
    @Override
    public PsiElementVisitor buildVisitor(final @NotNull ProblemsHolder holder, boolean isOnTheFly) {

        PsiFile psiFile = holder.getFile();
        if(!Symfony2ProjectComponent.isEnabled(psiFile.getProject()) || !Settings.getInstance(psiFile.getProject()).phpAnnotateRoute) {
            return super.buildVisitor(holder, isOnTheFly);
        }

        psiFile.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
            @Override
            public void visitElement(PsiElement element) {
                if(element instanceof StringLiteralExpression) {
                    annotate((StringLiteralExpression) element, holder);
                }
                super.visitElement(element);
            }
        });


        return super.buildVisitor(holder, isOnTheFly);
    }

    public void annotate(@NotNull final StringLiteralExpression element, @NotNull ProblemsHolder holder) {

        String routeName = element.getContents();
        if(StringUtils.isBlank(routeName)) {
            return;
        }

        MethodMatcher.MethodMatchParameter methodMatchParameter = new MethodMatcher.StringParameterMatcher(element, 0)
            .withSignature(PhpRouteReferenceContributor.GENERATOR_SIGNATURES)
            .match();

        if(methodMatchParameter == null) {
            return;
        }

        annotateRouteName(element, holder, routeName);
    }

    abstract protected void annotateRouteName(PsiElement target, @NotNull ProblemsHolder holder, final String routeName);

}

@@ -118,65 +118,8 @@
 ol;%0A
-import com.intellij.codeInspection.ProblemHighlightType;%0A
 impo
@@ -340,108 +340,8 @@
 or;%0A
-import com.intellij.psi.search.GlobalSearchScope;%0Aimport com.intellij.util.indexing.FileBasedIndex;%0A
 impo
@@ -370,19 +370,19 @@
 ang.
+psi.
 PhpFile
-Type
 ;%0Aim
@@ -451,61 +451,8 @@
 on;%0A
-import fr.adrienbrault.idea.symfony2plugin.Settings;%0A
 impo
@@ -644,505 +644,274 @@
 gin.
-routing.Route;%0Aimport fr.adrienbrault.idea.symfony2plugin.stubs.indexes.AnnotationRoutesStubIndex;%0Aimport fr.adrienbrault.idea.symfony2plugin.stubs.indexes.YamlRoutesStubIndex;%0Aimport fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher;%0Aimport org.apache.commons.lang.StringUtils;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.yaml.YAMLFileType;%0A%0Aimport java.util.Collection;%0Aimport java.util.Map;%0A%0Aabstract public class AbstractPhpRouteInspection extends LocalInspectionTool %7B
+util.MethodMatcher;%0Aimport org.apache.commons.lang.StringUtils;%0Aimport org.jetbrains.annotations.NotNull;%0A%0Aabstract public class AbstractPhpRouteInspection extends LocalInspectionTool %7B%0A%0A    @Override%0A    public boolean runForWholeFile() %7B%0A        return true;%0A    %7D
 %0A%0A  
@@ -1154,72 +1154,8 @@
 t())
- %7C%7C !Settings.getInstance(psiFile.getProject()).phpAnnotateRoute
 ) %7B%0A
@@ -1216,32 +1216,77 @@
 ly);%0A        %7D%0A%0A
+        if(psiFile instanceof PhpFile) %7B%0A    
         psiFile.
@@ -1338,24 +1338,28 @@
 Visitor() %7B%0A
+    
             
@@ -1376,24 +1376,28 @@
             
+    
 public void 
@@ -1427,24 +1427,28 @@
  element) %7B%0A
+    
             
@@ -1512,32 +1512,36 @@
                 
+ 
+   
 annotate((String
@@ -1534,16 +1534,17 @@
 notate((
+(
 StringLi
@@ -1559,16 +1559,40 @@
 ression)
+ element).getContents(),
  element
@@ -1610,34 +1610,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -1677,34 +1677,42 @@
 t);%0A            
+ 
-%7D%0A
+   %7D%0A    
         %7D);%0A%0A%0A  
@@ -1707,16 +1707,539 @@
     %7D);%0A
+        %7D%0A%0A        /* too slow%0A        if(psiFile instanceof TwigFile) %7B%0A            psiFile.acceptChildren(new PsiRecursiveElementWalkingVisitor() %7B%0A                @Override%0A                public void visitElement(PsiElement element) %7B%0A%0A                    if(TwigHelper.getAutocompletableRoutePattern().accepts(element)) %7B%0A                        annotateRouteName(element, holder, element.getText());%0A                    %7D%0A%0A                    super.visitElement(element);%0A                %7D%0A            %7D);%0A        %7D*/
 %0A%0A      
@@ -2323,46 +2323,51 @@
 ate(
-@NotNull final StringLiteralExpression
+String routeName, @NotNull final PsiElement
  ele
@@ -2411,58 +2411,8 @@
  %7B%0A%0A
-        String routeName = element.getContents();%0A
     

allow independent languages files for route inspection; twig too slow for active now