package eu.humanbrainproject.mip.algorithms;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * Configuration of the algorithms running in MIP Woken execution runtime.
 *
 * The configuration is done via a set of environment variables.
 */
public class Configuration {

    public static final Configuration INSTANCE = new Configuration();

    private Configuration() {
    }

    public String inputJdbcUrl() {
        return env("IN_JDBC_URL");
    }

    public String inputJdbcUser() {
        return env("IN_JDBC_USER");
    }

    public String inputJdbcPassword() {
        return env("IN_JDBC_PASSWORD");
    }

    public String inputSqlQuery() {
        return env("PARAM_query", "");
    }

    public String outputJdbcUrl() {
        return env("OUT_JDBC_URL");
    }

    public String outputJdbcUser() {
        return env("OUT_JDBC_USER");
    }

    public String outputJdbcPassword() {
        return env("OUT_JDBC_PASSWORD");
    }

    public String outputResultTable() {
        return env("RESULT_TABLE", "job_result");
    }

    public String[] variables() {

        final String variables = env("PARAM_variables");
        if (variables == null) {
            throw new RuntimeException("Environment parameter PARAM_variables is empty");
        }
        return variables.split(",");
    }

    public String[] covariables() {
        final String covariables = env("PARAM_covariables");
        if (covariables == null) {
            throw new RuntimeException("Environment parameter PARAM_covariables is empty");
        }
        return covariables.split(",");
    }

    public double randomSeed() {
        return Double.valueOf(env("PARAM_seed", "0.67"));
    }

    public String jobId() {
        return env("JOB_ID");
    }

    public String executionNode() {
        return env("NODE");
    }

    public String function() {
        return env("FUNCTION", "JAVA");
    }

    public String dockerImage() {
        return env("DOCKER_IMAGE", "");
    }

    /**
     * Return the values of the algorithm parameters read from the environment variables.
     * For a parameter k, we read first the value of environment variable MODEL_PARAM_k,
     * then for backwards compatibility the value of environment variable PARAM_MODEL_k
     */
    public Map<String, String> algorithmParameterValues(Set<String> algorithmParameters) {
        HashMap<String, String> values = new HashMap<>();
        for (String param: algorithmParameters) {
            values.put(param, env("MODEL_PARAM_" + param, env("PARAM_MODEL_" + param)));
        }
        return values;
    }

    /**
     * Return the values of the algorithm parameters read from the environment variables.
     * For a parameter k, we read first the value of environment variable MODEL_PARAM_k,
     * then for backwards compatibility the value of environment variable PARAM_MODEL_k.
     * If no matching environment variable is found, then the value of k in the input map is used.
     */
    public Map<String, String> algorithmParameterValues(Map<String, String> algorithmParametersWithDefaultValues) {
        HashMap<String, String> values = new HashMap<>();
        for (String param: algorithmParametersWithDefaultValues.keySet()) {
            values.put(param, env("MODEL_PARAM_" + param, env("PARAM_MODEL_" + param, algorithmParametersWithDefaultValues.get(param))));
        }
        return values;
    }

    private static String env(String key) {
        // Read first system property then env variables
        return System.getProperty(key, System.getenv(key));
    }

    private static String env(String key, String defaultValue) {
        // Read first system property then env variables
        return System.getProperty(key, System.getenv().getOrDefault(key, defaultValue));
    }

}

@@ -391,32 +391,140 @@
 tion() %7B%0A    %7D%0A%0A
+    public String inputJdbcDriver() %7B%0A        return env(%22IN_JDBC_DRIVER%22, %22org.postgresql.Driver%22);%0A    %7D%0A%0A
     public Strin
@@ -534,32 +534,32 @@
 nputJdbcUrl() %7B%0A
-
         return e
@@ -643,21 +643,16 @@
 env(%22IN_
-JDBC_
 USER%22);%0A
@@ -721,21 +721,16 @@
 env(%22IN_
-JDBC_
 PASSWORD
@@ -814,32 +814,142 @@
 y%22, %22%22);%0A    %7D%0A%0A
+    public String outputJdbcDriver() %7B%0A        return env(%22OUT_JDBC_DRIVER%22, %22org.postgresql.Driver%22);%0A    %7D%0A%0A
     public Strin
@@ -1072,21 +1072,16 @@
 nv(%22OUT_
-JDBC_
 USER%22);%0A
@@ -1120,32 +1120,32 @@
 dbcPassword() %7B%0A
+
         return e
@@ -1152,21 +1152,16 @@
 nv(%22OUT_
-JDBC_
 PASSWORD
@@ -2752,37 +2752,8 @@
 aram
-, env(%22PARAM_MODEL_%22 + param)
 ));%0A
@@ -3486,36 +3486,8 @@
 ram,
- env(%22PARAM_MODEL_%22 + param,
  alg
@@ -3532,17 +3532,16 @@
 param)))
-)
 ;%0A      

Update environment parameters for Woken 2.7+