package org.jetbrains.kotlin.core.utils;

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

import org.eclipse.core.resources.IFile;
import org.eclipse.jdt.core.IClasspathEntry;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.java.PackageClassUtils;
import org.jetbrains.jet.lang.resolve.name.FqName;
import org.jetbrains.jet.plugin.JetMainDetector;

public class ProjectUtils {
    
    public static IFile getMainClass(Collection<IFile> files) {
        IJavaProject javaProject = getJavaProjectFromCollection(files);
        KotlinEnvironment kotlinEnvironment = new KotlinEnvironment(javaProject);
        
        for (IFile file : files) {
            JetFile jetFile = kotlinEnvironment.getJetFile(file);
            if (jetFile == null) {
                continue;
            }
            
            if (JetMainDetector.hasMain(jetFile.getDeclarations())) {
                return file;
            }
        }
                
        return null;
    }
    
    public static IJavaProject getJavaProjectFromCollection(Collection<IFile> files) {
        IJavaProject javaProject = null;
        for (IFile file : files) {
            javaProject = JavaCore.create(file.getProject());
            break;
        }
        
        return javaProject;
    }
    
    public static boolean hasMain(IFile file) {
        return getMainClass(Arrays.asList(file)) != null;
    }
    
    @Nullable
    public static String getPackageByFile(IFile file) {
        IJavaProject javaProject = JavaCore.create(file.getProject());
        JetFile jetFile = new KotlinEnvironment(javaProject).getJetFile(file);
        
        assert jetFile != null;
        
        return jetFile.getPackageName();
    }
    
    public static FqName createPackageClassName(IFile file) {
        String filePackage = getPackageByFile(file);
        if (filePackage == null) {
            return null;
        }
        return PackageClassUtils.getPackageClassFqName(new FqName(filePackage));
    }
    
    @NotNull
    public static List<File> getSrcDirectories(@NotNull IJavaProject javaProject) throws JavaModelException {
        List<File> srcDirectories = new ArrayList<File>();
        
        IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
        String rootDirectory = javaProject.getProject().getLocation().removeLastSegments(1).toPortableString();
        for (IClasspathEntry classpathEntry : classpathEntries) {
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                File file = new File(rootDirectory + classpathEntry.getPath().toPortableString());
                srcDirectories.add(file);
            }
        }
        
        return srcDirectories;
    }
    
    @NotNull
    public static List<File> getLibDirectories(@NotNull IJavaProject javaProject) throws JavaModelException {
        List<File> libDirectories = new ArrayList<File>();
        
        IClasspathEntry[] classpathEntries = javaProject.getRawClasspath();
        for (IClasspathEntry classpathEntry : classpathEntries) {
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
                File file = new File(classpathEntry.getPath().toPortableString());
                libDirectories.add(file);
            }
        }
        
        return libDirectories;
    }
}
@@ -199,24 +199,63 @@
 rces.IFile;%0A
+import org.eclipse.core.runtime.IPath;%0A
 import org.e
@@ -3329,34 +3329,39 @@
 javaProject.getR
-aw
+esolved
 Classpath();%0A   
@@ -3346,35 +3346,177 @@
 solvedClasspath(
+false
 );%0A
+        IPath rootDirectory = javaProject.getProject().getLocation();%0A        String projectName = rootDirectory.lastSegment();%0A        %0A
         for (ICl
@@ -3665,74 +3665,544 @@
     
-File file = new File(classpathEntry.getPath().toPortableString());
+String classpath = classpathEntry.getPath().toPortableString();%0A                File file = new File(classpath);%0A                %0A                if (!file.isAbsolute()) %7B%0A                    if (classpathEntry.getPath().segment(0).equals(projectName)) %7B%0A                        file = new File(rootDirectory.removeLastSegments(1).toPortableString() + classpath);%0A                    %7D else %7B%0A                        file = new File(rootDirectory.toPortableString() + classpath);%0A                    %7D%0A                %7D%0A                
 %0A   

Fix KT-3831 - "Can't reference the local jar"