package fr.adrienbrault.idea.symfony2plugin.translation.collector;


import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.yaml.psi.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class YamlTranslationVistor {

    public static void collectFileTranslations(YAMLFile yamlFile, YamlTranslationCollector translationCollector) {
        List<YAMLDocument> documents = yamlFile.getDocuments();

        if(documents.size() < 1) {
            return;
        }

        Collection<YAMLKeyValue> yamlKeyValues = PsiTreeUtil.getChildrenOfTypeAsList(documents.get(0), YAMLKeyValue.class);
        for(YAMLKeyValue yamlKeyValue: yamlKeyValues) {
            collectItems(yamlKeyValue, translationCollector);
        }

    }

    private static void collectNextLevelElements(YAMLCompoundValue yamlCompoundValue, List<String> levels, YamlTranslationCollector translationCollector) {
        Collection<YAMLKeyValue> yamlKeyValues = PsiTreeUtil.getChildrenOfTypeAsList(yamlCompoundValue, YAMLKeyValue.class);
        for(YAMLKeyValue yamlKeyValue: yamlKeyValues) {
            if(!collectItems(levels, yamlKeyValue, translationCollector)) {
                return;
            }
        }
    }

    private static void collectItems(YAMLKeyValue yamlKeyValue, YamlTranslationCollector translationCollector ) {
        collectItems(new ArrayList<String>(), yamlKeyValue, translationCollector);
    }

    private static boolean collectItems(List<String> levels, YAMLKeyValue yamlKeyValue, YamlTranslationCollector translationCollector) {

        List<YAMLPsiElement> childElements = yamlKeyValue.getYAMLElements();
        String keyText = yamlKeyValue.getKeyText();
        if(StringUtils.isBlank(keyText)) {
            return true;
        }

        // we check again after cleanup
        keyText = keyNormalizer(keyText);
        if(StringUtils.isBlank(keyText)) {
            return true;
        }

        // yaml key-value provide main psielement in last child element
        // depending of what we get here we have another key-value inside, multiline or string value
        if(childElements.size() == 1) {


            PsiElement lastChildElement = childElements.get(0);

            // catch next level keys
            if(lastChildElement instanceof YAMLCompoundValue) {

                // use copy of current level and pipe to children call
                ArrayList<String> copyLevels = new ArrayList<String>(levels);
                copyLevels.add(keyText);

                collectNextLevelElements((YAMLCompoundValue) childElements.get(0), copyLevels, translationCollector);
                return true;
            }

            // check multiline is also an tree end indicator
            // stats: |
            // accomplishment: >
            if((lastChildElement instanceof YAMLScalarText || lastChildElement instanceof YAMLScalarList)) {
                return callCollectCallback(levels, yamlKeyValue, translationCollector, keyText);
            }

        } else {
            return callCollectCallback(levels, yamlKeyValue, translationCollector, keyText);
        }

        return true;
    }

    private static boolean callCollectCallback(List<String> levels, YAMLKeyValue yamlKeyValue, YamlTranslationCollector translationCollector, String keyText) {
        ArrayList<String> copyLevels = new ArrayList<String>(levels);
        copyLevels.add(keyText);

        return translationCollector.collect(StringUtils.join(copyLevels, "."), yamlKeyValue);
    }

    /**
     * Translation key allow quoted values and also space before and after
     */
    private static String keyNormalizer(String keyName) {
        return StringUtils.trim(StringUtils.strip(StringUtils.strip(keyName, "'"), "\""));
    }

}

@@ -140,16 +140,81 @@
 eeUtil;%0A
+import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper;%0A
 import o
@@ -249,16 +249,52 @@
 gUtils;%0A
+import org.jetbrains.yaml.YAMLUtil;%0A
 import o
@@ -565,150 +565,12 @@
     
-List%3CYAMLDocument%3E documents = yamlFile.getDocuments();%0A%0A        if(documents.size() %3C 1) %7B%0A            return;%0A        %7D%0A%0A        Collection%3C
+for(
 YAML
@@ -569,33 +569,32 @@
 for(YAMLKeyValue
-%3E
  yamlKeyValues =
@@ -594,115 +594,31 @@
 alue
-s = PsiTreeUtil.getChildrenOfTypeAsList(documents.get(0), YAMLKeyValue.class);%0A        for(YAMLKeyValue yam
+: YamlHelper.getTopLeve
 lKey
@@ -618,39 +618,35 @@
 evelKeyValue
-: 
+s(
 yaml
-KeyValues
+File)
 ) %7B%0A        
@@ -1888,32 +1888,87 @@
 rue;%0A        %7D%0A%0A
+        // @TODO: use features of new yaml integration%0A
         // yaml 
@@ -2023,16 +2023,16 @@
 element%0A
-
         
@@ -2160,16 +2160,63 @@
 e() == 1
+ && childElements.get(0) instanceof YAMLMapping
 ) %7B%0A%0A%0A  
@@ -2362,29 +2362,23 @@
 eof YAML
-CompoundValue
+Mapping
 ) %7B%0A%0A   

support yaml translation keys on new yaml plugin version #626