package com.cedricziel.idea.typo3.annotation;

import com.cedricziel.idea.typo3.container.RouteProvider;
import com.cedricziel.idea.typo3.psi.PhpElementsUtil;
import com.intellij.lang.annotation.Annotation;
import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.lang.annotation.Annotator;
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import org.jetbrains.annotations.NotNull;

public class RouteAnnotator implements Annotator {
    @Override
    public void annotate(@NotNull PsiElement psiElement, @NotNull AnnotationHolder annotationHolder) {

        if (!(psiElement instanceof StringLiteralExpression)) {
            return;
        }

        StringLiteralExpression literalExpression = (StringLiteralExpression) psiElement;
        String value = literalExpression.getContents();

        if (value.isEmpty()) {
            return;
        }

        PsiElement methodReference = PsiTreeUtil.getParentOfType(psiElement, MethodReference.class);
        if (PhpElementsUtil.isMethodWithFirstStringOrFieldReference(methodReference, "getAjaxUrl")) {
            annotateAjaxRoutes(psiElement, annotationHolder, value);
        }
        if (PhpElementsUtil.isMethodWithFirstStringOrFieldReference(methodReference, "buildUriFromRoute")) {
            annotateRoutes(psiElement, annotationHolder, value);
        }
    }

    private void annotateAjaxRoutes(PsiElement psiElement, AnnotationHolder annotationHolder, String value) {
        RouteProvider routeProvider = new RouteProvider();
        routeProvider.collect(psiElement.getProject());

        annotateRoute(psiElement, annotationHolder, value, routeProvider, RouteProvider.ROUTE_TYPE_AJAX);
    }

    private void annotateRoutes(PsiElement psiElement, AnnotationHolder annotationHolder, String value) {
        RouteProvider routeProvider = new RouteProvider();
        routeProvider.collect(psiElement.getProject());

        annotateRoute(psiElement, annotationHolder, value, routeProvider, RouteProvider.ROUTE_TYPE_BACKEND);
    }


    private void annotateRoute(PsiElement psiElement, AnnotationHolder annotationHolder, String value, RouteProvider routeProvider, String routeType) {
        if (routeProvider.has(value, routeType)) {
            TextRange range = new TextRange(psiElement.getTextRange().getStartOffset(), psiElement.getTextRange().getEndOffset());
            Annotation annotation = annotationHolder.createInfoAnnotation(range, null);
            annotation.setTextAttributes(DefaultLanguageHighlighterColors.LINE_COMMENT);
        } else {
            TextRange range = new TextRange(psiElement.getTextRange().getStartOffset(), psiElement.getTextRange().getEndOffset());
            annotationHolder.createErrorAnnotation(range, "Unresolved route");
        }
    }
}

@@ -488,24 +488,92 @@
 siTreeUtil;%0A
+import com.jetbrains.php.lang.psi.elements.ArrayCreationExpression;%0A
 import com.j
@@ -1403,16 +1403,55 @@
 jaxUrl%22)
+ && !hasArrayCreationParent(psiElement)
 ) %7B%0A    
@@ -1630,16 +1630,55 @@
 mRoute%22)
+ && !hasArrayCreationParent(psiElement)
 ) %7B%0A    
@@ -1747,32 +1747,284 @@
        %7D%0A    %7D%0A%0A
+    private boolean hasArrayCreationParent(@NotNull PsiElement psiElement) %7B%0A        if (psiElement.getParent() == null) %7B%0A            return false;%0A        %7D%0A        return psiElement.getParent().getParent() instanceof ArrayCreationExpression;%0A    %7D%0A%0A
     private void
@@ -3185,20 +3185,14 @@
 ors.
-LINE_COMMENT
+STRING
 );%0A 

Fix faulty error highlighting for route parameters