package org.foxide.wizard;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.JarURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Path;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;

import org.eclipse.ui.wizards.datatransfer.FileSystemStructureProvider;
import org.eclipse.ui.wizards.datatransfer.IImportStructureProvider;

public class ImportProject {

    private URL projectUrl;
    private URL projectFileUrl;
    private URLConnection projectFileConnection;
    private URLConnection projectConnection;

    public ImportProject(URL projectUrl) throws IOException, URISyntaxException {
        this.projectUrl = projectUrl;
        Path projectPath = new File(projectUrl.toString()).toPath();
        Path projectFilePath = projectPath.resolve(".project");
        this.projectFileUrl = new URL(projectFilePath.toString());
        this.projectConnection = projectUrl.openConnection();
        this.projectFileConnection = projectFileUrl.openConnection();
    }

    public InputStream getProjectFile() throws IOException, URISyntaxException {
        if (isJarFile()) {
            JarURLConnection jarconnection = (JarURLConnection) projectFileConnection;
            JarFile jarFile = jarconnection.getJarFile();
            String entryName = jarconnection.getEntryName();
            ZipEntry zentry = jarFile.getEntry(entryName);
            return jarFile.getInputStream(zentry);
        } else {
            return new FileInputStream(new File(projectFileUrl.toURI()));
        }
    }

    public Object getProjectRootFolder() throws URISyntaxException, IOException {
        if (isJarFile()) {
            JarURLConnection jarconnection = (JarURLConnection) projectConnection;
            JarFile jarFile = jarconnection.getJarFile();
            String entryName = jarconnection.getEntryName();
            ZipEntry zentry = jarFile.getEntry(entryName);
            return zentry;
        } else {
            return new File(projectUrl.toURI());
        }
    }

    private boolean isJarFile() {
        return projectConnection instanceof JarURLConnection;
    }

    public IImportStructureProvider getImportStructureProvider() throws IOException {
        if (isJarFile()) {
            JarURLConnection jarconnection = (JarURLConnection) projectConnection;
            return new JarImportStructureProvider(jarconnection.getJarFile());
        } else {
            return FileSystemStructureProvider.INSTANCE;
        }
    }

    public URLConnection getProjectConnection() {
        return projectConnection;
    }
}
@@ -256,35 +256,8 @@
 on;%0A
-import java.nio.file.Path;%0A
 impo
@@ -769,28 +769,30 @@
         
-Path
+String
  project
 Path = n
@@ -787,24 +787,19 @@
 ject
+File
 Path = 
-new File(
 proj
@@ -819,18 +819,8 @@
 ng()
-).toPath()
 ;%0A  
@@ -825,20 +825,83 @@
         
+if (!projectFilePath.endsWith(%22/%22)) %7B%0A            projectFile
 Path
+ =
  project
@@ -913,30 +913,69 @@
 ath 
-= projectPath.resolve(
++ %22/%22;%0A        %7D%0A        projectFilePath = projectFilePath + 
 %22.pr
@@ -980,17 +980,16 @@
 project%22
-)
 ;%0A      
@@ -1035,27 +1035,16 @@
 FilePath
-.toString()
 );%0A     

support windows