package fr.adrienbrault.idea.symfony2plugin.util.completion;


import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.completion.CompletionProvider;
import com.intellij.codeInsight.completion.CompletionResultSet;
import com.intellij.psi.PsiElement;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.PhpIndex;
import com.jetbrains.php.completion.PhpLookupElement;
import com.jetbrains.php.lang.psi.stubs.indexes.PhpClassIndex;
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
import fr.adrienbrault.idea.symfony2plugin.config.component.ParameterLookupElement;
import fr.adrienbrault.idea.symfony2plugin.config.component.parser.ParameterServiceParser;
import fr.adrienbrault.idea.symfony2plugin.config.yaml.ParameterPercentWrapInsertHandler;
import fr.adrienbrault.idea.symfony2plugin.dic.ServiceStringLookupElement;
import fr.adrienbrault.idea.symfony2plugin.util.service.ServiceXmlParserFactory;
import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;
import org.jetbrains.annotations.NotNull;

import java.util.Map;

public class PhpClassAndParameterCompletionProvider extends CompletionProvider<CompletionParameters> {

    public void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {

        PsiElement psiElement = parameters.getOriginalPosition();
        if(psiElement == null || !Symfony2ProjectComponent.isEnabled(psiElement)) {
            return;
        }

        PhpIndex phpIndex = PhpIndex.getInstance(psiElement.getProject());

        for (String className : phpIndex.getAllClassNames(resultSet.getPrefixMatcher())) {
            resultSet.addElement(new PhpLookupElement(className, PhpClassIndex.KEY, parameters.getOriginalFile().getProject(), PhpClassReferenceInsertHandler.getInstance()));
        }

        // ParameterPercentWrapInsertHandler
        // @TODO: we need a parameter class dict
        Map<String, String> configParameters = ServiceXmlParserFactory.getInstance(parameters.getOriginalFile().getProject(), ParameterServiceParser.class).getParameterMap();
        for(Map.Entry<String, String> Entry: configParameters.entrySet()) {
            // some better class filter
            if(Entry.getValue().contains("\\") && resultSet.getPrefixMatcher().prefixMatches(Entry.getValue())) {
                resultSet.addElement(new ParameterLookupElement(Entry.getKey(), Entry.getValue(), ParameterPercentWrapInsertHandler.getInstance(), psiElement.getText()));
            }
        }

        for( Map.Entry<String, String> entry: YamlHelper.getLocalParameterMap(psiElement).entrySet()) {
            if(!configParameters.containsKey(entry.getKey())) {
                resultSet.addElement(
                    new ParameterLookupElement(entry.getKey(), entry.getValue(), ParameterPercentWrapInsertHandler.getInstance(), psiElement.getText())
                );
            }
        }

    }

}

@@ -637,99 +637,8 @@
 nt;%0A
-import fr.adrienbrault.idea.symfony2plugin.config.component.parser.ParameterServiceParser;%0A
 impo
@@ -774,34 +774,26 @@
 dic.
-ServiceStringLookupElement
+ContainerParameter
 ;%0Aim
@@ -837,107 +837,39 @@
 gin.
-util.service.ServiceXmlParserFactory;%0Aimport fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelp
+stubs.ContainerCollectionResolv
 er;%0A
@@ -1727,760 +1727,89 @@
     
-// ParameterPercentWrapInsertHandler%0A        // @TODO: we need a parameter class dict%0A        Map%3CString, String%3E configParameters = ServiceXmlParserFactory.getInstance(parameters.getOriginalFile().getProject(), ParameterServiceParser.class).getParameterMap();%0A        for(Map.Entry%3CString, String%3E Entry: configParameters.entrySet()) %7B%0A            // some better class filter%0A            if(Entry.getValue().contains(%22%5C%5C%22) && resultSet.getPrefixMatcher().prefixMatches(Entry.getValue())) %7B%0A                resultSet.addElement(new ParameterLookupElement(Entry.getKey(), Entry.getValue(), ParameterPercentWrapInsertHandler.getInstance(), psiElement.getText()));%0A            %7D%0A        %7D%0A%0A        for( Map.Entry%3CString, String%3E entry: YamlHelp
+for( Map.Entry%3CString, ContainerParameter%3E entry: ContainerCollectionResolv
 er.get
-Local
 Para
@@ -1813,19 +1813,17 @@
 arameter
-Map
+s
 (psiElem
@@ -1829,92 +1829,37 @@
 ment
-).entrySet()) %7B%0A            if(!configParameters.containsKey(entry.getKey()
+.getProject()).entrySet(
 )) %7B%0A
-    
     
@@ -1904,20 +1904,16 @@
         
- 
-   
 new Para
@@ -1935,24 +1935,8 @@
 ent(
-entry.getKey(), 
 entr
@@ -2036,28 +2036,10 @@
     
-    );%0A            %7D
+);
 %0A   

remove local map from PhpClassAndParameterCompletionProvider