/*******************************************************************************
 * This file is part of the Symfony eclipse plugin.
 *
 * (c) Robert Gruendler <r.gruendler@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 ******************************************************************************/
package com.dubture.symfony.core.compiler;


import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.dltk.ast.declarations.MethodDeclaration;
import org.eclipse.dltk.ast.declarations.TypeDeclaration;
import org.eclipse.dltk.ast.expressions.Expression;
import org.eclipse.dltk.ast.expressions.StringLiteral;
import org.eclipse.dltk.ast.statements.Statement;
import org.eclipse.dltk.compiler.env.IModuleSource;
import org.eclipse.php.core.compiler.PHPSourceElementRequestorExtension;
import org.eclipse.php.internal.core.Logger;
import org.eclipse.php.internal.core.compiler.ast.nodes.ClassDeclaration;
import org.eclipse.php.internal.core.compiler.ast.nodes.FullyQualifiedReference;
import org.eclipse.php.internal.core.compiler.ast.nodes.PHPMethodDeclaration;

import com.dubture.symfony.core.builder.SymfonyNature;
import com.dubture.symfony.core.model.Service;
import com.dubture.symfony.core.model.SymfonyModelAccess;
import com.dubture.symfony.core.preferences.SymfonyCoreConstants;
import com.dubture.symfony.core.util.PathUtils;


/**
 *
 *
 *
 *
 * @author Robert Gruendler <r.gruendler@gmail.com>
 *
 */
@SuppressWarnings("restriction")
public class SourceElementRequestor extends PHPSourceElementRequestorExtension {

    private ClassDeclaration currentClass;
    private boolean inController;

    private SymfonyModelAccess model = SymfonyModelAccess.getDefault();
    private boolean isSymfonySource;


    @Override
    public void setSourceModule(IModuleSource sourceModule) {

        super.setSourceModule(sourceModule);

        try {
            isSymfonySource = sourceModule.getModelElement().getScriptProject().getProject().exists() && sourceModule.getModelElement().getScriptProject().getProject().getNature(SymfonyNature.NATURE_ID) != null;
        } catch (CoreException e) {
            isSymfonySource = false;
            Logger.logException(e);
        }
    }

    public boolean isInController() {
        return inController;
    }

    @Override
    public boolean visit(TypeDeclaration s) throws Exception {


        if (!isSymfonySource)
            return false;

        if (s instanceof ClassDeclaration) {
            currentClass = (ClassDeclaration) s;
            for (Object o : currentClass.getSuperClasses().getChilds()) {

                if (o instanceof FullyQualifiedReference) {

                    FullyQualifiedReference superReference = (FullyQualifiedReference) o;

                    //TODO: find a way to check against the FQCN
                    // via the UseStatement
                    if (/*superReference.getNamespace().equals(SymfonyCoreConstants.CONTROLLER_NS)
                            && */superReference.getName().equals(SymfonyCoreConstants.CONTROLLER_CLASS)) {

                        // the ControllerIndexer does the actual work of parsing the
                        // the relevant elements inside the controller
                        // which are then being collected in the endVisit() method
                        inController = true;

                    }
                }
            }
        }

        return true;
    }

    @Override
    public boolean visit(Statement st) throws Exception {

        return true;
    }


    @Override
    public boolean visit(Expression st) throws Exception {

        if (!isSymfonySource)
            return false;


        if (st instanceof StringLiteral) {

            StringLiteral literal = (StringLiteral) st;
            String literalValue = literal.getValue().replaceAll("['\"]", "");

            if (PathUtils.isViewPath(literalValue)) {
                //TODO: report viewpath reference
            }

            IPath path = getSourceModule().getModelElement().getScriptProject().getPath();
            Service service = model.findService(literalValue, path);

            if (service != null) {
                fRequestor.acceptTypeReference(service.getFullyQualifiedName(), literal.sourceStart());
            }
        }

        return true;
    }


    @Override
    public boolean visit(MethodDeclaration s) throws Exception {



        if (s instanceof PHPMethodDeclaration) {




        }


        return true;
    }




    @Override
    public boolean endvisit(TypeDeclaration s) throws Exception {

        if (currentClass != null) {


        }

        inController = false;

        currentClass = null;
        return true;
    }
}

@@ -685,24 +685,86 @@
 Expression;%0A
+import org.eclipse.dltk.ast.expressions.MethodCallExpression;%0A
 import org.e
@@ -3844,25 +3844,24 @@
 urn false;%0A%0A
-%0A
         if (
@@ -4138,32 +4138,34 @@
 %0A            %7D%0A%0A
+//
             IPat
@@ -4235,24 +4235,26 @@
 .getPath();%0A
+//
             
@@ -4310,17 +4310,18 @@
  path);%0A
-%0A
+//
         
@@ -4343,24 +4343,26 @@
  != null) %7B%0A
+//
             
@@ -4453,16 +4453,18 @@
 art());%0A
+//
         

Stop reporting references inside SourceElementRequestor