package jetbrains.buildServer.autotools.agent;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.VisibleForTesting;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.util.text.VersionComparatorUtil;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jetbrains.buildServer.ExecResult;
import jetbrains.buildServer.SimpleCommandLineProcessRunner;
import jetbrains.buildServer.agent.*;
import jetbrains.buildServer.util.EventDispatcher;
import org.jetbrains.annotations.NotNull;
import static jetbrains.buildServer.autotools.common.AutotoolsBuildConstants.*;

/**
 * Created on 26.07.2017.
 * Author     : Nadezhda Demina
 */
public class AutotoolsLifeCycleListener extends AgentLifeCycleAdapter {

  /**
   * Does autotools test report
   */
  RuntestToolProvider myRuntestToolProvider;
  private AutotoolsTestsReporter myTestReporter;
  private ToolProvidersRegistry myProvidersRegistry;
  AutotoolsLifeCycleListener(EventDispatcher<AgentLifeCycleListener> dispatcher, ToolProvidersRegistry toolProvidersRegistry) {
    dispatcher.addListener(this);
    myProvidersRegistry = toolProvidersRegistry;
  }

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

    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();
  }
}

@@ -52,414 +52,27 @@
 ort 
-com.google.common.annotations.GwtCompatible;%0Aimport com.google.common.annotations.VisibleForTesting;%0Aimport com.intellij.execution.configurations.GeneralCommandLine;%0Aimport com.intellij.util.text.VersionComparatorUtil;%0Aimport java.util.UUID;%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0Aimport jetbrains.buildServer.ExecResult;%0Aimport jetbrains.buildServer.SimpleCommandLineProcessRunner
+java.io.IOException
 ;%0Aim
@@ -1452,21 +1452,16 @@
 (UI_
-NEED_
 DEJAGNU_
 VALI
@@ -1460,17 +1460,160 @@
 GNU_
-VALID_XML
+XML_REPLACE_AMP)),%0A                                                Boolean.parseBoolean(runner.getRunnerParameters().get(UI_DEJAGNU_XML_REPLACE_CONTROLS
 )));

add xml dejagnu flags in call AutotoolsTestsReporter constructor in DejagnuXmlPasringTest