package de.espend.idea.php.drupal.references;

import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.*;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.lang.PhpLanguage;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import de.espend.idea.php.drupal.DrupalProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.routing.RouteReference;
import fr.adrienbrault.idea.symfony2plugin.util.MethodMatcher;
import org.jetbrains.annotations.NotNull;

public class PhpRouteReferenceContributor extends PsiReferenceContributor {

    public static MethodMatcher.CallToSignature[] GENERATOR_SIGNATURES = new MethodMatcher.CallToSignature[] {
        new MethodMatcher.CallToSignature("\\Drupal\\Core\\Routing\\UrlGeneratorInterface", "getPathFromRoute"),
        new MethodMatcher.CallToSignature("\\Drupal\\Core\\Routing\\UrlGeneratorInterface", "generateFromRoute"),
    };


    @Override
    public void registerReferenceProviders(PsiReferenceRegistrar psiReferenceRegistrar) {

        psiReferenceRegistrar.registerReferenceProvider(
            PlatformPatterns.psiElement(StringLiteralExpression.class).withLanguage(PhpLanguage.INSTANCE),
            new PsiReferenceProvider() {
                @NotNull
                @Override
                public PsiReference[] getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) {

                    if(!DrupalProjectComponent.isEnabled(psiElement)) {
                        return new PsiReference[0];
                    }

                    if (MethodMatcher.getMatchedSignatureWithDepth(psiElement, GENERATOR_SIGNATURES) == null) {
                        return new PsiReference[0];
                    }

                    return new PsiReference[]{ new RouteReference((StringLiteralExpression) psiElement) };
                }
            }
        );

    }
}

@@ -244,39 +244,17 @@
 lements.
-StringLiteralExpression
+*
 ;%0Aimport
@@ -773,24 +773,63 @@
 FromRoute%22),
+ // %3C- %3C@TODO: remove: pre Drupal8 beta
 %0A        new
@@ -930,16 +930,343 @@
 Route%22),
+ // %3C- %3C@TODO: remove: pre Drupal8 beta%0A        new MethodMatcher.CallToSignature(%22%5C%5CDrupal%5C%5CCore%5C%5CRouting%5C%5CUrlGenerator%22, %22getPathFromRoute%22),%0A        new MethodMatcher.CallToSignature(%22%5C%5CDrupal%5C%5CCore%5C%5CRouting%5C%5CUrlGenerator%22, %22generateFromRoute%22),%0A        new MethodMatcher.CallToSignature(%22%5C%5CDrupal%5C%5CCore%5C%5CUrl%22, %22fromRoute%22),
 %0A    %7D;%0A
@@ -1266,17 +1266,16 @@
     %7D;%0A%0A
-%0A
     @Ove
@@ -2095,32 +2095,32 @@
              %7D%0A%0A
-
                 
@@ -2254,16 +2254,1687 @@
     );%0A%0A
+        psiReferenceRegistrar.registerReferenceProvider(%0A            PlatformPatterns.psiElement(StringLiteralExpression.class).withLanguage(PhpLanguage.INSTANCE),%0A            new PsiReferenceProvider() %7B%0A                @NotNull%0A                @Override%0A                public PsiReference%5B%5D getReferencesByElement(@NotNull PsiElement psiElement, @NotNull ProcessingContext processingContext) %7B%0A%0A                    if(!DrupalProjectComponent.isEnabled(psiElement)) %7B%0A                        return new PsiReference%5B0%5D;%0A                    %7D%0A%0A                    if(psiElement instanceof StringLiteralExpression) %7B%0A                        PsiElement parameterList = psiElement.getParent();%0A                        if(parameterList instanceof ParameterList) %7B%0A                            PsiElement newExpression = parameterList.getParent();%0A                            if(newExpression instanceof NewExpression) %7B%0A                                ClassReference classReference = ((NewExpression) newExpression).getClassReference();%0A                                if(classReference != null && %22Url%22.equals(classReference.getName())) %7B%0A                                    String fqn = classReference.getFQN();%0A                                    if(fqn != null && fqn.equals(%22%5C%5CDrupal%5C%5CCore%5C%5CUrl%22)) %7B%0A                                        return new PsiReference%5B%5D%7B new RouteReference((StringLiteralExpression) psiElement) %7D;%0A                                    %7D%0A                                %7D%0A                            %7D%0A                        %7D%0A                    %7D%0A%0A                    return new PsiReference%5B0%5D;%0A                %7D%0A            %7D%0A        );%0A%0A
     %7D%0A%7D%0A

add new route method signatures also support Drupal\Core\Url #2