package fr.adrienbrault.idea.symfony2plugin.config.component;

import com.intellij.codeInsight.completion.InsertHandler;
import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.codeInsight.lookup.LookupElementPresentation;
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
import fr.adrienbrault.idea.symfony2plugin.dic.ContainerParameter;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;

/**
 * @author Daniel Espendiller <daniel@espendiller.net>
 */
public class ParameterLookupElement extends LookupElement {

    private String parameterKey;
    private String parameterValue;
    private Object psiElement;
    private InsertHandler<LookupElement> insertHandler = null;
    private ContainerParameter containerParameter;

    public ParameterLookupElement(ContainerParameter containerParameter) {
        this.containerParameter = containerParameter;
    }

    public ParameterLookupElement(ContainerParameter containerParameter, InsertHandler<LookupElement> insertHandler, Object psiElement) {
        this.insertHandler = insertHandler;
        this.psiElement = psiElement;
    }

    @NotNull
    @Override
    public Object getObject() {
        return this.psiElement != null ? this.psiElement : this;
    }

    public void handleInsert(InsertionContext context) {
        if (this.insertHandler != null) {
            this.insertHandler.handleInsert(context, this);
        }
    }

    @NotNull
    @Override
    public String getLookupString() {
        return parameterKey;
    }

    public void renderElement(LookupElementPresentation presentation) {
        presentation.setItemText(getLookupString());

        if(parameterValue != null && !StringUtils.isBlank(parameterValue)) {
            presentation.setTypeText(parameterValue);
        }

        presentation.setTypeGrayed(true);
        presentation.setIcon(Symfony2Icons.PARAMETER);

        if(this.containerParameter != null && this.containerParameter.isWeak()) {
            presentation.setIcon(Symfony2Icons.PARAMETER_OPACITY);
        }

    }

}
@@ -635,76 +635,8 @@
  %7B%0A%0A
-    private String parameterKey;%0A    private String parameterValue;%0A
     
@@ -658,24 +658,24 @@
 psiElement;%0A
+
     private 
@@ -1129,24 +1129,78 @@
 psiElement;%0A
+        this.containerParameter = containerParameter;%0A
     %7D%0A%0A    @
@@ -1585,20 +1585,36 @@
 urn 
-parameterKey
+containerParameter.getName()
 ;%0A  
@@ -1756,20 +1756,41 @@
     
-if(p
+String value = containerP
 arameter
 Valu
@@ -1785,21 +1785,45 @@
 arameter
+.get
 Value
+();%0A        if(value
  != null
@@ -1826,17 +1826,16 @@
 null && 
-!
 StringUt
@@ -1844,24 +1844,18 @@
 s.is
+Not
 Blank(
-parameterV
+v
 alue
@@ -1900,18 +1900,9 @@
 ext(
-parameterV
+v
 alue

fix possible npe in ParameterLookupElement #351