package com.hartwig.healthchecks;

import com.hartwig.healthchecks.common.adapter.HealthCheckAdapter;
import com.hartwig.healthchecks.common.exception.NotFoundException;
import com.hartwig.healthchecks.util.adapter.HealthChecksFlyweight;
import org.apache.commons.cli.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import rx.Observable;
import rx.schedulers.Schedulers;

import java.io.IOException;
import java.util.Collection;

public class HealthChecksApplication {

    private static Logger LOGGER = LogManager.getLogger(HealthChecksApplication.class);

    private static final String RUN_DIRECTORY = "rundir";
    private static final String CHECK_TYPE = "checktype";
    private static final String ALL_CHECKS = "all";

    public static void main(String[] args) throws ParseException, IOException {
        Options options = createOptions();
        CommandLine cmd = createCommandLine(args, options);

        String runDirectory = cmd.getOptionValue(RUN_DIRECTORY);
        String checkType = cmd.getOptionValue(CHECK_TYPE);

        if (runDirectory == null || checkType == null) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("Health-Checks", options);
        } else {
            if (checkType.equals(ALL_CHECKS)) {
                executeAllcheck(runDirectory);
            } else {
                HealthChecksFlyweight flyweight = HealthChecksFlyweight.getInstance();
                try {
                    HealthCheckAdapter healthCheckAdapter = flyweight.getAdapter(checkType);
                    healthCheckAdapter.runCheck(runDirectory);
                } catch (NotFoundException e) {
                    LOGGER.error(e.getMessage());
                }
            }
        }
    }

    private static void executeAllcheck(String runDirectory)  {
        HealthChecksFlyweight flyweight = HealthChecksFlyweight.getInstance();
        Collection<HealthCheckAdapter> adapters = flyweight.getAllAdapters();

        Observable<HealthCheckAdapter> adapterObservable = Observable.from(adapters)
                    .subscribeOn(Schedulers.io())
                    .observeOn(Schedulers.newThread());

        adapterObservable
                    .forEach(healthCheckAdapter ->  healthCheckAdapter.runCheck(runDirectory), error -> error.printStackTrace());
    }

    @NotNull
    private static CommandLine createCommandLine(@NotNull String[] args, @NotNull Options options)
            throws ParseException {
        CommandLineParser parser = new DefaultParser();
        return parser.parse(options, args);
    }

    @NotNull
    private static Options createOptions() {
        Options options = new Options();
        options.addOption(RUN_DIRECTORY, true, "The path containing the data for a single run");
        options.addOption(CHECK_TYPE, true, "The type of check to b executed for a single run");
        return options;
    }
}
@@ -811,311 +811,142 @@
    p
-ublic static void main(String%5B%5D args) throws ParseException, IOException %7B%0A        Options options = createOptions();%0A        CommandLine cmd = createCommandLine(args, options);%0A%0A        String runDirectory = cmd.getOptionValue(RUN_DIRECTORY);%0A        String checkType = cmd.getOptionValue(CHECK_TYPE);%0A
+rivate String runDirectory;%0A    private String checkType;%0A%0A    public HealthChecksApplication(String runDirectory, String checkType) %7B
 %0A   
@@ -950,20 +950,21 @@
         
-if (
+this.
 runDirec
@@ -973,185 +973,114 @@
 ry =
-= null %7C%7C checkType == null) %7B%0A            HelpFormatter formatter = new HelpFormatter();%0A   
+ runDirectory;%0A        this.checkType = checkType;%0A
     
+%7D%0A%0A
     
- formatter.printHelp(%22
+public void process
 Health
--
 Checks
-%22, options);%0A        %7D else %7B%0A   
+() %7B%0A
         
  if 
@@ -1075,17 +1075,16 @@
         
- 
 if (chec
@@ -1123,20 +1123,16 @@
         
-    
 executeA
@@ -1146,36 +1146,32 @@
 (runDirectory);%0A
-    
         %7D else %7B
@@ -1175,36 +1175,32 @@
 e %7B%0A            
-    
 HealthChecksFlyw
@@ -1266,20 +1266,16 @@
         
-    
 try %7B%0A  
@@ -1280,36 +1280,32 @@
                 
- 
-   
 HealthCheckAdapt
@@ -1377,20 +1377,16 @@
         
-    
 healthCh
@@ -1432,20 +1432,16 @@
         
-   
- 
 %7D catch 
@@ -1480,20 +1480,16 @@
         
-    
 LOGGER.e
@@ -1510,20 +1510,16 @@
 age());%0A
-    
         
@@ -1524,30 +1524,16 @@
       %7D%0A
-            %7D%0A
         
@@ -1547,28 +1547,23 @@
 %0A%0A    pr
-ivate static
+otected
  void ex
@@ -1576,16 +1576,25 @@
 llcheck(
+@NotNull 
 String r
@@ -1868,20 +1868,16 @@
         
-    
 .subscri
@@ -1898,20 +1898,16 @@
 s.io())%0A
-    
         
@@ -1977,20 +1977,16 @@
 ervable%0A
-    
         
@@ -2102,32 +2102,701 @@
 race());%0A    %7D%0A%0A
+    public static void main(String%5B%5D args) throws ParseException, IOException %7B%0A        Options options = createOptions();%0A        CommandLine cmd = createCommandLine(args, options);%0A%0A        String runDirectory = cmd.getOptionValue(RUN_DIRECTORY);%0A        String checkType = cmd.getOptionValue(CHECK_TYPE);%0A%0A        if (runDirectory == null %7C%7C checkType == null) %7B%0A            HelpFormatter formatter = new HelpFormatter();%0A            formatter.printHelp(%22Health-Checks%22, options);%0A        %7D%0A%0A        HealthChecksApplication healthChecksApplication = new HealthChecksApplication(runDirectory, checkType);%0A        healthChecksApplication.processHealthChecks();%0A    %7D%0A%0A
     @NotNull%0A   

Improve application by decoupling and adding multi checks computation