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

import com.intellij.codeInsight.completion.CompletionConfidence;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.util.ThreeState;
import com.jetbrains.php.lang.documentation.phpdoc.lexer.PhpDocTokenTypes;
import com.jetbrains.php.lang.documentation.phpdoc.parser.PhpDocElementTypes;
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment;
import com.jetbrains.php.lang.patterns.PhpPatterns;
import com.jetbrains.php.lang.psi.PhpFile;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import com.jetbrains.php.lang.psi.elements.impl.PhpPsiElementImpl;
import org.jetbrains.annotations.NotNull;


public class PhpAnnotationCompletionConfidence extends CompletionConfidence {
    @NotNull
    @Override
    public ThreeState shouldSkipAutopopup(@NotNull PsiElement contextElement, @NotNull PsiFile psiFile, int offset) {

        if(!(psiFile instanceof PhpFile)) {
            return ThreeState.UNSURE;
        }

        PsiElement context = contextElement.getContext();
        if(context instanceof StringLiteralExpression) {

            // foo="<|>"
            if(PhpPatterns.psiElement(PhpDocElementTypes.phpDocString).accepts(context)) {
                return ThreeState.NO;
            }

        }

        if(context instanceof PhpDocComment) {

            // * <|>
            if(PhpPatterns.psiElement().beforeLeafSkipping(PhpPatterns.psiElement(PhpDocTokenTypes.DOC_LEADING_ASTERISK), PhpPatterns.psiElement(PsiWhiteSpace.class)).accepts(context)) {
                return ThreeState.NO;
            }

        }

        if(context instanceof PhpPsiElementImpl) {

            // @Foo(<|>)
            if(PhpPatterns.psiElement(PhpDocElementTypes.phpDocAttributeList).accepts(context)) {
                return ThreeState.NO;
            }

            // @<|>
            if(PhpPatterns.psiElement(PhpDocElementTypes.phpDocTag).accepts(context)) {
                return ThreeState.NO;
            }

        }

        return ThreeState.UNSURE;
    }

}

@@ -1358,33 +1358,29 @@
 %7D%0A%0A        %7D
-%0A%0A       
+ else
  if(context 
@@ -1473,14 +1473,13 @@
 t().
-before
+after
 Leaf
@@ -1487,16 +1487,33 @@
 kipping(
+%0A                
 PhpPatte
@@ -1532,90 +1532,119 @@
 nt(P
-hpDocTokenTypes.DOC_LEADING_ASTERISK), PhpPatterns.psiElement(PsiWhiteSpace.class)
+siWhiteSpace.class),%0A                PhpPatterns.psiElement(PhpDocTokenTypes.DOC_LEADING_ASTERISK)%0A            
 ).ac
@@ -1648,32 +1648,39 @@
 .accepts(context
+Element
 )) %7B%0A           
@@ -1730,25 +1730,21 @@
        %7D
-%0A%0A       
+ else
  if(cont

fix that docblock completion confidence if valid in comments only