package de.espend.idea.shopware.index;

import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiRecursiveElementWalkingVisitor;
import com.intellij.util.indexing.*;
import com.intellij.util.io.DataExternalizer;
import com.intellij.util.io.EnumeratorStringDescriptor;
import com.intellij.util.io.KeyDescriptor;
import com.jetbrains.php.lang.PhpFileType;
import com.jetbrains.php.lang.psi.elements.MethodReference;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author Daniel Espendiller <daniel@espendiller.net>
 */
public class EventConfigGoToIndex extends FileBasedIndexExtension<String, Void> {

    public static final ID<String, Void> KEY = ID.create("de.espend.idea.shopware.event_config_go_to_index");
    private final KeyDescriptor<String> myKeyDescriptor = new EnumeratorStringDescriptor();

    public static final Set<String> METHOD_NAMES = new HashSet<String>(Arrays.asList("notify", "notifyUntil", "setElement", "addElement","filter"));

    @NotNull
    @Override
    public ID<String, Void> getName() {
        return KEY;
    }

    @NotNull
    @Override
    public DataIndexer<String, Void, FileContent> getIndexer() {
        return new DataIndexer<String, Void, FileContent>() {

            @NotNull
            @Override
            public Map<String, Void> map(FileContent inputData) {
                final Map<String, Void> map = new THashMap<String, Void>();

                PsiFile psiFile = inputData.getPsiFile();
                if(!Symfony2ProjectComponent.isEnabled(psiFile.getProject())) {
                    return map;
                }

                psiFile.acceptChildren(new PsiRecursiveElementWalkingVisitor() {
                    @Override
                    public void visitElement(PsiElement element) {

                        if(element instanceof MethodReference && METHOD_NAMES.contains(((MethodReference) element).getName())) {
                            map.put(((MethodReference) element).getName(), null);
                        }

                        super.visitElement(element);
                    }

                });

                return map;
            }


        };
    }

    @Override
    public KeyDescriptor<String> getKeyDescriptor() {
        return this.myKeyDescriptor;
    }

    @Override
    public DataExternalizer<Void> getValueExternalizer() {
        return ScalarIndexExtension.VOID_DATA_EXTERNALIZER;
    }

    @Override
    public FileBasedIndex.InputFilter getInputFilter() {
        return new FileBasedIndex.InputFilter() {
            @Override
            public boolean acceptInput(VirtualFile file) {
                return file.getFileType() == PhpFileType.INSTANCE;
            }
        };
    }

    @Override
    public boolean dependsOnFileContent() {
        return true;
    }

    @Override
    public int getVersion() {
        return 1;
    }
}

@@ -2417,32 +2417,45 @@
       %7D;%0A    %7D%0A%0A
+    @NotNull%0A
     @Override%0A  
@@ -2444,32 +2444,32 @@
 l%0A    @Override%0A
-
     public KeyDe
@@ -2542,32 +2542,45 @@
 criptor;%0A    %7D%0A%0A
+    @NotNull%0A
     @Override%0A  
@@ -2695,32 +2695,45 @@
 NALIZER;%0A    %7D%0A%0A
+    @NotNull%0A
     @Override%0A  
@@ -2851,32 +2851,32 @@
       @Override%0A
-
             publ
@@ -2898,16 +2898,25 @@
 ptInput(
+@NotNull 
 VirtualF

add annotations