/*
 * Copyright 2014 the original author or authors.
 *
 * 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.gradle.plugins.ide.internal.tooling;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.tooling.internal.impl.LaunchableGradleTask;
import org.gradle.tooling.internal.gradle.DefaultBuildInvocations;
import org.gradle.tooling.internal.impl.LaunchableGradleTaskSelector;
import org.gradle.tooling.model.internal.ProjectSensitiveToolingModelBuilder;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class BuildInvocationsBuilder extends ProjectSensitiveToolingModelBuilder {
    private final GradleProjectBuilder gradleProjectBuilder;

    public BuildInvocationsBuilder(GradleProjectBuilder gradleProjectBuilder) {
        this.gradleProjectBuilder = gradleProjectBuilder;
    }

    public boolean canBuild(String modelName) {
        return modelName.equals("org.gradle.tooling.model.gradle.BuildInvocations");
    }

    public DefaultBuildInvocations buildAll(String modelName, Project project) {
        if (!canBuild(modelName)) {
            throw new GradleException("Unknown model name " + modelName);
        }
        List<LaunchableGradleTaskSelector> selectors = Lists.newArrayList();
        Set<String> aggregatedTasks = findTasks(project);
        for (String selectorName : aggregatedTasks) {
            selectors.add(new LaunchableGradleTaskSelector().
                    setName(selectorName).
                    setTaskName(selectorName).
                    setProjectPath(project.getPath()).
                    setDescription(project.getParent() != null
                            ? String.format("%s:%s task selector", project.getPath(), selectorName)
                            : String.format("%s task selector", selectorName)).
                    setDisplayName(String.format("%s in %s and subprojects.", selectorName, project.toString())));
        }
        return new DefaultBuildInvocations()
                .setSelectors(selectors)
                .setTasks(convertTasks(new ArrayList(gradleProjectBuilder.buildAll(project).findByPath(project.getPath()).getTasks())));
    }

    public DefaultBuildInvocations buildAll(String modelName, Project project, boolean implicitProject) {
        return buildAll(modelName, implicitProject ? project.getRootProject() : project);
    }

    // build tasks without project reference
    private List<LaunchableGradleTask> convertTasks(List<LaunchableGradleTask> tasks) {
        List<LaunchableGradleTask> convertedTasks = Lists.newArrayList();
        for (LaunchableGradleTask task : tasks) {
            convertedTasks.add(new LaunchableGradleTask()
                    .setPath(task.getPath())
                    .setName(task.getName())
                    .setDisplayName(task.toString())
                    .setDescription(task.getDescription()));
        }
        return convertedTasks;
    }

    private Set<String> findTasks(Project project) {
        Set<String> aggregatedTasks = Sets.newHashSet();
        for (Project child : project.getSubprojects()) {
            Set<String> childTasks = findTasks(child);
            aggregatedTasks.addAll(childTasks);
        }
        for (Task task : project.getTasks()) {
            aggregatedTasks.add(task.getName());
        }
        return aggregatedTasks;
    }

}

@@ -876,33 +876,38 @@
 nal.
-impl.LaunchableGradleTask
+gradle.DefaultBuildInvocations
 ;%0Aim
@@ -943,38 +943,33 @@
 nal.
-gradle.DefaultBuildInvocations
+impl.LaunchableGradleTask
 ;%0Aim
@@ -1132,25 +1132,26 @@
 va.util.
-ArrayList
+Collection
 ;%0Aimport
@@ -1646,32 +1646,54 @@
 BuildInvocations
+%3CLaunchableGradleTask%3E
  buildAll(String
@@ -1962,25 +1962,69 @@
 s = 
-findTasks(project
+Sets.newHashSet();%0A        findTasks(project, aggregatedTasks
 );%0A 
@@ -2693,16 +2693,38 @@
 ocations
+%3CLaunchableGradleTask%3E
 ()%0A     
@@ -2802,22 +2802,8 @@
 sks(
-new ArrayList(
 grad
@@ -2875,25 +2875,24 @@
 getTasks()))
-)
 ;%0A    %7D%0A%0A   
@@ -3188,20 +3188,24 @@
 rtTasks(
-List
+Iterable
 %3CLauncha
@@ -3674,27 +3674,20 @@
 private 
-Set%3CString%3E
+void
  findTas
@@ -3708,68 +3708,37 @@
 ject
-) %7B%0A        Set%3CString%3E aggregatedTasks = Sets.newHashSet();
+, Collection%3CString%3E tasks) %7B
 %0A   
@@ -3807,92 +3807,26 @@
     
-Set%3CString%3E childTasks = findTasks(child);%0A            aggregatedTasks.addAll(childT
+findTasks(child, t
 asks
@@ -3897,27 +3897,17 @@
         
-aggregatedT
+t
 asks.add
@@ -3938,40 +3938,8 @@
   %7D%0A
-        return aggregatedTasks;%0A
     

Create less garbage when assembling the selectors.