package io.protostuff.jetbrains.plugin.view.structure;

import com.intellij.ide.util.ActionShortcutProvider;
import com.intellij.ide.util.FileStructureNodeProvider;
import com.intellij.ide.util.treeView.smartTree.ActionPresentation;
import com.intellij.ide.util.treeView.smartTree.ActionPresentationData;
import com.intellij.ide.util.treeView.smartTree.TreeElement;
import com.intellij.openapi.actionSystem.Shortcut;
import com.intellij.psi.PsiElement;
import com.intellij.util.IncorrectOperationException;
import io.protostuff.jetbrains.plugin.Icons;
import io.protostuff.jetbrains.plugin.ProtostuffBundle;
import io.protostuff.jetbrains.plugin.psi.EnumConstantNode;
import io.protostuff.jetbrains.plugin.psi.FieldNode;
import io.protostuff.jetbrains.plugin.psi.RpcMethodNode;
import org.jetbrains.annotations.NotNull;

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

/**
 * @author Kostiantyn Shchepanovskyi
 */
final class ProtoFieldsNodeProvider implements FileStructureNodeProvider<TreeElement>, ActionShortcutProvider {

    private static final String ID = "SHOW_FIELDS";

    @NotNull
    @Override
    public String getCheckBoxText() {
        return ProtostuffBundle.message("file.structure.toggle.show.fields");
    }

    @NotNull
    @Override
    public Shortcut[] getShortcut() {
        throw new IncorrectOperationException("see getActionIdForShortcut()");
    }

    @NotNull
    @Override
    public String getActionIdForShortcut() {
        return "FileStructurePopup";
    }

    @Override
    @NotNull
    public ActionPresentation getPresentation() {
        String message = ProtostuffBundle.message("action.structureview.show.fields");
        return new ActionPresentationData(message, null, Icons.FIELD);
    }

    @Override
    @NotNull
    public String getName() {
        return ID;
    }

    @NotNull
    @Override
    public Collection<TreeElement> provideNodes(@NotNull TreeElement parent) {
        if (parent instanceof AbstractTreeElement) {
            AbstractTreeElement element = (AbstractTreeElement) parent;
            PsiElement psiElement = element.getValue();
            List<TreeElement> treeElements = new ArrayList<>();
            for (PsiElement childBlock : psiElement.getChildren()) {
                // first and the only child
                PsiElement node = childBlock.getFirstChild();
                if (node instanceof FieldNode) {
                    treeElements.add(new MessageFieldTreeElement((FieldNode) node));
                }
                if (node instanceof EnumConstantNode) {
                    treeElements.add(new EnumConstantTreeElement((EnumConstantNode) node));
                }
                if (node instanceof RpcMethodNode) {
                    treeElements.add(new ServiceMethodTreeElement((RpcMethodNode) node));
                }
            }
            return treeElements;
        }
        return Collections.emptyList();
    }

}

@@ -2257,26 +2257,20 @@
 Element 
-childBlock
+node
  : psiEl
@@ -2296,114 +2296,8 @@
 ) %7B%0A
-                // first and the only child%0A                PsiElement node = childBlock.getFirstChild();%0A
     

Fix structure view panel (issue introduced after PSI structure change)