package com.tang.intellij.lua.editor.completion;

import com.intellij.codeInsight.completion.*;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.patterns.PsiElementPattern;
import com.intellij.psi.PsiElement;
import com.intellij.util.ProcessingContext;
import com.tang.intellij.lua.doc.psi.LuaDocClassDef;
import com.tang.intellij.lua.psi.LuaIndexExpr;
import com.tang.intellij.lua.psi.LuaNameRef;
import com.tang.intellij.lua.psi.LuaTypeResolvable;
import org.jetbrains.annotations.NotNull;

import static com.intellij.patterns.PlatformPatterns.psiElement;

/**
 *
 * Created by tangzx on 2016/11/27.
 */
public class LuaCompletionContributor extends CompletionContributor {

    private static final PsiElementPattern.Capture<PsiElement> AFTER_DOT = psiElement().afterLeaf(
            psiElement().withText(".").withParent(LuaIndexExpr.class));

    public LuaCompletionContributor() {
        extend(CompletionType.BASIC, AFTER_DOT, new CompletionProvider<CompletionParameters>() {
            @Override
            protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet) {
                Project project = completionParameters.getOriginalFile().getProject();

                //global functions
                //Collection<String> all = LuaGlobalFuncIndex.getInstance().getAllKeys(completionParameters.getOriginalFile().getProject());
                //all.forEach(name -> completionResultSet.addElement(LookupElementBuilder.create(name)));

                //class
                //Collection<String> allClasses = LuaClassIndex.getInstance().getAllKeys(project);
                //allClasses.forEach(className -> completionResultSet.addElement(LookupElementBuilder.create(className)));

                PsiElement element = completionParameters.getOriginalFile().findElementAt(completionParameters.getOffset() - 1);
                if (element != null) {
                    LuaIndexExpr indexExpr = (LuaIndexExpr) element.getParent();
                    PsiElement prev = indexExpr.getPrevSibling();
                    if (prev instanceof LuaNameRef) {
                        LuaNameRef ref = (LuaNameRef) prev;
                        PsiElement resolve = ref.resolve();
                        if (resolve instanceof LuaTypeResolvable) {
                            LuaTypeResolvable typeResolvable = (LuaTypeResolvable) resolve;
                            LuaDocClassDef classDef = typeResolvable.resolveType();
                            if (classDef != null) {
                                completionResultSet.addElement(LookupElementBuilder.create(classDef.getClassName()));
                            }
                        }
                    }
                }
            }
        });
    }

}

@@ -279,16 +279,73 @@
 lement;%0A
+import com.intellij.psi.search.ProjectAndLibrariesScope;%0A
 import c
@@ -380,16 +380,16 @@
 ontext;%0A
-
 import c
@@ -429,24 +429,75 @@
 ocClassDef;%0A
+import com.tang.intellij.lua.psi.LuaGlobalFuncDef;%0A
 import com.t
@@ -628,16 +628,75 @@
 lvable;%0A
+import com.tang.intellij.lua.psi.index.LuaGlobalFuncIndex;%0A
 import o
@@ -730,16 +730,46 @@
 tNull;%0A%0A
+import java.util.Collection;%0A%0A
 import s
@@ -2804,24 +2804,60 @@
 solveType();
+%0A%0A                            //%E6%8F%90%E7%A4%BA%E6%96%B9%E6%B3%95
 %0A           
@@ -2889,32 +2889,355 @@
 sDef != null) %7B%0A
+                                String clazzName = classDef.getClassName().getText();%0A                                Collection%3CLuaGlobalFuncDef%3E list = LuaGlobalFuncIndex.getInstance().get(clazzName, project, new ProjectAndLibrariesScope(project));%0A                                for (LuaGlobalFuncDef def : list) %7B%0A    
                 
@@ -3311,39 +3311,51 @@
 .create(
-classD
+d
 ef.get
-Class
+Func
 Name()
+.getId().getText()
 ));%0A    
@@ -3370,33 +3370,103 @@
                 
-%7D
+    %7D%0A                            %7D%0A%0A                            //%E6%8F%90%E7%A4%BA%E5%B1%9E%E6%80%A7
 %0A               
@@ -3548,11 +3548,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

test completion