package com.reason.ide.insight.provider;

import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionProvider;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.project.Project;
import com.intellij.util.ProcessingContext;
import com.intellij.util.PsiIconUtil;
import com.reason.Platform;
import com.reason.ide.files.FileBase;
import com.reason.lang.core.PsiFinder;
import com.reason.lang.core.PsiSignatureUtil;
import com.reason.lang.core.psi.PsiModule;
import com.reason.lang.core.psi.PsiNamedElement;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.Collection;

import static com.reason.lang.core.MlFileType.interfaceOrImplementation;

public class FreeExpressionCompletionProvider extends CompletionProvider<CompletionParameters> {

    @Override
    protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {
        //System.out.println("»» FREE expression completion");

        Project project = parameters.getOriginalFile().getProject();

        // Find all possible paths
        // todo

        // Add file modules
        Collection<PsiModule> fileModules = PsiFinder.getInstance().findFileModules(project, interfaceOrImplementation);
        for (PsiModule module : fileModules) {
            if (!module.isComponent()) {
                resultSet.addElement(
                        LookupElementBuilder.create(module).
                                withTypeText(Platform.removeProjectDir(project, module.getContainingFile().getVirtualFile()).replace("node_modules" + File.separator, "")).
                                withIcon(PsiIconUtil.getProvidersIcon(module, 0))
                );
            }
        }

        // Add all local expressions
        PsiModule currentFileModule = ((FileBase) parameters.getOriginalFile()).asModule();
        if (currentFileModule != null) {
            for (PsiNamedElement expression : currentFileModule.getExpressions()) {
                resultSet.addElement(
                        LookupElementBuilder.create(expression).
                                withTypeText(PsiSignatureUtil.getProvidersType(expression)).
                                withIcon(PsiIconUtil.getProvidersIcon(expression, 0))
                );
            }

            // Add open and includes
            //Collection<PsiOpen> openExpressions = currentFileModule.getOpenExpressions();
            //for (PsiOpen openExpression : openExpressions) {
            // todo
            //}

            //Collection<PsiInclude> includeExpressions = currentFileModule.getIncludeExpressions();
            //for (PsiInclude includeExpression : includeExpressions) {
            // todo
            //}
        }
    }

}

@@ -284,24 +284,71 @@
 entBuilder;%0A
+import com.intellij.openapi.diagnostic.Logger;%0A
 import com.i
@@ -380,16 +380,16 @@
 roject;%0A
-
 import c
@@ -490,16 +490,45 @@
 atform;%0A
+import com.reason.ide.Debug;%0A
 import c
@@ -557,16 +557,53 @@
 leBase;%0A
+import com.reason.lang.core.MlScope;%0A
 import c
@@ -1035,16 +1035,105 @@
 eters%3E %7B
+%0A    private final Debug m_debug = new Debug(Logger.getInstance(%22ReasonML.insight.dot%22));
 %0A%0A    @O
@@ -1297,33 +1297,23 @@
     
-//System.out.println(%22%C2%BB%C2%BB 
+m_debug.debug(%22
 FREE
@@ -3108,22 +3108,627 @@
 /%7D%0A        %7D
+%0A%0A        // Add pervasives expressions%0A        PsiModule pervasives = PsiFinder.getInstance().findModule(project, %22Pervasives%22, interfaceOrImplementation, MlScope.all);%0A        if (pervasives != null) %7B%0A            for (PsiNamedElement expression : pervasives.getExpressions()) %7B%0A                resultSet.addElement(%0A                        LookupElementBuilder.create(expression).%0A                                withTypeText(PsiSignatureUtil.getProvidersType(expression)).%0A                                withIcon(PsiIconUtil.getProvidersIcon(expression, 0))%0A                );%0A            %7D%0A        %7D
 %0A    %7D%0A%0A%7D%0A

add Pervasives to free expressions