package ru.yandex.qatools.allure.data;

import com.google.inject.Guice;
import com.google.inject.Inject;
import ru.yandex.qatools.allure.data.converters.TestCaseConverter;
import ru.yandex.qatools.allure.data.io.Reader;
import ru.yandex.qatools.allure.data.io.ReportWriter;
import ru.yandex.qatools.allure.data.plugins.PluginLoader;
import ru.yandex.qatools.allure.data.plugins.PluginLoaderSpi;
import ru.yandex.qatools.allure.data.plugins.PluginManager;
import ru.yandex.qatools.allure.model.Attachment;
import ru.yandex.qatools.allure.model.TestCaseResult;
import ru.yandex.qatools.commons.model.Environment;

import java.io.File;

/**
 * @author Dmitry Baev charlie@yandex-team.ru
 *         Date: 12.02.15
 */
@SuppressWarnings({"unused"})
public class AllureReportGenerator {

    @Inject
    private Reader<TestCaseResult> testCaseReader;

    @Inject
    private Reader<Attachment> attachmentReader;

    @Inject
    private Reader<Environment> environmentReader;

    @Inject
    private TestCaseConverter converter;

    @Inject
    private PluginManager pluginManager;

    public AllureReportGenerator(final File... inputDirectories) {
        //TODO we should have an ability to specify class loader
        Guice.createInjector(new AppInjector(
                getClass().getClassLoader(),
                inputDirectories
        )).injectMembers(this);

        PluginLoader loader = new PluginLoaderSpi(getClass().getClassLoader());
        this.pluginManager = new PluginManager(loader);
    }

    public void generate(File outputDirectory) {
        ReportWriter writer = new ReportWriter(outputDirectory);

        for (TestCaseResult result : testCaseReader) {
            AllureTestCase testCase = converter.convert(result);
            pluginManager.prepare(testCase);
            pluginManager.process(testCase);
            writer.write(testCase);
        }

        for (Environment environment : environmentReader) {
            pluginManager.prepare(environment);
            pluginManager.process(environment);
        }

        writer.close();
    }
}

@@ -271,129 +271,8 @@
 er;%0A
-import ru.yandex.qatools.allure.data.plugins.PluginLoader;%0Aimport ru.yandex.qatools.allure.data.plugins.PluginLoaderSpi;%0A
 impo
@@ -504,16 +504,41 @@
 io.File;
+%0Aimport java.lang.Thread;
 %0A%0A/**%0A *
@@ -1014,14 +1014,8 @@
 tor(
-final 
 File
@@ -1050,64 +1050,178 @@
     
-//TODO we should have an ability to specify class loader
+this(Thread.currentThread().getContextClassLoader(), inputDirectories);%0A    %7D%0A%0A    public AllureReportGenerator(ClassLoader pluginClassLoader, File... inputDirectories) %7B
 %0A   
@@ -1279,30 +1279,22 @@
         
-getClass().get
+plugin
 ClassLoa
@@ -1296,18 +1296,16 @@
 ssLoader
-()
 ,%0A      
@@ -1367,142 +1367,162 @@
 s);%0A
-%0A        PluginLoader loader = new PluginLoaderSpi(getClass().getClassLoader());%0A        this.pluginManager = new PluginManager(loader
+    %7D%0A%0A    /**%0A     * For testing only%0A     */%0A    AllureReportGenerator(AppInjector injector) %7B%0A        Guice.createInjector(injector).injectMembers(this
 );%0A 
@@ -1641,16 +1641,96 @@
 ctory);%0A
+        generate(writer);%0A    %7D%0A%0A    public void generate(ReportWriter writer) %7B
 %0A       

add new methods to generator