package no.cantara.jau;

import no.cantara.jau.serviceconfig.client.ConfigServiceClient;
import no.cantara.jau.serviceconfig.client.DownloadUtil;
import no.cantara.jau.serviceconfig.dto.DownloadItem;
import no.cantara.jau.serviceconfig.dto.ServiceConfig;
import no.cantara.jau.serviceconfig.dto.ServiceConfigSerializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author <a href="mailto:erik-dev@fjas.no">Erik Drolshammer</a> 2015-07-13.
 */
public class Main {
    private static final Logger log = LoggerFactory.getLogger(Main.class);

    private static final String CONFIG_SERVICE_URL_KEY = "configServiceUrl";
    private static final String CONFIG_FILENAME = "config_override/config.properties";

    private final ExecutorService worker = Executors.newSingleThreadExecutor();


    //String serviceConfigUrl = "http://localhost:7000/jau/serviceconfig/query?clientid=clientid1";
    public static void main(String[] args) {
        String serviceConfigUrl = getServiceConfigUrlOrExit();

        String workingDirectory = "./";
        final Main main = new Main();
        main.start(serviceConfigUrl, workingDirectory);
    }


    private static String getServiceConfigUrlOrExit() {
        String serviceConfigUrl = null;
        final Properties properties = new Properties();
        try {
            properties.load(Main.class.getClassLoader().getResourceAsStream(CONFIG_FILENAME));
            serviceConfigUrl = properties.getProperty(CONFIG_SERVICE_URL_KEY);
        } catch (NullPointerException|IOException e) {
            log.debug("Could not load {} from classpath due to {}: {}.", CONFIG_FILENAME, e.getClass().getSimpleName(), e.getMessage());
        }

        if (serviceConfigUrl == null) {
            //-DconfigServiceUrl=http://localhost:7000/jau/serviceconfig/query?clientid=clientid1
            serviceConfigUrl = System.getProperty(CONFIG_SERVICE_URL_KEY);
        }

        if (serviceConfigUrl == null) {
            log.error("Application cannot start! {} not set in {} or as property (-DconfigServiceUrl=http://localhost:7000/jau/serviceconfig/query?clientid=clientid1).",
                    CONFIG_SERVICE_URL_KEY, CONFIG_FILENAME);
            System.exit(1);
        }
        return serviceConfigUrl;
    }

    /**
     * Fetch ServiceConfig
     * Parse ServiceConfig
     * Check changedTimestamp
     * Download
     * Stop existing service if running
     * Start new service
     *
     * @param serviceConfigUrl  url to service config for this service
     * @param workingDirectory
     */
    public void start(String serviceConfigUrl, String workingDirectory) {
        String response = null;
        try {
            response = ConfigServiceClient.fetchServiceConfig(serviceConfigUrl);
            log.trace("fetchServiceConfig: serviceConfig={}", response);
        } catch (Exception e) {
            log.error("fetchServiceConfig failed with serviceConfigUrl={} Exiting.", serviceConfigUrl, e);
            System.exit(2);
        }

        //Parse
        ServiceConfig serviceConfig = ServiceConfigSerializer.fromJson(response);
        log.debug("{}", serviceConfig);


        //check changedTimestamp

        //Download
        Path path = null;
        for (DownloadItem downloadItem : serviceConfig.getDownloadItems()) {
            log.debug("Downloading {}", downloadItem);
            path = DownloadUtil.downloadFile(downloadItem, workingDirectory);
        }

        //Stop existing service if running


        //Start new service
        //String jarPath = path.toString();
        //new JarProcess("-DIAM_MODE=DEV", jarPath).run();
        //String commandAsString = "java -DIAM_MODE=DEV -jar" + path.getFileName().toString();
        String commandAsString = serviceConfig.getStartServiceScript();
        String[] command = commandAsString.split("\\s+");
        Future<?> future = worker.submit(new ApplicationProcess(workingDirectory, command));
    }
}

@@ -2833,24 +2833,85 @@
 irectory) %7B%0A
+        // https://github.com/Cantara/ConfigService/issues/3%0A
         Stri
@@ -3448,16 +3448,80 @@
 imestamp
+%0A        // https://github.com/Cantara/Java-Auto-Update/issues/5
 %0A%0A      
@@ -3823,18 +3823,16 @@
 running%0A
-%0A%0A
         
@@ -3837,223 +3837,91 @@
   //
-Start new service%0A        //String jarPath = path.toString();%0A        //new JarProcess(%22-DIAM_MODE=DEV%22, jarPath).run();%0A        //String commandAsString = %22java -DIAM_MODE=DEV -jar%22 + path.getFileName().toString();
+ https://github.com/Cantara/Java-Auto-Update/issues/4%0A%0A%0A        //Start new service
 %0A   

Add links to further improvements as comments in Main.