package bndtools.launch;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

import org.bndtools.api.ILogger;
import org.bndtools.api.Logger;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.sourcelookup.ISourceContainer;
import org.eclipse.debug.core.sourcelookup.ISourceContainerType;
import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
import org.eclipse.debug.core.sourcelookup.containers.CompositeSourceContainer;
import org.eclipse.debug.core.sourcelookup.containers.DefaultSourceContainer;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.sourcelookup.containers.JavaProjectSourceContainer;

import aQute.bnd.build.Container;
import aQute.bnd.build.Container.TYPE;
import aQute.bnd.build.Project;
import bndtools.launch.util.LaunchUtils;

public class BndDependencySourceContainer extends CompositeSourceContainer {
    private static final ILogger logger = Logger.getLogger(BndDependencySourceContainer.class);

    public static final String TYPE_ID = "org.bndtools.core.launch.sourceContainerTypes.bndDependencies";

    @Override
    public boolean equals(Object obj) {
        return obj instanceof DefaultSourceContainer;
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }

    protected ILaunchConfiguration getLaunchConfiguration() {
        ISourceLookupDirector director = getDirector();
        if (director != null) {
            return director.getLaunchConfiguration();
        }
        return null;
    }

    public String getName() {
        return "Bnd Dependencies";
    }

    public ISourceContainerType getType() {
        return getSourceContainerType(TYPE_ID);
    }

    @Override
    protected ISourceContainer[] createSourceContainers() throws CoreException {
        List<ISourceContainer> result = new LinkedList<ISourceContainer>();

        ILaunchConfiguration config = getLaunchConfiguration();
        Project project = LaunchUtils.getBndProject(config);
        if (project != null) {
            try {
                Collection<Container> runbundles = project.getRunbundles();
                for (Container runbundle : runbundles) {
                    if (runbundle.getType() == TYPE.PROJECT) {
                        String targetProjName = runbundle.getProject().getName();
                        IProject targetProj = ResourcesPlugin.getWorkspace().getRoot().getProject(targetProjName);
                        if (targetProj != null) {
                            IJavaProject targetJavaProj = JavaCore.create(targetProj);
                            result.add(new JavaProjectSourceContainer(targetJavaProj));
                        }
                    }
                }
            } catch (Exception e) {
                logger.logError("Error querying Bnd Dependency source containers.", e);
            }
        }

        return result.toArray(new ISourceContainer[result.size()]);
    }

}

@@ -158,32 +158,73 @@
 ols.api.Logger;%0A
+import org.eclipse.core.resources.IFile;%0A
 import org.eclip
@@ -243,32 +243,32 @@
 urces.IProject;%0A
-
 import org.eclip
@@ -345,24 +345,63 @@
 eException;%0A
+import org.eclipse.core.runtime.IPath;%0A
 import org.e
@@ -632,16 +632,94 @@
 rector;%0A
+import org.eclipse.debug.core.sourcelookup.containers.ArchiveSourceContainer;%0A
 import o
@@ -1107,16 +1107,16 @@
 r.TYPE;%0A
-
 import a
@@ -1135,24 +1135,57 @@
 ld.Project;%0A
+import bndtools.central.Central;%0A
 import bndto
@@ -3092,32 +3092,449 @@
               %7D%0A
+                    %7D else if (runbundle.getType() == TYPE.REPO) %7B%0A                        IPath bundlePath = Central.toPath(runbundle.getFile());%0A                        IFile bundleFile = ResourcesPlugin.getWorkspace().getRoot().getFile(bundlePath);%0A                        ArchiveSourceContainer tempArchiveCont = new ArchiveSourceContainer(bundleFile, false);%0A                        result.add(tempArchiveCont);%0A
                 
@@ -3531,32 +3531,32 @@
               %7D%0A
-
                 
@@ -3774,17 +3774,16 @@
 ze()%5D);%0A
+
     %7D%0A
-%0A
 %7D%0A

create source containers for bundles with attached sources