/*
 * 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.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import org.gradle.api.GradleException;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.tooling.internal.gradle.DefaultBuildInvocations;
import org.gradle.tooling.internal.gradle.DefaultGradleProject;
import org.gradle.tooling.internal.gradle.DefaultGradleTask;
import org.gradle.tooling.internal.gradle.DefaultGradleTaskSelector;
import org.gradle.tooling.internal.gradle.PartialGradleProject;
import org.gradle.tooling.model.internal.ProjectSensitiveToolingModelBuilder;

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

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<DefaultGradleTaskSelector> selectors = Lists.newArrayList();
        Multimap<String, String> aggregatedTasks = findTasks(project);
        for (String selectorName : aggregatedTasks.keySet()) {
            selectors.add(new DefaultGradleTaskSelector().
                    setName(selectorName).
                    setTaskName(selectorName).
                    setProjectDir(project.getProjectDir()).
                    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) {
        if (implicitProject) {
            DefaultGradleProject gradleProject = gradleProjectBuilder.buildAll(project);
            List<DefaultGradleTask> tasks = new ArrayList<DefaultGradleTask>();
            fillTaskList(gradleProject, tasks);
            return new DefaultBuildInvocations()
                    .setSelectors(buildRecursively(modelName, project.getRootProject()))
                    .setTasks(convertTasks(tasks));
        } else {
            return buildAll(modelName, project);
        }
    }

    private void fillTaskList(PartialGradleProject gradleProject, List<DefaultGradleTask> tasks) {
        tasks.addAll(gradleProject.getTasks());
        for (PartialGradleProject childProject : gradleProject.getChildren()) {
            fillTaskList(childProject, tasks);
        }
    }

    private List<DefaultGradleTaskSelector> buildRecursively(String modelName, Project project) {
        List<DefaultGradleTaskSelector> selectors = Lists.newArrayList();
        selectors.addAll(buildAll(modelName, project).getTaskSelectors());
        for (Project childProject : project.getSubprojects()) {
            selectors.addAll(buildRecursively(modelName, childProject));
        }
        return selectors;
    }

    private List<DefaultGradleTask> convertTasks(List<DefaultGradleTask> tasks) {
        for (DefaultGradleTask task :  tasks) {
            task.setProject(null);
        }
        return tasks;
    }

    private Multimap<String, String> findTasks(Project project) {
        Multimap<String, String> aggregatedTasks = ArrayListMultimap.create();
        for (Project child : project.getSubprojects()) {
            Multimap<String, String> childTasks = findTasks(child);
            aggregatedTasks.putAll(childTasks);
        }
        for (Task task : project.getTasks()) {
            aggregatedTasks.put(task.getName(), task.getPath());
        }
        return aggregatedTasks;
    }

}

@@ -664,60 +664,8 @@
 g;%0A%0A
-import com.google.common.collect.ArrayListMultimap;%0A
 impo
@@ -733,24 +733,20 @@
 collect.
-Multimap
+Sets
 ;%0Aimport
@@ -1291,16 +1291,38 @@
 il.List;
+%0Aimport java.util.Set;
 %0A%0Apublic
@@ -2028,33 +2028,20 @@
         
-Multimap%3CString, 
+Set%3C
 String%3E 
@@ -2132,17 +2132,8 @@
 asks
-.keySet()
 ) %7B%0A
@@ -4555,33 +4555,20 @@
 private 
-Multimap%3CString, 
+Set%3C
 String%3E 
@@ -4604,33 +4604,20 @@
         
-Multimap%3CString, 
+Set%3C
 String%3E 
@@ -4638,32 +4638,23 @@
 s = 
-ArrayListMultimap.create
+Sets.newHashSet
 ();%0A
@@ -4726,25 +4726,12 @@
     
-Multimap%3CString, 
+Set%3C
 Stri
@@ -4793,19 +4793,19 @@
 edTasks.
-put
+add
 All(chil
@@ -4902,11 +4902,11 @@
 sks.
-put
+add
 (tas
@@ -4920,24 +4920,8 @@
 me()
-, task.getPath()
 );%0A 

Use set to build task selector (REVIEW-4479).