package io.highway.to.urhell;

import io.highway.to.urhell.service.AbstractLeechService;
import io.highway.to.urhell.service.LeechService;
import io.highway.to.urhell.service.ReporterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class CoreEngine {

    private static final Logger LOGGER = LoggerFactory
            .getLogger(CoreEngine.class);

    private static CoreEngine instance;

    private Map<String, LeechService> leechPluginRegistry = new HashMap<String, LeechService>();
    private Set<ReporterService> reporterPluginRegistry = new HashSet<ReporterService>();

    private CoreEngine() {
        // nothing
    }

    public static CoreEngine getInstance() {
        if (instance == null) {
            synchronized (CoreEngine.class) {
                if (instance == null) {
                    instance = new CoreEngine();
                    instance.registerPlugins();
                    instance.runPluginsTriggeredAtStartup();
                }
            }
        }
        return instance;
    }

    private void runPluginsTriggeredAtStartup() {
        for (LeechService leechService : leechPluginRegistry.values()) {
            if (leechService.isTriggeredAtStartup()) {
                leechService.receiveData(null);
            }
        }
    }

    public void leech() {
        for (ReporterService reporterService : reporterPluginRegistry) {
            for (LeechService leechService : leechPluginRegistry.values()) {
                reporterService.report(leechService.getFrameworkInformations());
            }
        }
    }

    public Collection<LeechService> getLeechServiceRegistered() {
        return leechPluginRegistry.values();
    }

    private void registerPlugins() {
        autoDiscoverLeechPlugins();
        autoDiscoverReporterPlugins();
    }

    public LeechService getFramework(String frameworkName) {
        return leechPluginRegistry.get(frameworkName);
    }

    private void autoDiscoverLeechPlugins() {
        Set<AbstractLeechService> leechServices = PluginUtils.autodiscoverPlugin(AbstractLeechService.class);
        for (AbstractLeechService leechService : leechServices) {
            leechPluginRegistry.put(leechService.getFrameworkInformations().getFrameworkName(), leechService);
        }
    }

    private void autoDiscoverReporterPlugins() {
        reporterPluginRegistry = PluginUtils.autodiscoverPlugin(ReporterService.class);
    }

}


@@ -1107,271 +1107,8 @@
 %0D%0A%0D%0A
-    private void runPluginsTriggeredAtStartup() %7B%0D%0A        for (LeechService leechService : leechPluginRegistry.values()) %7B%0D%0A            if (leechService.isTriggeredAtStartup()) %7B%0D%0A                leechService.receiveData(null);%0D%0A            %7D%0D%0A        %7D%0D%0A    %7D%0D%0A
 %0D%0A  
@@ -1527,108 +1527,161 @@
 %0D%0A%0D%0A
+%0D%0A
     p
-rivate void registerPlugins() %7B%0D%0A        autoDiscoverLeechPlugins();%0D%0A        autoDiscoverRepor
+ublic LeechService getFramework(String frameworkName) %7B%0D%0A        return leechPluginRegistry.get(frameworkName);%0D%0A    %7D%0D%0A%0D%0A%0D%0A    private void regis
 terP
@@ -1692,134 +1692,85 @@
 ns()
-;
+ %7B
 %0D%0A    
-%7D%0D%0A%0D%0A    public LeechService getFramework(String frameworkName) %7B%0D%0A        return leechPluginRegistry.get(frameworkName
+    autoDiscoverLeechPlugins();%0D%0A        autoDiscoverReporterPlugins(
 );%0D%0A
@@ -2283,13 +2283,278 @@
    %7D%0D%0A%0D%0A
+    private void runPluginsTriggeredAtStartup() %7B%0D%0A        for (LeechService leechService : leechPluginRegistry.values()) %7B%0D%0A            if (leechService.isTriggeredAtStartup()) %7B%0D%0A                leechService.receiveData(null);%0D%0A            %7D%0D%0A        %7D%0D%0A    %7D%0D%0A%0D%0A
 %7D%0D%0A%0D%0A

reorder methods