package calabash.java.android;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import static calabash.java.android.Utils.isWindows;

public class Environment {
    public static final String ENV_ANDROID_HOME = "ANDROID_HOME";
    public static final String ENV_JAVA_HOME = "JAVA_HOME";
    private final String keytool;
    private final String jarsigner;
    private final Map<String, String> envVariables = new HashMap<String, String>();
    private final String androidHome;

    public Environment(String androidHome, String javaHome, String keytool, String jarsigner) throws CalabashException {
        this.keytool = keytool;
        this.jarsigner = jarsigner;
        if (!isValidAndroidHome(androidHome))
            throw new CalabashException(String.format("Invalid %s : %s", ENV_ANDROID_HOME, androidHome));
        this.androidHome = androidHome;
        envVariables.put(ENV_ANDROID_HOME, androidHome);
        if (javaHome != null && !javaHome.isEmpty())
            envVariables.put(ENV_JAVA_HOME, javaHome);
    }

    private static String getPlatformExecutable(String executable) {
        return isWindows() ? executable + ".exe" : executable;
    }

    private boolean isValidAndroidHome(String androidHome) {
        return getAdbFile(androidHome).exists();
    }

    public Map<String, String> getEnvVariables() {
        return new HashMap<String, String>(envVariables);
    }

    public String getKeytool() {
        String keytool = getPlatformExecutable(this.keytool);
        return quotify(keytool);
    }

    public String getJarsigner() {
        String jarsigner = getPlatformExecutable(this.jarsigner);
        return quotify(jarsigner);
    }

    public String getAdb() {
        return quotify(getAdbFile(androidHome).getAbsolutePath());
    }

    private File getAdbFile(String androidHome) {
        return new File(androidHome + File.separator + "platform-tools" + File.separator + getPlatformExecutable("adb"));
    }

    private File getEmulatorFile(String androidHome) {
        return new File(androidHome + File.separator + "tools" + File.separator + getPlatformExecutable("emulator"));
    }

    public String getEmulator() {
        return quotify(getEmulatorFile(androidHome).getAbsolutePath());
    }

    private String quotify(String executable) {
        return  "\"" + executable + "\"";
    }
}

@@ -1534,19 +1534,26 @@
 urn quot
-ify
+eIfWindows
 (keytool
@@ -1682,19 +1682,26 @@
 urn quot
-ify
+eIfWindows
 (jarsign
@@ -1756,27 +1756,34 @@
  return quot
-ify
+eIfWindows
 (getAdbFile(
@@ -1993,32 +1993,152 @@
 %22adb%22));%0A    %7D%0A%0A
+    public String getEmulator() %7B%0A        return quoteIfWindows(getEmulatorFile(androidHome).getAbsolutePath());%0A    %7D%0A%0A
     private File
@@ -2310,142 +2310,36 @@
    p
-ublic String getEmulator() %7B%0A        return quotify(getEmulatorFile(androidHome).getAbsolutePath());%0A    %7D%0A%0A    private String quotify
+rivate String quoteIfWindows
 (Str
@@ -2371,16 +2371,29 @@
  return 
+isWindows() ?
  %22%5C%22%22 + 
@@ -2409,16 +2409,29 @@
 e + %22%5C%22%22
+ : executable
 ;%0A    %7D%0A

Revert "quoting executable path in all platforms" - Doesn't work in *nix