package com.poratu.idea.plugins.tomcat.utils;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.util.JdkBundle;
import com.poratu.idea.plugins.tomcat.conf.TomcatRunConfiguration;
import com.poratu.idea.plugins.tomcat.setting.TomcatInfo;
import org.apache.commons.lang.StringUtils;

import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
import java.util.jar.JarFile;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;

/**
 * Author : zengkid
 * Date   : 2017-03-06
 * Time   : 21:35
 */
public abstract class PluginUtils {

    public static String getJavaHome() {
        Sdk sdk = getProjectJDK();
        String javahome;
        if (sdk == null) {
            JdkBundle jdkBundle = JdkBundle.createBoot();
            javahome = jdkBundle.getLocation().getAbsolutePath();
        } else {
            javahome = sdk.getHomePath();
        }
        return javahome;
    }

    private static Sdk getProjectJDK() {
        Sdk[] allJdks = ProjectJdkTable.getInstance().getAllJdks();
        Sdk jdk = allJdks.length == 0 ? null : allJdks[0];
        return jdk;
    }

    public static TomcatInfo getTomcatInfo(String tomcatHome) {
        final TomcatInfo tomcatInfo = new TomcatInfo();
        tomcatInfo.setPath(tomcatHome);

        try {
            File jarFile = new File(tomcatHome, "lib/catalina.jar");
            if (!jarFile.exists()) {
                throw new RuntimeException("tomcat path [" + tomcatHome + "] is incorrect!");
            }
            try (JarFile jar = new JarFile(jarFile)) {
                ZipEntry entry = jar.getEntry("org/apache/catalina/util/ServerInfo.properties");

                Properties p = new Properties();
                try (InputStream is = jar.getInputStream(entry)) {
                    p.load(is);
                }

                String serverInfo = p.getProperty("server.info");
                String serverNumber = p.getProperty("server.number");
                tomcatInfo.setName(serverInfo);
                tomcatInfo.setVersion(serverNumber);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return tomcatInfo;
    }

    public static Path getWorkPath(TomcatRunConfiguration configuration) {

        String userHome = System.getProperty("user.home");
        Project project = configuration.getProject();
        Module module = configuration.getModule();
        Path workPath = Paths.get(userHome, ".SmartTomcat", project.getName(), module.getName());

        return workPath;
    }
}

@@ -397,52 +397,57 @@
 fo;%0A
+%0A
 import 
-org.apache.commons.lang.StringUtils;%0A
+java.io.File;%0Aimport java.io.IOException;
 %0Aimp
@@ -458,17 +458,27 @@
 java.io.
-*
+InputStream
 ;%0Aimport
@@ -589,40 +589,8 @@
 le;%0A
-import java.util.stream.Stream;%0A
 impo
@@ -1197,17 +1197,14 @@
     
-Sdk jdk =
+return
  all
@@ -1245,28 +1245,8 @@
 0%5D;%0A
-        return jdk;%0A
     
@@ -2570,24 +2570,25 @@
 dule();%0A
+%0A
         
 Path wor
@@ -2583,23 +2583,14 @@
     
-Path workPath =
+return
  Pat
@@ -2663,34 +2663,8 @@
 ());
-%0A%0A        return workPath;
 %0A   

remove no used import