/*******************************************************************************
 * 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.codeassist.strategies;

import java.util.List;

import org.eclipse.dltk.core.IScriptProject;
import org.eclipse.dltk.core.IType;
import org.eclipse.dltk.internal.core.ModelElement;
import org.eclipse.dltk.internal.core.SourceRange;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.php.core.codeassist.ICompletionContext;
import org.eclipse.php.internal.core.codeassist.CodeAssistUtils;
import org.eclipse.php.internal.core.codeassist.ICompletionReporter;
import org.eclipse.php.internal.core.codeassist.strategies.MethodParameterKeywordStrategy;

import com.dubture.symfony.core.codeassist.contexts.ServiceContainerContext;
import com.dubture.symfony.core.model.Service;
import com.dubture.symfony.core.model.SymfonyModelAccess;


/**
 * CompletionStrategy to provide service names for DI::get calls like
 *
 *
 * <pre>
 *
 *  // inside a ContainerAware interface
 *     $this->get('|
 *  $this->container->get('|
 *
 * </pre>
 *
 *
 *
 * @author "Robert Gruendler <r.gruendler@gmail.com>"
 *
 */
@SuppressWarnings({ "restriction", "deprecation" })
public class ServiceContainerCompletionStrategy extends
        MethodParameterKeywordStrategy {

    public ServiceContainerCompletionStrategy(ICompletionContext context) {
        super(context);

    }

    @Override
    public void apply(ICompletionReporter reporter) throws BadLocationException {

        ServiceContainerContext context = (ServiceContainerContext) getContext();
        IScriptProject project = context.getSourceModule().getScriptProject();

        SymfonyModelAccess model= SymfonyModelAccess.getDefault();
        List<Service> services = model.findServices(project.getPath());
        SourceRange range = getReplacementRange(context);

        String prefix = context.getPrefix();

        if (services == null) {
            return;
        }

        for(Service service : services) {
            if (CodeAssistUtils.startsWithIgnoreCase(service.getId(), prefix)) {
            	
                IType[] serviceTypes = model.findServiceTypes(service, project);
                ModelElement parent = null;
                
                if (serviceTypes.length > 0) {
                    parent = (ModelElement) serviceTypes[0];
                } else {
                    parent = (ModelElement) context.getSourceModule();
                }

                Service s = new Service(parent, service.getElementName());
                s.setId(service.getId());
                reporter.reportType(s, "", range);
            }
        }
    }
}

@@ -988,16 +988,58 @@
 trategy;
+%0Aimport org.pdtextensions.core.log.Logger;
 %0A%0Aimport
@@ -2238,25 +2238,24 @@
 etPrefix();%0A
-%0A
         if (
@@ -2444,304 +2444,24 @@
     
-%09%0A                IType%5B%5D serviceTypes = model.findServiceTypes(service, project);%0A                ModelElement parent = null;%0A                %0A                if (serviceTypes.length %3E 0) %7B%0A                    parent = (ModelElement) serviceTypes%5B0%5D;%0A                %7D else %7B%0A                   
+    ModelElement
  par
@@ -2512,27 +2512,8 @@
 ();%0A
-                %7D%0A%0A
     

fix #189