package org.jboss.as.test.shared;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.jboss.as.controller.client.ModelControllerClient;
import org.wildfly.test.api.Authentication;

/**
 * Class that allows for non arquillian tests to access the current server address and port, and other testsuite environment
 * properties.
 * <p/>
 *
 * @author Stuart Douglas
 */
public class TestSuiteEnvironment {

    public static ModelControllerClient getModelControllerClient() {
        try {
            return ModelControllerClient.Factory.create(
                    InetAddress.getByName(getServerAddress()),
                    TestSuiteEnvironment.getServerPort(),
                    Authentication.getCallbackHandler()
                    );
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    }

    public static String getJavaPath() {
        String home = System.getenv("JAVA_HOME");
        if(home == null) {
            home = getSystemProperty("java.home");
        }
        if(home != null) {
            return home + java.io.File.separator + "bin" + java.io.File.separator + "java";
        }
        return "java";
    }

    public static String getSystemProperty(String name, String def) {
        return System.getProperty(name, def);
    }

    public static String getSystemProperty(String name) {
        return System.getProperty(name);
    }

    public static void setSystemProperty(String name, String value) {
        System.setProperty(name, value);
    }

    public static void clearSystemProperty(String name) {
        System.clearProperty(name);
    }

    public static String getTmpDir() {
        return getSystemProperty("java.io.tmpdir");
    }

    /**
     * @return The server port for node0
     */
    public static int getServerPort() {
        return Integer.getInteger("management.port", 9990);
    }

    /**
     * @return The server address of node0
     */
    public static String getServerAddress() {
        return formatPossibleIpv6Address(System.getProperty("management.address", "localhost"));
    }

    /**
     * @return The ipv6 arguments that should be used when launching external java processes, such as the application client
     */
    public static String getIpv6Args() {
        if (System.getProperty("ipv6") == null) {
            return " -Djava.net.preferIPv4Stack=true -Djava.net.preferIPv6Addresses=false ";
        }
        return " -Djava.net.preferIPv4Stack=false -Djava.net.preferIPv6Addresses=true ";
    }

    /**
     *
     */
    public static void getIpv6Args(List<String> command) {
        if (System.getProperty("ipv6") == null) {
            command.add("-Djava.net.preferIPv4Stack=true");
            command.add("-Djava.net.preferIPv6Addresses=false");
        } else {
            command.add("-Djava.net.preferIPv4Stack=false");
            command.add("-Djava.net.preferIPv6Addresses=true");
        }
    }

    public static String formatPossibleIpv6Address(String address) {
        if (address == null) {
            return address;
        }
        if (!address.contains(":")) {
            return address;
        }
        if (address.startsWith("[") && address.endsWith("]")) {
            return address;
        }
        return "[" + address + "]";
    }

    public static String getSecondaryTestAddress(final boolean useCanonicalHost) {
        String address = System.getProperty("secondary.test.address");
        if (StringUtils.isBlank(address)) {
            address = getServerAddress();
        }
        if (useCanonicalHost) {
            address = StringUtils.strip(address, "[]");
        }
        return address;
    }
}

@@ -1911,24 +1911,136 @@
 verPort() %7B%0A
+        //this here is just fallback logic for older testsuite code that wasn't updated to newer property names%0A
         retu
@@ -2083,14 +2083,55 @@
 rt%22,
+ Integer.getInteger(%22as.managementPort%22,
  9990)
+)
 ;%0A  
@@ -2252,41 +2252,117 @@
     
-return formatPossibleIpv6A
+String address = System.getProperty(%22management.address%22);%0A        if (address==null)%7B%0A            a
 ddress
-(
+ = 
 Syst
@@ -2381,28 +2381,134 @@
 ty(%22
-management.address%22,
+node0%22);%0A        %7D%0A        if (address!=null)%7B%0A            return formatPossibleIpv6Address(address);%0A        %7D%0A        return
  %22lo
@@ -2515,18 +2515,16 @@
 calhost%22
-))
 ;%0A    %7D%0A

Allow legacy system properites for configuring address & port