/*
 * 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.psi.impl.statements;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.ResolveState;
import com.intellij.psi.scope.PsiScopeProcessor;
import com.intellij.psi.tree.IElementType;
import com.sylvanaar.idea.Lua.lang.lexer.LuaTokenTypes;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaDeclarationExpression;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaExpressionList;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaIdentifierList;
import com.sylvanaar.idea.Lua.lang.psi.expressions.LuaReferenceExpression;
import com.sylvanaar.idea.Lua.lang.psi.statements.LuaAssignmentStatement;
import com.sylvanaar.idea.Lua.lang.psi.statements.LuaLocalDefinitionStatement;
import com.sylvanaar.idea.Lua.lang.psi.statements.LuaStatementElement;
import com.sylvanaar.idea.Lua.lang.psi.visitor.LuaElementVisitor;
import org.jetbrains.annotations.NotNull;

/**
 * Created by IntelliJ IDEA.
 * User: Jon S Akhtar
 * Date: Sep 6, 2010
 * Time: 10:00:19 AM
 */
public class LuaLocalDefinitionStatementImpl extends LuaStatementElementImpl implements LuaLocalDefinitionStatement, LuaStatementElement, LuaAssignmentStatement {
    public LuaLocalDefinitionStatementImpl(ASTNode node) {
        super(node);
    }

    @Override
    public void accept(LuaElementVisitor visitor) {
        visitor.visitDeclarationStatement(this);
        //visitor.visitAssignment(this);
    }

    @Override
    public void accept(@NotNull PsiElementVisitor visitor) {
        if (visitor instanceof LuaElementVisitor) {
            ((LuaElementVisitor) visitor).visitDeclarationStatement(this);
            //((LuaElementVisitor) visitor).visitAssignment(this);
        } else {
            visitor.visitElement(this);
        }
    }


    @Override
    public LuaDeclarationExpression[] getDeclarations() {
        return findChildByClass(LuaIdentifierList.class).getDeclarations();
    }

    @Override
    public LuaReferenceExpression[] getReferenceExprs() {
        return findChildByClass(LuaIdentifierList.class).getReferenceExprs();  // TODO
    }

    @Override
    public LuaStatementElement replaceWithStatement(LuaStatementElement newCall) {
        return null;
    }

        // locals are undefined within the statement, so  local a,b = b,a
        // should not resolve a to a or b to b. So to handle this we process
        // our declarations unless we are walking from a child of ourself.
        // in our case its, (localstat) <- (expr list) <- (expression) <- (variable) <- (reference )

    public boolean processDeclarations(@NotNull PsiScopeProcessor processor,
                                       @NotNull ResolveState resolveState,
                                       PsiElement lastParent,
                                       @NotNull PsiElement place) {

        if (place.getParent().getParent().getParent().getParent() != this) {
            final LuaDeclarationExpression[] decls = getDeclarations();
            for (LuaDeclarationExpression decl : decls) {
                if (!processor.execute(decl, resolveState)) return false;
            }
        }
        return true;
    }

    @Override
    public LuaIdentifierList getLeftExprs() {
        return findChildByClass(LuaIdentifierList.class);
    }

    @Override
    public LuaExpressionList getRightExprs() {
        return findChildByClass(LuaExpressionList.class);
    }

    @Override
    public IElementType getOperationTokenType() {
        return findChildByType(LuaTokenTypes.ASSIGN) == null ? null : LuaTokenTypes.ASSIGN;
    }

    @Override
    public LuaIdentifierList getDefinedNames() {
        return findChildByClass(LuaIdentifierList.class);
    }
}

@@ -1041,262 +1041,73 @@
 psi.
-expressions.LuaDeclarationExpression;%0D%0Aimport com.sylvanaar.idea.Lua.lang.psi.expressions.LuaExpressionList;%0D%0Aimport com.sylvanaar.idea.Lua.lang.psi.expressions.LuaIdentifierList;%0D%0Aimport com.sylvanaar.idea.Lua.lang.psi.expressions.LuaReferenceExpression
+LuaPsiFile;%0D%0Aimport com.sylvanaar.idea.Lua.lang.psi.expressions.*
 ;%0D%0Ai
@@ -3424,65 +3424,121 @@
     
-if (place.getParent().getParent().getParent().getP
+PsiElement parent = place.getParent();%0D%0A        while (!(parent instanceof LuaPsiFile)) %7B%0D%0A            if (p
 arent
-()
  != 
@@ -3542,24 +3542,28 @@
 != this) %7B%0D%0A
+    
             
@@ -3639,13 +3639,78 @@
     
+    
 for (
+int i = decls.length-1; i %3E= 0 ; i--) %7B%0D%0A                    
 LuaD
@@ -3739,20 +3739,25 @@
 ecl 
-:
+=
  decls
-) %7B%0D%0A
+%5Bi%5D;%0D%0A    
     
@@ -3831,35 +3831,199 @@
 e;%0D%0A            
-%7D%0D%0A
+    %7D%0D%0A            %7D%0D%0A%0D%0A            parent = parent.getParent();%0D%0A        %7D%0D%0A%0D%0A%0D%0A//        if (place.getParent().getParent().getParent().getParent() != this) %7B%0D%0A//%0D%0A//
         %7D%0D%0A     

fix local foo, foo = 1, 2; print(foo)