package com.haskforce.psi.references;

import com.haskforce.utils.HaskellUtil;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementResolveResult;
import com.intellij.psi.PsiNamedElement;
import com.intellij.psi.PsiPolyVariantReference;
import com.intellij.psi.PsiReferenceBase;
import com.intellij.psi.ResolveResult;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
 * Resolves references to elements.
 */
public class HaskellReference extends PsiReferenceBase<PsiElement> implements PsiPolyVariantReference {
    private String name;

    public HaskellReference(@NotNull PsiNamedElement element, TextRange textRange) {
        super(element, textRange);
        name = element.getName();
    }

    @NotNull
    @Override
    public ResolveResult[] multiResolve(boolean incompleteCode) {
        Project project = myElement.getProject();
        final List<PsiNamedElement> namedElements = HaskellUtil.findNamedNode(project, name);
        // Guess 20 variants tops most of the time in any real code base.
        List<ResolveResult> results = new ArrayList<ResolveResult>(20);
        for (PsiNamedElement property : namedElements) {
            //noinspection ObjectAllocationInLoop
            results.add(new PsiElementResolveResult(property));
        }
        return results.toArray(new ResolveResult[results.size()]);
    }

    @Nullable
    @Override
    public PsiElement resolve() {
        ResolveResult[] resolveResults = multiResolve(false);
        return resolveResults.length == 1 ? resolveResults[0].getElement() : null;
    }

    @NotNull
    @Override
    public Object[] getVariants() {
        /*
        Project project = myElement.getProject();
        List<PsiNamedElement> namedNodes = HaskellUtil.findNamedNodes(project);
        List<LookupElement> variants = new ArrayList<LookupElement>();
        for (final PsiNamedElement namedElement : namedNodes) {
            if (namedElement.getName() != null && !namedElement.getName().isEmpty()) {
                variants.add(LookupElementBuilder.create(namedElement).
                                withIcon(HaskellIcons.FILE).
                                withTypeText(namedElement.getContainingFile().getName())
                );
            }
        }
        return variants.toArray();*/
        return EMPTY_ARRAY;
    }

    @Override
    public TextRange getRangeInElement() {
        return new TextRange(0, this.getElement().getNode().getTextLength());
    }
}

@@ -1734,32 +1734,150 @@
  : null;%0A    %7D%0A%0A
+    /**%0A     * Controls what names that get added to the autocompletion popup available%0A     * on ctrl-space.%0A     */%0A
     @NotNull%0A   
@@ -1933,16 +1933,67 @@
       /*
+ FIXME: Overwrites the local history with bad data.
 %0A       

Document where identifier completion happens.