package com.daveme.intellij.chocolateCakePHP.navigation;

import com.daveme.intellij.chocolateCakePHP.util.PsiUtil;
import com.daveme.intellij.chocolateCakePHP.util.StringUtil;
import com.intellij.codeInsight.navigation.actions.GotoDeclarationHandler;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.patterns.PlatformPatterns;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.jetbrains.php.lang.PhpLanguage;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.HashSet;

public class ElementGotoDeclarationHandler implements GotoDeclarationHandler {

    @Nullable
    @Override
    public PsiElement[] getGotoDeclarationTargets(@Nullable PsiElement psiElement, int i, Editor editor) {
        if (psiElement == null) {
            return new PsiElement[0];
        }
        Project project = psiElement.getProject();
        if (!PlatformPatterns
                .psiElement(StringLiteralExpression.class)
                .withLanguage(PhpLanguage.INSTANCE)
                .accepts(psiElement.getContext())
        ) {
            return new PsiElement[0];
        }
        PsiFile containingFile = psiElement.getContainingFile();
        String path = containingFile.getVirtualFile().getCanonicalPath();
        String appDir = StringUtil.lastOccurrenceOf(path, "app");
        if (appDir == null) {
            return new PsiElement[0];
        }
        String elementPath = String.format("%s/View/Elements/%s.ctp", appDir, psiElement.getText());
        VirtualFileManager vfManager = VirtualFileManager.getInstance();
        VirtualFile fileByUrl = vfManager.findFileByUrl(VirtualFileManager.constructUrl("file", elementPath));
        if (fileByUrl != null) {
            Collection<VirtualFile> files = new HashSet<>();
            files.add(fileByUrl);
            return PsiUtil.convertVirtualFilesToPsiFiles(project, files).toArray(new PsiElement[files.size()]);
        }
        return new PsiElement[0];
    }

    @Nullable
    @Override
    public String getActionText(DataContext dataContext) {
        return null;
    }
}

@@ -1071,36 +1071,32 @@
          return 
-new 
 PsiElement%5B0%5D;%0A 
@@ -1081,35 +1081,44 @@
 eturn PsiElement
-%5B0%5D
+.EMPTY_ARRAY
 ;%0A        %7D%0A    
@@ -1378,36 +1378,32 @@
          return 
-new 
 PsiElement%5B0%5D;%0A 
@@ -1388,35 +1388,44 @@
 eturn PsiElement
-%5B0%5D
+.EMPTY_ARRAY
 ;%0A        %7D%0A    
@@ -1555,24 +1555,105 @@
 icalPath();%0A
+        if (path == null) %7B%0A            return PsiElement.EMPTY_ARRAY;%0A        %7D%0A
         Stri
@@ -1747,36 +1747,32 @@
          return 
-new 
 PsiElement%5B0%5D;%0A 
@@ -1761,27 +1761,36 @@
 n PsiElement
-%5B0%5D
+.EMPTY_ARRAY
 ;%0A        %7D%0A
@@ -2339,20 +2339,16 @@
  return 
-new 
 PsiEleme
@@ -2353,11 +2353,20 @@
 ment
-%5B0%5D
+.EMPTY_ARRAY
 ;%0A  

Use PsiElement.EMPTY_ARRAY