/*
 * Copyright 2010 Jon S Akhtar (Sylvanaar)
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.sylvanaar.idea.Lua.lang;

import com.intellij.lang.ASTNode;
import com.intellij.lang.folding.FoldingBuilder;
import com.intellij.lang.folding.FoldingDescriptor;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.sylvanaar.idea.Lua.lang.parser.LuaElementTypes;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaTableConstructor;
import com.sylvanaar.idea.Lua.lang.psi.statements.LuaFunctionDefinitionStatement;
import org.jetbrains.annotations.NotNull;

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

import static com.sylvanaar.idea.Lua.lang.lexer.LuaTokenTypes.LONGCOMMENT;

/**
 * Created by IntelliJ IDEA.
 * User: Jon S Akhtar
 * Date: Apr 10, 2010
 * Time: 2:54:53 PM
 */
public class LuaFoldingBuilder implements FoldingBuilder, DumbAware {
    @NotNull
    @Override
    public FoldingDescriptor[] buildFoldRegions(@NotNull ASTNode node, @NotNull Document document) {
        final List<FoldingDescriptor> descriptors = new ArrayList<FoldingDescriptor>();
        final ASTNode fnode = node;
        final Document fdoc = document;
        ApplicationManager.getApplication().runReadAction(new Runnable() {
            @Override
            public void run() {
               appendDescriptors(fnode, fdoc, descriptors);
            }
        });

        return descriptors.toArray(new FoldingDescriptor[descriptors.size()]);
    }


    private void appendDescriptors(final ASTNode node, final Document document, final List<FoldingDescriptor> descriptors) {
        ProgressManager.checkCanceled();
        
        try {
            if (isFoldableNode(node)) {
                final PsiElement psiElement = node.getPsi();

                if (psiElement instanceof LuaFunctionDefinitionStatement) {
                    LuaFunctionDefinitionStatement stmt = (LuaFunctionDefinitionStatement) psiElement;

                    if (stmt.getText().indexOf('\n')>0 && stmt.getTextLength()>3)
                    descriptors.add(new FoldingDescriptor(node,
                            new TextRange(stmt.getParameters().getTextRange().getEndOffset() + 1,
                                    node.getTextRange().getEndOffset())));
                }

                if (psiElement instanceof LuaTableConstructor) {
                    LuaTableConstructor stmt = (LuaTableConstructor) psiElement;

                    if (stmt.getText().indexOf('\n')>0 && stmt.getTextLength()>3)
                        descriptors.add(new FoldingDescriptor(node,
                                new TextRange(stmt.getTextRange().getStartOffset() + 1,
                                        node.getTextRange().getEndOffset() - 1)));
                }
            }

            if (node.getElementType() == LONGCOMMENT && node.getTextLength() > 2) {
                descriptors.add(new FoldingDescriptor(node, node.getTextRange()));
            }

            ASTNode child = node.getFirstChildNode();
            while (child != null) {
                appendDescriptors(child, document, descriptors);
                child = child.getTreeNext();
            }
        } catch (Exception ignored) {
        }
    }

    private boolean isFoldableNode(ASTNode node) {
        return node.getElementType() == LuaElementTypes.FUNCTION_DEFINITION ||
                node.getElementType() == LuaElementTypes.TABLE_CONSTUCTOR;
    }

    @Override
    public String getPlaceholderText(@NotNull ASTNode node) {
        if (node.getElementType() == LONGCOMMENT)
            return "comment";


        return "...";
    }

    @Override
    public boolean isCollapsedByDefault(@NotNull ASTNode node) {
        return false;
    }
}

@@ -1162,16 +1162,163 @@
 Types;%0D%0A
+import com.sylvanaar.idea.Lua.lang.psi.LuaFunctionDefinition;%0D%0Aimport com.sylvanaar.idea.Lua.lang.psi.expressions.LuaAnonymousFunctionExpression;%0D%0A
 import c
@@ -1378,24 +1378,24 @@
 nstructor;%0D%0A
-
 import com.s
@@ -2794,25 +2794,16 @@
 finition
-Statement
 ) %7B%0D%0A   
@@ -2840,25 +2840,16 @@
 finition
-Statement
  stmt = 
@@ -2870,25 +2870,16 @@
 finition
-Statement
 ) psiEle
@@ -4250,32 +4250,32 @@
 STNode node) %7B%0D%0A
-
         return n
@@ -4336,16 +4336,184 @@
 ITION %7C%7C
+ %0D%0A                node.getElementType() == LuaElementTypes.LOCAL_FUNCTION %7C%7C%0D%0A                node.getElementType() == LuaElementTypes.ANONYMOUS_FUNCTION_EXPRESSION %7C%7C
 %0D%0A      

allow all function declarations to fold ( fix #34 )