/*
 * 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.Maps;
import com.google.common.collect.Sets;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.internal.project.ProjectTaskLister;
import org.gradle.tooling.internal.impl.DefaultBuildInvocations;
import org.gradle.tooling.internal.impl.LaunchableGradleTask;
import org.gradle.tooling.internal.impl.LaunchableGradleTaskSelector;
import org.gradle.tooling.model.internal.ProjectSensitiveToolingModelBuilder;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class BuildInvocationsBuilder extends ProjectSensitiveToolingModelBuilder {

    private static class TaskSelectorBuilder {
        private String selectorName;
        private String description;

        public TaskSelectorBuilder(String selectorName, String description) {
            this.selectorName = selectorName;
            this.description = description;
        }

        public void addDescription(String description) {
            if (description != null) {
                this.description = description;
            }
        }

        LaunchableGradleTaskSelector build(Project project, Set<String> visibleTasks) {
            String desc = description != null
                    ? description
                    : (project.getParent() != null
                        ? String.format("%s:%s task selector", project.getPath(), selectorName)
                        : String.format("%s task selector", selectorName));
            return new LaunchableGradleTaskSelector().
                    setName(selectorName).
                    setTaskName(selectorName).
                    setProjectPath(project.getPath()).
                    setDescription(desc).
                    setDisplayName(String.format("%s in %s and subprojects.", selectorName, project.toString())).
                    setPublic(visibleTasks.contains(selectorName));
        }
    }
    private final ProjectTaskLister taskLister;

    public BuildInvocationsBuilder(ProjectTaskLister taskLister) {
        this.taskLister = taskLister;
    }

    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();
        Map<String, TaskSelectorBuilder> aggregatedTasks = Maps.newLinkedHashMap();
        Set<String> visibleTasks = Sets.newLinkedHashSet();
        findTasks(project, aggregatedTasks, visibleTasks);
        for (TaskSelectorBuilder selectorData : aggregatedTasks.values()) {
            selectors.add(selectorData.build(project, visibleTasks));
        }
        return new DefaultBuildInvocations()
                .setSelectors(selectors)
                .setTasks(tasks(project));
    }

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

    // build tasks without project reference
    private List<LaunchableGradleTask> tasks(Project project) {
        List<LaunchableGradleTask> tasks = Lists.newArrayList();
        for (Task task : taskLister.listProjectTasks(project)) {
            tasks.add(new LaunchableGradleTask()
                    .setPath(task.getPath())
                    .setName(task.getName())
                    .setDisplayName(task.toString())
                    .setDescription(task.getDescription())
                    .setPublic(task.getGroup() != null));
        }
        return tasks;
    }

    private void findTasks(Project project, Map<String, TaskSelectorBuilder> tasks, Collection<String> visibleTasks) {
        for (Project child : project.getChildProjects().values()) {
            findTasks(child, tasks, visibleTasks);
        }
        for (Task task : taskLister.listProjectTasks(project)) {
            TaskSelectorBuilder selectorBuilder = tasks.get(task.getName());
            if (selectorBuilder != null) {
                selectorBuilder.addDescription(task.getDescription());
            } else {
                tasks.put(task.getName(), new TaskSelectorBuilder(task.getName(), task.getDescription()));
            }
            if (task.getGroup() != null) {
                visibleTasks.add(task.getName());
            }
        }
    }

}

@@ -704,47 +704,8 @@
 ts;%0A
-import com.google.common.collect.Maps;%0A
 impo
@@ -735,24 +735,24 @@
 llect.Sets;%0A
+
 import org.g
@@ -1227,30 +1227,8 @@
 st;%0A
-import java.util.Map;%0A
 impo
@@ -1332,1309 +1332,8 @@
 er %7B
-%0A%0A    private static class TaskSelectorBuilder %7B%0A        private String selectorName;%0A        private String description;%0A%0A        public TaskSelectorBuilder(String selectorName, String description) %7B%0A            this.selectorName = selectorName;%0A            this.description = description;%0A        %7D%0A%0A        public void addDescription(String description) %7B%0A            if (description != null) %7B%0A                this.description = description;%0A            %7D%0A        %7D%0A%0A        LaunchableGradleTaskSelector build(Project project, Set%3CString%3E visibleTasks) %7B%0A            String desc = description != null%0A                    ? description%0A                    : (project.getParent() != null%0A                        ? String.format(%22%25s:%25s task selector%22, project.getPath(), selectorName)%0A                        : String.format(%22%25s task selector%22, selectorName));%0A            return new LaunchableGradleTaskSelector().%0A                    setName(selectorName).%0A                    setTaskName(selectorName).%0A                    setProjectPath(project.getPath()).%0A                    setDescription(desc).%0A                    setDisplayName(String.format(%22%25s in %25s and subprojects.%22, selectorName, project.toString())).%0A                    setPublic(visibleTasks.contains(selectorName));%0A        %7D%0A    %7D
 %0A   
@@ -1920,39 +1920,18 @@
     
-Map
+Set
 %3CString
-, TaskSelectorBuilder
 %3E ag
@@ -1946,19 +1946,19 @@
 Tasks = 
-Map
+Set
 s.newLin
@@ -1964,19 +1964,19 @@
 nkedHash
-Map
+Set
 ();%0A    
@@ -2107,137 +2107,678 @@
 or (
-TaskSelectorBuilder selectorData : aggregatedTasks.values()) %7B%0A            selectors.add(selectorData.build(project, visibleTasks
+String selectorName : aggregatedTasks) %7B%0A            selectors.add(new LaunchableGradleTaskSelector().%0A                    setName(selectorName).%0A                    setTaskName(selectorName).%0A                    setProjectPath(project.getPath()).%0A                    setDescription(project.getParent() != null%0A                            ? String.format(%22%25s:%25s task selector%22, project.getPath(), selectorName)%0A                            : String.format(%22%25s task selector%22, selectorName)).%0A                    setDisplayName(String.format(%22%25s in %25s and subprojects.%22, selectorName, project.toString())).%0A                    setPublic(visibleTasks.contains(selectorName)
 ));%0A
@@ -3761,39 +3761,25 @@
 ct, 
-Map%3CString, TaskSelectorBuilder
+Collection%3CString
 %3E ta
@@ -4028,328 +4028,34 @@
     
-TaskSelectorBuilder selectorBuilder = tasks.get(task.getName());%0A            if (selectorBuilder != null) %7B%0A                selectorBuilder.addDescription(task.getDescription());%0A            %7D else %7B%0A                tasks.put(task.getName(), new TaskSelectorBuilder(task.getName(), task.getDescription()));%0A            %7D
+tasks.add(task.getName());
 %0A   

Revert Radim's last two commits