/*
 * Copyright 2000-2011 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 com.intellij.core;

import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.impl.jar.CoreJarFileSystem;
import com.intellij.psi.*;
import com.intellij.psi.impl.file.PsiPackageImpl;
import com.intellij.psi.impl.file.impl.JavaFileManager;
import com.intellij.psi.search.GlobalSearchScope;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author yole
 */
public class CoreJavaFileManager implements JavaFileManager {
  private final CoreJarFileSystem myJarFileSystem;
  private final List<File> myClasspath = new ArrayList<File>();
  private final PsiManager myPsiManager;

  public CoreJavaFileManager(PsiManager psiManager, CoreJarFileSystem jarFileSystem) {
    myPsiManager = psiManager;
    myJarFileSystem = jarFileSystem;
  }

  @Override
  public PsiPackage findPackage(@NotNull String packageName) {
    String dirName = packageName.replace(".", "/");
    for (File file : myClasspath) {
      if (file.isFile()) {
        VirtualFile classDir = myJarFileSystem.findFileByPath(file.getPath() + "!/" + dirName);
        if (classDir != null) {
          return new PsiPackageImpl(myPsiManager, packageName);
        }
      }
    }
    return null;
  }

  @Override
  public PsiClass findClass(@NotNull String qName, @NotNull GlobalSearchScope scope) {
    String fileName = qName.replace(".", "/") + ".class";
    for (File file : myClasspath) {
      if (file.isFile()) {
        VirtualFile classFile = myJarFileSystem.findFileByPath(file.getPath() + "!/" + fileName);
        if (classFile != null) {
          PsiFile psiFile = myPsiManager.findFile(classFile);
          if (!(psiFile instanceof PsiJavaFile)) {
            throw new UnsupportedOperationException("no java file for .class");
          }
          final PsiClass[] classes = ((PsiJavaFile)psiFile).getClasses();
          if (classes.length == 1) {
            return classes[0];
          }
        }
      }
    }
    return null;
  }

  @Override
  public PsiClass[] findClasses(@NotNull String qName, @NotNull GlobalSearchScope scope) {
    throw new UnsupportedOperationException("TODO");
  }

  @Override
  public Collection<String> getNonTrivialPackagePrefixes() {
    return Collections.emptyList();
  }

  @Override
  public void initialize() {
  }

  public void addToClasspath(File path) {
    myClasspath.add(path);
  }
}

@@ -951,16 +951,59 @@
 NotNull;
+%0Aimport org.jetbrains.annotations.Nullable;
 %0A%0Aimport
@@ -2058,24 +2058,303 @@
 cope) %7B%0A    
+for (File file : myClasspath) %7B%0A      final PsiClass psiClass = findClassInClasspathEntry(qName, file);%0A      if (psiClass != null) %7B%0A        return null;%0A      %7D%0A    %7D%0A    return null;%0A  %7D%0A%0A  @Nullable%0A  private PsiClass findClassInClasspathEntry(String qName, File file) %7B%0A    
 String fileN
@@ -2399,46 +2399,8 @@
 s%22;%0A
-    for (File file : myClasspath) %7B%0A  
     
@@ -2418,34 +2418,32 @@
 File()) %7B%0A      
-  
 VirtualFile clas
@@ -2514,34 +2514,32 @@
 ileName);%0A      
-  
 if (classFile !=
@@ -2555,18 +2555,16 @@
         
- 
- 
 PsiFile 
@@ -2611,26 +2611,24 @@
 e);%0A        
-  
 if (!(psiFil
@@ -2652,26 +2652,24 @@
 avaFile)) %7B%0A
-  
           th
@@ -2738,30 +2738,26 @@
 %22);%0A        
-  %7D%0A  
+%7D%0A
         fina
@@ -2820,26 +2820,24 @@
 ();%0A        
-  
 if (classes.
@@ -2857,26 +2857,24 @@
 %7B%0A          
-  
 return class
@@ -2880,18 +2880,16 @@
 ses%5B0%5D;%0A
-  
         
@@ -2890,26 +2890,16 @@
       %7D%0A
-        %7D%0A
       %7D%0A
@@ -3037,54 +3037,294 @@
     
-throw new UnsupportedOperationException(%22TODO%22
+List%3CPsiClass%3E result = new ArrayList%3CPsiClass%3E();%0A    for (File file : myClasspath) %7B%0A      final PsiClass psiClass = findClassInClasspathEntry(qName, file);%0A      if (psiClass != null) %7B%0A        result.add(psiClass);%0A      %7D%0A    %7D%0A    return result.toArray(new PsiClass%5Bresult.size()%5D
 );%0A 

implement JavaFileManager.findClasses()