package com.cedricziel.idea.typo3.tca.codeInsight;

import com.cedricziel.idea.typo3.psi.PhpElementsUtil;
import com.cedricziel.idea.typo3.util.TableUtil;
import com.intellij.codeInsight.completion.*;
import com.intellij.psi.PsiElement;
import com.intellij.util.ProcessingContext;
import com.jetbrains.php.lang.psi.elements.ArrayHashElement;
import com.jetbrains.php.lang.psi.elements.PhpPsiElement;
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;

import static com.cedricziel.idea.typo3.util.TableUtil.TCA_TABLE_FIELDS;

public class TCACompletionContributor extends CompletionContributor {

    public TCACompletionContributor() {
        /*
         * Complete table names in array values.
         */
        extend(
                CompletionType.BASIC,
                PhpElementsUtil.isStringArrayValue(),
                new CompletionProvider<CompletionParameters>() {
                    @Override
                    protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
                        PsiElement element = parameters.getPosition();

                        // ArrayHashElement
                        PsiElement arrayElement = element.getParent().getParent().getParent();
                        if (arrayElement instanceof ArrayHashElement) {
                            ArrayHashElement arrayHashElement = (ArrayHashElement) arrayElement;
                            PhpPsiElement keyPsiElement = arrayHashElement.getKey();
                            if (keyPsiElement instanceof StringLiteralExpression) {
                                String key = ((StringLiteralExpression) keyPsiElement).getContents();

                                if (Arrays.asList(TCA_TABLE_FIELDS).contains(key)) {
                                    TableUtil.completeAvailableTableNames(element.getProject(), result);
                                }
                            }
                        }
                    }
                }
        );
    }
}

@@ -278,195 +278,8 @@
 xt;%0A
-import com.jetbrains.php.lang.psi.elements.ArrayHashElement;%0Aimport com.jetbrains.php.lang.psi.elements.PhpPsiElement;%0Aimport com.jetbrains.php.lang.psi.elements.StringLiteralExpression;%0A
 impo
@@ -321,34 +321,8 @@
 l;%0A%0A
-import java.util.Arrays;%0A%0A
 impo
@@ -367,33 +367,44 @@
 il.T
-able
+CA
 Util.
-TCA_TABLE_FIELDS
+arrayIndexIsTCATableNameField
 ;%0A%0Ap
@@ -1021,682 +1021,78 @@
 ();%0A
-%0A                        // ArrayHashElement%0A                        PsiElement arrayElement = element.getParent().getParent().getParent();%0A                        if (arrayElement instanceof ArrayHashElement) %7B%0A                            ArrayHashElement arrayHashElement = (ArrayHashElement) arrayElement;%0A                            PhpPsiElement keyPsiElement = arrayHashElement.getKey();%0A                            if (keyPsiElement instanceof StringLiteralExpression) %7B%0A                                String key = ((StringLiteralExpression) keyPsiElement).getContents();%0A%0A                                if (Arrays.asList(TCA_TABLE_FIELDS).contains(key)) %7B%0A        
+                        if (arrayIndexIsTCATableNameField(element)) %7B%0A
     
@@ -1188,72 +1188,8 @@
 t);%0A
-                                %7D%0A                            %7D%0A
     

Refactor TCACompletionContributor to use external logic to extract information about the context