package com.tang.intellij.lua.editor.structure;

import com.intellij.ide.structureView.StructureViewTreeElement;
import com.intellij.ide.util.treeView.smartTree.TreeElement;
import com.intellij.navigation.ItemPresentation;
import com.intellij.psi.PsiElement;
import com.tang.intellij.lua.comment.psi.LuaDocClassDef;
import com.tang.intellij.lua.comment.psi.LuaDocFieldDef;
import com.tang.intellij.lua.comment.psi.LuaDocGlobalDef;
import com.tang.intellij.lua.comment.psi.LuaDocVisitor;
import com.tang.intellij.lua.lang.LuaIcons;
import com.tang.intellij.lua.psi.LuaFile;
import com.tang.intellij.lua.psi.LuaGlobalFuncDef;
import com.tang.intellij.lua.psi.LuaTypes;
import com.tang.intellij.lua.psi.LuaVisitor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * Created by TangZX on 2016/12/13.
 */
public class LuaFileTreeElement implements StructureViewTreeElement {

    private LuaFile file;

    public LuaFileTreeElement(LuaFile file) {

        this.file = file;
    }

    @Override
    public Object getValue() {
        return file;
    }

    @Override
    public void navigate(boolean b) {

    }

    @Override
    public boolean canNavigate() {
        return false;
    }

    @Override
    public boolean canNavigateToSource() {
        return false;
    }

    @NotNull
    @Override
    public ItemPresentation getPresentation() {
        return new ItemPresentation() {
            @Nullable
            @Override
            public String getPresentableText() {
                return file.getName();
            }

            @Nullable
            @Override
            public String getLocationString() {
                return file.getName();
            }

            @Nullable
            @Override
            public Icon getIcon(boolean b) {
                return LuaIcons.FILE;
            }
        };
    }

    @NotNull
    @Override
    public TreeElement[] getChildren() {
        List<TreeElement> list = new ArrayList<>();

        file.acceptChildren(new LuaVisitor(){

            public void visitDocComment(PsiElement comment) {
                comment.acceptChildren(new LuaDocVisitor(){
                    @Override
                    public void visitClassDef(@NotNull LuaDocClassDef o) {
                        String name = o.getClassNameText();
                        if (name != null) {
                            list.add(new LuaClassTreeElement(o));
                        }
                    }

                    @Override
                    public void visitGlobalDef(@NotNull LuaDocGlobalDef o) {

                    }

                    @Override
                    public void visitFieldDef(@NotNull LuaDocFieldDef o) {

                    }
                });
            }

            @Override
            public void visitGlobalFuncDef(@NotNull LuaGlobalFuncDef o) {
                list.add(new LuaGlobalFunctionTreeElement(o));
            }

            @Override
            public void visitElement(PsiElement element) {
                if (element.getNode().getElementType() == LuaTypes.DOC_COMMENT) {
                    visitDocComment(element);
                }
            }
        });

        return list.toArray(new TreeElement[list.size()]);
    }
}
@@ -513,114 +513,34 @@
 lua.
-lang.LuaIcons;%0Aimport com.tang.intellij.lua.psi.LuaFile;%0Aimport com.tang.intellij.lua.psi.LuaGlobalFuncDef
+comment.psi.api.LuaComment
 ;%0Aim
@@ -570,19 +570,20 @@
 lua.
-psi.LuaType
+lang.LuaIcon
 s;%0Ai
@@ -614,26 +614,17 @@
 lua.psi.
-LuaVisitor
+*
 ;%0Aimport
@@ -2038,39 +2038,32 @@
 )%7B%0A%0A            
-public 
 void visitDocCom
@@ -3053,63 +3053,216 @@
 ment
-.getNode().getElementType() == LuaTypes.DOC_COMMENT) %7B%0A
+ instanceof LuaCommentOwner) %7B%0A                    LuaCommentOwner owner = (LuaCommentOwner) element;%0A                    LuaComment comment = owner.getComment();%0A                    if (comment != null)%0A    
     
@@ -3293,19 +3293,19 @@
 Comment(
-ele
+com
 ment);%0A 

structure view