package jetbrains.buildServer.autotools.agent;

import jetbrains.buildServer.agent.*;
import jetbrains.buildServer.util.EventDispatcher;
import org.jetbrains.annotations.NotNull;
import static jetbrains.buildServer.autotools.common.AutotoolsBuildConstants.*;

/**
 * @author Nadezhda Demina
 */
public final class AutotoolsLifeCycleListener extends AgentLifeCycleAdapter {
  private AutotoolsTestsReporter myTestReporter;

  private final ToolProvidersRegistry myProvidersRegistry;

  AutotoolsLifeCycleListener(final EventDispatcher<AgentLifeCycleListener> dispatcher, final ToolProvidersRegistry toolProvidersRegistry) {
    dispatcher.addListener(this);
    myProvidersRegistry = toolProvidersRegistry;
  }

  @Override
  public void beforeRunnerStart(@NotNull final BuildRunnerContext runner) {
    final RuntestToolProvider runtestToolProvider = (RuntestToolProvider) myProvidersRegistry.findToolProvider(TOOL_RUNTEST);
    if (runtestToolProvider != null){
      runtestToolProvider.setDejagnuParameters(runner);
    }
    myTestReporter = new AutotoolsTestsReporter(System.currentTimeMillis(), runner.getBuild().getBuildLogger(),
                                                runner.getBuild().getCheckoutDirectory().getAbsolutePath() + '/',
                                              Boolean.parseBoolean(runner.getRunnerParameters().get(UI_DEJAGNU_XML_REPLACE_AMP)),
                                                Boolean.parseBoolean(runner.getRunnerParameters().get(UI_DEJAGNU_XML_REPLACE_CONTROLS)));

    myTestReporter.findDejagnu(runner.getBuild().getCheckoutDirectory());
  }



  @Override
  public void runnerFinished(@NotNull final BuildRunnerContext runner, @NotNull final BuildFinishedStatus status) {
    super.runnerFinished(runner, status);
    myTestReporter.searchTestsFiles(runner.getBuild().getCheckoutDirectory());
    myTestReporter.doTestsReport();
  }
}

@@ -363,17 +363,16 @@
 eAdapter
- 
 %7B%0A  priv
@@ -477,16 +477,24 @@
 try;%0A%0A  
+private 
 Autotool
@@ -518,16 +518,189 @@
 r(final 
+ToolProvidersRegistry toolProvidersRegistry) %7B%0A    myProvidersRegistry = toolProvidersRegistry;%0A  %7D%0A%0A  @NotNull%0A  public static AutotoolsLifeCycleListener getInstance(final 
 EventDis
@@ -797,92 +797,224 @@
 try)
- 
 %7B%0A    
-dispatcher.addListener(this);%0A    myProvidersRegistry = toolProvidersRegistry
+final AutotoolsLifeCycleListener autotoolsLifeCycleListener = new AutotoolsLifeCycleListener(toolProvidersRegistry);%0A    dispatcher.addListener(autotoolsLifeCycleListener);%0A    return autotoolsLifeCycleListener
 ;%0A  
@@ -1346,28 +1346,24 @@
 stReporter =
- new
  AutotoolsTe
@@ -1373,16 +1373,28 @@
 Reporter
+.getInstance
 (System.

add factory-method for AutotoolsLifeCycleListener