package de.espend.idea.php.annotation.dict;

import com.jetbrains.php.lang.documentation.phpdoc.parser.PhpDocElementTypes;
import com.jetbrains.php.lang.documentation.phpdoc.psi.tags.PhpDocTag;
import com.jetbrains.php.lang.psi.elements.PhpClass;
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import de.espend.idea.php.annotation.pattern.AnnotationPattern;
import de.espend.idea.php.annotation.util.PhpElementsUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PhpDocTagAnnotation {

    final private PhpClass phpClass;
    final private PhpDocTag phpDocTag;

    public PhpDocTagAnnotation(@NotNull PhpClass phpClass, @NotNull PhpDocTag phpDocTag) {
        this.phpClass = phpClass;
        this.phpDocTag = phpDocTag;
    }

    @NotNull
    public PhpClass getPhpClass() {
        return phpClass;
    }

    @NotNull
    public PhpDocTag getPhpDocTag() {
        return phpDocTag;
    }

    /**
     * Get property Value from "@Template(template="foo");
     *
     * @param propertyName property name template=""
     * @return Property value
     */
    @Nullable
    public String getPropertyValue(String propertyName) {
        PhpPsiElement docAttrList = phpDocTag.getFirstPsiChild();
        if(docAttrList != null) {
            StringLiteralExpression literalExpression = PhpElementsUtil.getChildrenOnPatternMatch(docAttrList, AnnotationPattern.getPropertyIdentifierValue(propertyName));
            if(literalExpression != null) {
                return literalExpression.getContents();
            }
        }

        return null;
    }

    /**
     * Get default property value from annotation "@Template("foo");
     *
     * @return Content of property value literal
     */
    @Nullable
    public String getDefaultPropertyValue() {
        PhpPsiElement phpDocAttrList = phpDocTag.getFirstPsiChild();

        if(phpDocAttrList != null) {
            if(phpDocAttrList.getNode().getElementType() == PhpDocElementTypes.phpDocAttributeList) {
                PhpPsiElement phpPsiElement = phpDocAttrList.getFirstPsiChild();
                if(phpPsiElement instanceof StringLiteralExpression) {
                    return ((StringLiteralExpression) phpPsiElement).getContents();
                }
            }
        }

        return null;
    }

}

@@ -38,16 +38,52 @@
 .dict;%0A%0A
+import com.intellij.psi.PsiElement;%0A
 import c
@@ -1290,24 +1290,472 @@
 ertyName) %7B%0A
+        StringLiteralExpression literalExpression = getPropertyValuePsi(propertyName);%0A        if(literalExpression != null) %7B%0A            return literalExpression.getContents();%0A        %7D%0A%0A        return null;%0A    %7D%0A%0A    /**%0A     * Get property psi element%0A     *%0A     * @param propertyName property name template=%22%22%0A     * @return Property value%0A     */%0A    @Nullable%0A    public StringLiteralExpression getPropertyValuePsi(String propertyName) %7B%0A
         PhpP
@@ -1858,51 +1858,14 @@
     
-StringLiteralExpression literalExpression =
+return
  Php
@@ -1981,122 +1981,8 @@
 ));%0A
-            if(literalExpression != null) %7B%0A                return literalExpression.getContents();%0A            %7D%0A
     

add additional proxy method to get property value as psielement