/*
 * Copyright 2011 Jon S Akhtar (Sylvanaar)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sylvanaar.idea.Lua.lang.psi.resolve.completion;

import com.intellij.psi.*;
import com.sylvanaar.idea.Lua.lang.psi.impl.symbols.*;
import com.sylvanaar.idea.Lua.lang.psi.resolve.*;
import com.sylvanaar.idea.Lua.lang.psi.resolve.processors.*;
import com.sylvanaar.idea.Lua.lang.psi.symbols.*;
import org.jetbrains.annotations.*;

import java.util.*;

/**
 * @author ilyas
 */
public class CompletionProcessor extends SymbolResolveProcessor {

    public CompletionProcessor(PsiElement myPlace) {
        super(null, myPlace, true);
        setFilter(false);
    }

//    public Collection<PsiElement> getResultCollection() {
//        return ContainerUtil.map(myCandidates, new Function<LuaResolveResult, PsiElement>() {
//            @Override
//            public PsiElement fun(LuaResolveResult luaResolveResult) {
//                return luaResolveResult.getElement();
//            }
//        });
//    }

    @Override
    public void addCandidate(LuaResolveResult candidate) {
        if (getPlace() instanceof LuaCompoundReferenceElementImpl)
            if (!(candidate.getElement() instanceof LuaCompoundIdentifier))
                return;

        if (getPlace() instanceof LuaLocal)
            if (candidate.getElement() instanceof LuaCompoundIdentifier)
                return;

        super.addCandidate(candidate);
    }

    public PsiElement[] getResultElements() {

        PsiElement[] res = new PsiElement[myCandidates.size()];

        Iterator<LuaResolveResult> iter = myCandidates.iterator();

        for (int i = 0; i < res.length; i++)
            res[i] = iter.next().getElement();

        return res;
    }

    public boolean execute(@NotNull PsiElement element, ResolveState state) {
        super.execute(element, state);
        return true;
    }
}

@@ -683,13 +683,246 @@
 lij.
-psi.*
+codeInsight.lookup.LookupElement;%0Aimport com.intellij.psi.PsiElement;%0Aimport com.intellij.psi.ResolveState;%0Aimport com.sylvanaar.idea.Lua.editor.completion.LuaLookupElement;%0Aimport com.sylvanaar.idea.Lua.lang.psi.expressions.LuaExpression
 ;%0Aim
@@ -963,33 +963,63 @@
 si.impl.symbols.
-*
+LuaCompoundReferenceElementImpl
 ;%0Aimport com.syl
@@ -1051,17 +1051,32 @@
 resolve.
-*
+LuaResolveResult
 ;%0Aimport
@@ -1127,17 +1127,38 @@
 cessors.
-*
+SymbolResolveProcessor
 ;%0Aimport
@@ -1198,17 +1198,94 @@
 symbols.
-*
+LuaCompoundIdentifier;%0Aimport com.sylvanaar.idea.Lua.lang.psi.symbols.LuaLocal
 ;%0Aimport
@@ -1311,17 +1311,23 @@
 tations.
-*
+NotNull
 ;%0A%0Aimpor
@@ -1338,17 +1338,24 @@
 va.util.
-*
+Iterator
 ;%0A%0A/**%0A 
@@ -2347,27 +2347,30 @@
 %0A    public 
-Psi
+Lookup
 Element%5B%5D ge
@@ -2398,19 +2398,22 @@
         
-Psi
+Lookup
 Element%5B
@@ -2424,19 +2424,22 @@
 s = new 
-Psi
+Lookup
 Element%5B
@@ -2594,16 +2594,64 @@
 res%5Bi%5D =
+ LuaLookupElement.createElement(((LuaExpression)
  iter.ne
@@ -2667,16 +2667,18 @@
 lement()
+))
 ;%0A%0A     

Make completions look consistant