/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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.jetbrains.plugins.gradle.tooling.internal;

import org.gradle.tooling.model.DomainObjectSet;
import org.gradle.tooling.model.idea.IdeaSourceDirectory;
import org.gradle.tooling.model.internal.ImmutableDomainObjectSet;
import org.jetbrains.plugins.gradle.model.ExtIdeaContentRoot;

import java.io.File;
import java.util.*;

/**
 * @author Vladislav.Soroka
 * @since 11/13/13
 */
public class IdeaContentRootImpl implements ExtIdeaContentRoot {

  private final File myRootDirectory;
  private final List<IdeaSourceDirectory> mySourceDirectories;
  private final List<IdeaSourceDirectory> myTestDirectories;
  private final List<IdeaSourceDirectory> myResourceDirectories;
  private final List<IdeaSourceDirectory> myTestResourceDirectories;
  private final Set<File> myExcludeDirectories;

  public IdeaContentRootImpl(File rootDirectory) {
    myRootDirectory = rootDirectory;
    mySourceDirectories = new ArrayList<IdeaSourceDirectory>();
    myTestDirectories = new ArrayList<IdeaSourceDirectory>();
    myResourceDirectories = new ArrayList<IdeaSourceDirectory>();
    myTestResourceDirectories = new ArrayList<IdeaSourceDirectory>();
    myExcludeDirectories = new HashSet<File>();
  }

  @Override
  public File getRootDirectory() {
    return myRootDirectory;
  }

  @Override
  public DomainObjectSet<? extends IdeaSourceDirectory> getSourceDirectories() {
    return ImmutableDomainObjectSet.of(mySourceDirectories);
  }

  public void addSourceDirectory(IdeaSourceDirectory sourceDirectory) {
    mySourceDirectories.add(sourceDirectory);
  }

  @Override
  public DomainObjectSet<? extends IdeaSourceDirectory> getGeneratedSourceDirectories() {
    return ImmutableDomainObjectSet.of(Collections.<IdeaSourceDirectory>emptyList());
  }

  public void addTestDirectory(IdeaSourceDirectory testDirectory) {
    myTestDirectories.add(testDirectory);
  }

  public void addResourceDirectory(IdeaSourceDirectory resourceDirectory) {
    myResourceDirectories.add(resourceDirectory);
  }

  public void addTestResourceDirectory(IdeaSourceDirectory resourceDirectory) {
    myTestResourceDirectories.add(resourceDirectory);
  }

  public void addExcludeDirectory(File excludeDirectory) {
    myExcludeDirectories.add(excludeDirectory);
  }

  @Override
  public DomainObjectSet<? extends IdeaSourceDirectory> getTestDirectories() {
    return ImmutableDomainObjectSet.of(myTestDirectories);
  }

  @Override
  public DomainObjectSet<? extends IdeaSourceDirectory> getGeneratedTestDirectories() {
    return ImmutableDomainObjectSet.of(Collections.<IdeaSourceDirectory>emptyList());
  }

  @Override
  public DomainObjectSet<? extends IdeaSourceDirectory> getResourceDirectories() {
    return ImmutableDomainObjectSet.of(myResourceDirectories);
  }

  @Override
  public DomainObjectSet<? extends IdeaSourceDirectory> getTestResourceDirectories() {
    return ImmutableDomainObjectSet.of(myTestResourceDirectories);
  }

  @Override
  public Set<File> getExcludeDirectories() {
    return myExcludeDirectories;
  }
}

@@ -2254,32 +2254,312 @@
 Directories() %7B%0A
+    List%3CIdeaSourceDirectory%3E generatedSourceDirectories = new ArrayList%3CIdeaSourceDirectory%3E();%0A    for (IdeaSourceDirectory sourceDirectory : mySourceDirectories) %7B%0A      if(sourceDirectory.isGenerated()) %7B%0A        generatedSourceDirectories.add(sourceDirectory);%0A      %7D%0A    %7D%0A
     return Immut
@@ -2581,33 +2581,25 @@
 tSet.of(
-Collections.%3CIdea
+generated
 SourceDi
@@ -2600,37 +2600,27 @@
 urceDirector
-y%3EemptyList()
+ies
 );%0A  %7D%0A%0A  pu
@@ -3375,87 +3375,341 @@
     
-return ImmutableDomainObjectSet.of(Collections.%3CIdeaSourceDirectory%3EemptyList()
+List%3CIdeaSourceDirectory%3E generatedTestDirectories = new ArrayList%3CIdeaSourceDirectory%3E();%0A    for (IdeaSourceDirectory sourceDirectory : myTestDirectories) %7B%0A      if(sourceDirectory.isGenerated()) %7B%0A        generatedTestDirectories.add(sourceDirectory);%0A      %7D%0A    %7D%0A    return ImmutableDomainObjectSet.of(generatedTestDirectories
 );%0A 

upgrade Tooling API from 2.1 up to 2.2 + generated source folders importing added