/**
 * Copyright 2015-2016 Red Hat, Inc, and individual contributors.
 *
 * 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 org.wildfly.swarm.plugin.gradle;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Collectors;

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.DependencySet;
import org.gradle.api.artifacts.ResolvedDependency;
import org.gradle.api.artifacts.repositories.MavenArtifactRepository;
import org.gradle.api.internal.artifacts.DefaultExcludeRule;
import org.gradle.api.internal.artifacts.dependencies.DefaultDependencyArtifact;
import org.gradle.api.internal.artifacts.dependencies.DefaultExternalModuleDependency;
import org.wildfly.swarm.tools.ArtifactResolvingHelper;
import org.wildfly.swarm.tools.ArtifactSpec;

/**
 * @author Bob McWhirter
 */
public class GradleArtifactResolvingHelper implements ArtifactResolvingHelper {


    private final Project project;

    public GradleArtifactResolvingHelper(Project project) {
        this.project = project;
        this.project.getRepositories().maven(new Action<MavenArtifactRepository>() {
            @Override
            public void execute(MavenArtifactRepository repo) {
                repo.setName("jboss-public");
                repo.setUrl("http://repository.jboss.org/nexus/content/groups/public/");
            }
        });
    }


    @Override
    public ArtifactSpec resolve(final ArtifactSpec spec) {
        if (spec.file != null) {
            return spec;
        }

        final Iterator<ResolvedDependency> iterator =
                doResolve(new HashSet<>(Collections.singletonList(spec))).iterator();
        if (iterator.hasNext()) {
            spec.file = iterator.next()
                    .getModuleArtifacts()
                    .iterator().next()
                    .getFile();

            return spec;
        }

        return null;
    }

    @Override
    public Set<ArtifactSpec> resolveAll(final Set<ArtifactSpec> specs) throws Exception {
        if (specs.isEmpty()) {

            return specs;
        }

        final Set<ArtifactSpec> resolvedSpecs = new HashSet<>();

        doResolve(specs).forEach(dep -> dep.getAllModuleArtifacts()
                .forEach(artifact -> resolvedSpecs
                        .add(new ArtifactSpec(dep.getConfiguration(),
                                              dep.getModuleGroup(),
                                              artifact.getName(),
                                              dep.getModuleVersion(),
                                              artifact.getExtension(),
                                              artifact.getClassifier(),
                                              artifact.getFile()))));

        return resolvedSpecs.stream()
                .filter(a -> !"system".equals(a.scope))
                .collect(Collectors.toSet());
    }

    private Set<ResolvedDependency> doResolve(final Collection<ArtifactSpec> deps) {
        final Configuration config = this.project.getConfigurations().detachedConfiguration();
        final DependencySet dependencySet = config.getDependencies();

        deps.forEach(spec -> {
            final DefaultExternalModuleDependency d =
                    new DefaultExternalModuleDependency(spec.groupId(), spec.artifactId(), spec.version());
            final DefaultDependencyArtifact da =
                    new DefaultDependencyArtifact(spec.artifactId(), spec.type(), spec.type(), spec.classifier(), null);
            d.addArtifact(da);
            d.getExcludeRules().add(new DefaultExcludeRule());
            dependencySet.add(d);
        });

        return config.getResolvedConfiguration().getFirstLevelModuleDependencies();
    }

}

@@ -775,16 +775,38 @@
 erator;%0A
+import java.util.Map;%0A
 import j
@@ -815,24 +815,24 @@
 a.util.Set;%0A
-
 import java.
@@ -1354,32 +1354,237 @@
 duleDependency;%0A
+import org.gradle.api.internal.artifacts.dependencies.DefaultProjectDependency;%0Aimport org.gradle.api.internal.project.DefaultProjectAccessListener;%0Aimport org.gradle.api.internal.project.ProjectInternal;%0A
 import org.wildf
@@ -1818,16 +1818,51 @@
 project;
+%0A    Map%3CString, Project%3E projects;
 %0A%0A    pu
@@ -1943,24 +1943,196 @@
  = project;%0A
+        this.projects = project.getRootProject().getAllprojects().stream().collect(Collectors.toMap(p -%3E p.getGroup() + %22:%22 + p.getName() + %22:%22 + p.getVersion(), p -%3E p));%0A
         this
@@ -3279,19 +3279,16 @@
  dep.get
-All
 ModuleAr
@@ -4251,26 +4251,419 @@
 eps.
-forEach(spec -%3E %7B%0A
+stream()%0A                .forEach(spec -%3E %7B%0A                    if (projects.containsKey(spec.groupId() + %22:%22 + spec.artifactId() + %22:%22 + spec.version())) %7B%0A                        dependencySet.add(new DefaultProjectDependency((ProjectInternal) projects.get(spec.groupId() + %22:%22 + spec.artifactId() + %22:%22 + spec.version()), new DefaultProjectAccessListener(), false));%0A                    %7D else %7B%0A            
     
@@ -4724,32 +4724,44 @@
                 
+          
+  
 new DefaultExter
@@ -4828,24 +4828,36 @@
 version());%0A
+            
             
@@ -4905,32 +4905,44 @@
                 
+  
+          
 new DefaultDepen
@@ -5038,16 +5038,28 @@
         
+     
+       
 d.addArt
@@ -5069,16 +5069,28 @@
 ct(da);%0A
+            
         
@@ -5140,24 +5140,36 @@
 udeRule());%0A
+            
             
@@ -5190,16 +5190,46 @@
 add(d);%0A
+                    %7D%0A        
         

Add multi-project support to GradleArtifactResolvingHelper