package fr.adrienbrault.idea.symfony2plugin.routing;

import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.codeInsight.daemon.LineMarkerProvider;
import com.intellij.codeInsight.navigation.NavigationGutterIconBuilder;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.php.lang.psi.elements.Method;
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.util.controller.ControllerIndex;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.yaml.psi.YAMLCompoundValue;
import org.jetbrains.yaml.psi.YAMLDocument;
import org.jetbrains.yaml.psi.YAMLHash;
import org.jetbrains.yaml.psi.YAMLKeyValue;

import java.util.Collection;
import java.util.List;

public class YamlLineMarkerProvider implements LineMarkerProvider {

    @Override
    public void collectSlowLineMarkers(@NotNull List<PsiElement> psiElements, @NotNull Collection<LineMarkerInfo> lineMarkerInfos) {

        if(psiElements.size() == 0 || !Symfony2ProjectComponent.isEnabled(psiElements.get(0))) {
            return;
        }

        for(PsiElement psiElement : psiElements) {
            attachRouteActions(lineMarkerInfos, psiElement);
        }

    }

    private void attachRouteActions(Collection<LineMarkerInfo> lineMarkerInfos, PsiElement psiElement) {

        /*
         * foo:
         *   defaults: { _controller: "Bundle:Foo:Bar" }
         *   defaults:
         *      _controller: "Bundle:Foo:Bar"
         */
        if(psiElement instanceof YAMLKeyValue && psiElement.getParent() instanceof YAMLDocument) {
            YAMLKeyValue yamlKeyValue = YamlHelper.getYamlKeyValue((YAMLKeyValue) psiElement, "defaults");
            if(yamlKeyValue != null) {
                YAMLCompoundValue yamlCompoundValue = PsiTreeUtil.getChildOfType(yamlKeyValue, YAMLCompoundValue.class);
                if(yamlCompoundValue != null) {

                    // if we have a child of YAMLKeyValue, we need to go back to parent
                    // else on YAMLHash we can directly visit array keys
                    PsiElement yamlHashElement = PsiTreeUtil.getChildOfAnyType(yamlCompoundValue, YAMLHash.class, YAMLKeyValue.class);
                    if(yamlHashElement instanceof YAMLKeyValue) {
                        yamlHashElement = yamlCompoundValue;
                    }

                    if(yamlHashElement != null) {
                        YAMLKeyValue yamlKeyValueController = YamlHelper.getYamlKeyValue(yamlHashElement, "_controller", true);
                        if(yamlKeyValueController != null) {

                            Method method = ControllerIndex.getControllerMethod(psiElement.getProject(), yamlKeyValueController.getValueText());
                            if(method != null) {
                                NavigationGutterIconBuilder<PsiElement> builder = NavigationGutterIconBuilder.create(Symfony2Icons.TWIG_CONTROLLER_LINE_MARKER).
                                    setTargets(method).
                                    setTooltipText("Navigate to action");

                                lineMarkerInfos.add(builder.createLineMarkerInfo(psiElement));
                            }

                        }

                    }
                }

            }

        }
    }

    @Nullable
    @Override
    public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement psiElement) {
        return null;
    }

}

@@ -265,24 +265,115 @@
 PsiElement;%0A
+import com.intellij.psi.PsiFile;%0Aimport com.intellij.psi.PsiInvalidElementAccessException;%0A
 import com.i
@@ -453,16 +453,69 @@
 Method;%0A
+import com.jetbrains.php.lang.psi.elements.PhpClass;%0A
 import f
@@ -629,24 +629,89 @@
 tComponent;%0A
+import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;%0A
 import fr.ad
@@ -835,24 +835,68 @@
 YamlHelper;%0A
+import org.apache.commons.lang.StringUtils;%0A
 import org.j
@@ -1660,26 +1660,1335 @@
 t);%0A        
+    attachEntityClass(lineMarkerInfos, psiElement);%0A        %7D%0A%0A    %7D%0A%0A    private void attachEntityClass(Collection%3CLineMarkerInfo%3E lineMarkerInfos, PsiElement psiElement) %7B%0A%0A        if(psiElement instanceof YAMLKeyValue && psiElement.getParent() instanceof YAMLDocument) %7B%0A%0A            PsiFile containingFile;%0A            try %7B%0A                containingFile = psiElement.getContainingFile();%0A            %7D catch (PsiInvalidElementAccessException e) %7B%0A                return;%0A            %7D%0A%0A            String fileName = containingFile.getName();%0A            if(fileName.endsWith(%22orm.yml%22) %7C%7C fileName.endsWith(%22odm.yml%22)) %7B%0A                String keyText = ((YAMLKeyValue) psiElement).getKeyText();%0A                if(StringUtils.isNotBlank(keyText)) %7B%0A                    PhpClass phpClass = PhpElementsUtil.getClass(psiElement.getProject(), keyText);%0A                    if(phpClass != null) %7B%0A                        NavigationGutterIconBuilder%3CPsiElement%3E builder = NavigationGutterIconBuilder.create(Symfony2Icons.DOCTRINE_LINE_MARKER).%0A                            setTargets(phpClass).%0A                            setTooltipText(%22Navigate to class%22);%0A%0A                        lineMarkerInfos.add(builder.createLineMarkerInfo(psiElement));%0A                    %7D%0A                %7D%0A            %7D%0A        
 %7D
-%0A
 %0A    %7D%0A%0A    

add class linemarker for yaml entities