/*
 * Copyright 2000-2009 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.config;

import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.openapi.externalSystem.psi.search.ExternalModuleBuildGlobalSearchScope;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.ProjectFileIndex;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.NonClasspathClassFinder;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiFile;
import com.intellij.psi.search.EverythingGlobalScope;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.gradle.service.GradleBuildClasspathManager;
import org.jetbrains.plugins.groovy.GroovyFileType;

import java.util.List;

/**
 * @author peter
 */
public class GradleClassFinder extends NonClasspathClassFinder {

  @NotNull private final GradleBuildClasspathManager myBuildClasspathManager;

  public GradleClassFinder(Project project, @NotNull GradleBuildClasspathManager buildClasspathManager) {
    super(project, JavaFileType.DEFAULT_EXTENSION, GroovyFileType.DEFAULT_EXTENSION);
    myBuildClasspathManager = buildClasspathManager;
  }

  @Override
  protected List<VirtualFile> calcClassRoots() {
    // do not use default NonClasspathClassFinder caching strategy based on PSI change
    // the caching performed in GradleBuildClasspathManager
    throw new AssertionError();
  }

  @Override
  protected List<VirtualFile> getClassRoots() {
    return myBuildClasspathManager.getAllClasspathEntries();
  }

  @Override
  protected List<VirtualFile> getClassRoots(@Nullable GlobalSearchScope scope) {
    if (scope instanceof ExternalModuleBuildGlobalSearchScope) {
      ExternalModuleBuildGlobalSearchScope externalModuleBuildGlobalSearchScope = (ExternalModuleBuildGlobalSearchScope)scope;
      return myBuildClasspathManager.getModuleClasspathEntries(externalModuleBuildGlobalSearchScope.getExternalModulePath());
    }
    return myBuildClasspathManager.getAllClasspathEntries();
  }

  @Nullable
  @Override
  public Condition<PsiClass> getClassesFilter(@NotNull final GlobalSearchScope scope) {
    return new Condition<PsiClass>() {
      @Override
      public boolean value(PsiClass aClass) {
        if (scope instanceof ExternalModuleBuildGlobalSearchScope || scope instanceof EverythingGlobalScope) {
          return true;
        }
        PsiFile containingFile = aClass.getContainingFile();
        VirtualFile file = containingFile != null ? containingFile.getVirtualFile() : null;
        return file == null || !(ProjectFileIndex.SERVICE.getInstance(myProject).isInContent(file));
      }
    };
  }
}
@@ -860,289 +860,72 @@
 api.
-roots.ProjectFileIndex;%0Aimport com.intellij.openapi.util.Condition;%0Aimport com.intellij.openapi.vfs.VirtualFile;%0Aimport com.intellij.psi.NonClasspathClassFinder;%0Aimport com.intellij.psi.PsiClass;%0Aimport com.intellij.psi.PsiFile;%0Aimport com.intellij.psi.search.EverythingGlobalScope
+vfs.VirtualFile;%0Aimport com.intellij.psi.NonClasspathClassFinder
 ;%0Aim
@@ -2483,637 +2483,6 @@
 %0A  %7D
-%0A%0A  @Nullable%0A  @Override%0A  public Condition%3CPsiClass%3E getClassesFilter(@NotNull final GlobalSearchScope scope) %7B%0A    return new Condition%3CPsiClass%3E() %7B%0A      @Override%0A      public boolean value(PsiClass aClass) %7B%0A        if (scope instanceof ExternalModuleBuildGlobalSearchScope %7C%7C scope instanceof EverythingGlobalScope) %7B%0A          return true;%0A        %7D%0A        PsiFile containingFile = aClass.getContainingFile();%0A        VirtualFile file = containingFile != null ? containingFile.getVirtualFile() : null;%0A        return file == null %7C%7C !(ProjectFileIndex.SERVICE.getInstance(myProject).isInContent(file));%0A      %7D%0A    %7D;%0A  %7D
 %0A%7D

revert IDEA-129535 IDEA marks all classes inside buildSrc with "Class <name> already exists in <package>" (95eb44af0690)