package org.metaborg.core.build.paths;

import java.util.Collection;

import javax.annotation.Nullable;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.metaborg.core.MetaborgException;
import org.metaborg.core.MetaborgRuntimeException;
import org.metaborg.core.build.dependency.IDependencyService;
import org.metaborg.core.language.FacetContribution;
import org.metaborg.core.language.ILanguageComponent;
import org.metaborg.core.language.LanguagePathFacet;
import org.metaborg.core.project.IProject;

import com.google.common.collect.Lists;
import com.google.inject.Inject;

public class DependencyPathProvider implements ILanguagePathProvider {
    private final IDependencyService dependencyService;


    @Inject public DependencyPathProvider(IDependencyService dependencyService) {
        this.dependencyService = dependencyService;
    }


    @Override public Iterable<FileObject> sourcePaths(IProject project, String languageName) throws MetaborgException {
        final Iterable<ILanguageComponent> dependencies = dependencyService.compileDependencies(project);
        final Collection<FileObject> sources = Lists.newArrayList();
        for(ILanguageComponent dependency : dependencies) {
            final Iterable<LanguagePathFacet> facets = dependency.facets(LanguagePathFacet.class);
            for(LanguagePathFacet facet : facets) {
                final Collection<String> paths = facet.sources.get(languageName);
                if(paths != null) {
                    resolve(project.location(), paths, sources);
                }
            }
        }
        return sources;
    }

    @Override public Iterable<FileObject> includePaths(IProject project, String languageName) throws MetaborgException {
        final Iterable<ILanguageComponent> dependencies = dependencyService.runtimeDependencies(project);
        final Collection<FileObject> includes = Lists.newArrayList();
        for(ILanguageComponent dependency : dependencies) {
            final Iterable<FacetContribution<LanguagePathFacet>> facets =
                dependency.facetContributions(LanguagePathFacet.class);
            for(FacetContribution<LanguagePathFacet> facetContribution : facets) {
                final Collection<String> paths = facetContribution.facet.includes.get(languageName);
                if(paths != null) {
                    resolve(facetContribution.contributor.location(), paths, includes);
                }
            }

            final Iterable<ILanguageComponent> transitiveDeps = dependencyService.compileDependencies(dependency);
            for(ILanguageComponent transitiveDep : transitiveDeps) {
                final Iterable<FacetContribution<LanguagePathFacet>> transFacets =
                    transitiveDep.facetContributions(LanguagePathFacet.class);
                for(FacetContribution<LanguagePathFacet> facetContribution : transFacets) {
                    final Collection<String> paths = facetContribution.facet.sources.get(languageName);
                    if(paths != null) {
                        resolve(dependency.location(), paths, includes);
                    }
                }
            }
        }


        return includes;
    }


    private void resolve(FileObject basedir, @Nullable Collection<String> paths, Collection<FileObject> filesToAppend) {
        if(paths != null) {
            for(String path : paths) {
                try {
                    filesToAppend.add(basedir.resolveFile(path));
                } catch(FileSystemException ex) {
                    throw new MetaborgRuntimeException(ex);
                }
            }
        }
    }
}

@@ -2508,32 +2508,35 @@
   %7D%0A%0A           
+ //
  final Iterable%3C
@@ -2630,24 +2630,27 @@
 %0A           
+ //
  for(ILangua
@@ -2699,35 +2699,34 @@
 ) %7B%0A            
-   
+//
  final Iterable%3C
@@ -2789,23 +2789,18 @@
         
-       
+//
  transit
@@ -2855,35 +2855,34 @@
 s);%0A            
-   
+//
  for(FacetContri
@@ -2946,39 +2946,34 @@
 ) %7B%0A            
-   
-    
+//
  final Collectio
@@ -3045,39 +3045,34 @@
 e);%0A            
-       
+//
  if(paths != nul
@@ -3084,35 +3084,26 @@
             
-           
+//
  resolve(dep
@@ -3144,39 +3144,34 @@
 s);%0A            
-       
+//
  %7D%0A             
@@ -3161,35 +3161,34 @@
 / %7D%0A            
-   
+//
  %7D%0A            %7D
@@ -3177,32 +3177,35 @@
 // %7D%0A           
+ //
  %7D%0A        %7D%0A%0A%0A 

Disable transitive dependency paths again, Maven still does not cooperate.