package com.reason.ide.insight.provider;

import com.intellij.codeInsight.AutoPopupController;
import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionProvider;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.lookup.LookupElementBuilder;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorModificationUtil;
import com.intellij.openapi.project.Project;
import com.intellij.util.ProcessingContext;
import com.reason.ide.files.RmlFile;
import com.reason.lang.core.PsiFinder;
import com.reason.lang.core.PsiSignatureUtil;
import com.reason.lang.core.psi.PsiModule;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;

import static com.intellij.util.PsiIconUtil.getProvidersIcon;
import static com.reason.lang.core.MlFileType.interfaceOrImplementation;

public class JsxNameCompletionProvider extends CompletionProvider<CompletionParameters> {
    @Override
    protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext processingContext, @NotNull CompletionResultSet resultSet) {
        //System.out.println("JsxNameCompletionProvider.addCompletions");

        RmlFile originalFile = (RmlFile) parameters.getOriginalFile();
        String fileModuleName = originalFile.asModuleName();
        Project project = originalFile.getProject();

        // Find all files that are components ! TODO: components can be sub modules
        Collection<PsiModule> modules = PsiFinder.getInstance().findFileModules(project, interfaceOrImplementation);
        for (PsiModule module : modules) {
            String moduleName = module.getName();
            if (!fileModuleName.equals(moduleName) && module.isComponent()) {
                resultSet.addElement(LookupElementBuilder.create(module).
                        withIcon(getProvidersIcon(module, 0)).
                        withTypeText(PsiSignatureUtil.getProvidersType(module.getLetExpression("make"))).
                        withInsertHandler((context, item) -> insertTagNameHandler(project, context, moduleName))
                );
            }
        }
    }

    private static void insertTagNameHandler(Project project, InsertionContext context, String tagName) {
        char completionChar = context.getCompletionChar();
        if (completionChar == ' ') {
            context.setAddCompletionChar(false);
        }

        Editor editor = context.getEditor();
        EditorModificationUtil.insertStringAtCaret(editor, " ></" + tagName + ">");
        editor.getCaretModel().moveToOffset(editor.getCaretModel().getOffset() - 4 - tagName.length());

        AutoPopupController.getInstance(project).autoPopupMemberLookup(editor, null);
    }
}

@@ -398,24 +398,71 @@
 entBuilder;%0A
+import com.intellij.openapi.diagnostic.Logger;%0A
 import com.i
@@ -632,32 +632,61 @@
 cessingContext;%0A
+import com.reason.ide.Debug;%0A
 import com.reaso
@@ -1133,16 +1133,175 @@
 ters%3E %7B%0A
+    private final Debug m_debug;%0A%0A    public JsxNameCompletionProvider() %7B%0A        m_debug = new Debug(Logger.getInstance(%22ReasonML.insight.jsxname%22));%0A    %7D%0A%0A
     @Ove
@@ -1473,60 +1473,44 @@
     
-//System.out.println(%22JsxNameCompletionProvider.addC
+m_debug.debug(%22JSX name expression c
 ompl
@@ -1514,17 +1514,16 @@
 mpletion
-s
 %22);%0A%0A   
@@ -1906,16 +1906,72 @@
 ation);%0A
+        m_debug.debug(%22Modules found%22, modules.size());%0A
         
@@ -2047,32 +2047,172 @@
 dule.getName();%0A
+            if (m_debug.isDebugEnabled()) %7B%0A                m_debug.debug(%22 is component%22, moduleName, module.isComponent());%0A            %7D%0A
             if (

use debug in jsx name completion