package jetbrains.buildServer.autotools.server;

import java.util.*;
import jetbrains.buildServer.requirements.Requirement;
import jetbrains.buildServer.requirements.RequirementType;
import jetbrains.buildServer.serverSide.PropertiesProcessor;
import jetbrains.buildServer.serverSide.RunType;
import jetbrains.buildServer.serverSide.RunTypeRegistry;
import jetbrains.buildServer.web.openapi.PluginDescriptor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static jetbrains.buildServer.autotools.common.AutotoolsBuildConstants.*;

/**
 * Created on 12.07.2017.
 * @author     : Nadezhda Demina
 */
public class AutotoolsBuildRunType extends RunType{

  /**
   * Descriptor for AutotoolsRunnerPlugin.
   */
  private final PluginDescriptor myPluginDescriptor;
  public AutotoolsBuildRunType(final @NotNull RunTypeRegistry runTypeRegistry, final @NotNull PluginDescriptor descriptor) {
    runTypeRegistry.registerRunType(this);
    myPluginDescriptor = descriptor;
  }

  @Override
  public @NotNull String getType() {
    return TYPE;
  }

  @Override
  public @NotNull String getDisplayName() {
    return DISPLAY_NAME;
  }

  @Override
  public @NotNull String getDescription() {
    return DESCRIPTION;
  }

  @Override
  public @Nullable PropertiesProcessor getRunnerPropertiesProcessor() {
    return null;
  }


  @Override
  public String getEditRunnerParamsJspFilePath() {
    return myPluginDescriptor.getPluginResourcesPath("editAutotoolsBuildRunner.jsp");
  }

  @Override
  public String getViewRunnerParamsJspFilePath() {
    return myPluginDescriptor.getPluginResourcesPath("viewAutotoolsBuildRunner.jsp");
  }

  @Override
  public @NotNull Map<String, String> getDefaultRunnerProperties() {

    final Map<String, String> properties = new HashMap<>();
    properties.put(UI_NEED_AUTORECONF, Boolean.toString(true));
    properties.put(UI_ADDITIONAL_MAKE_PARAMS, "all");
    properties.put(UI_ADDITIONAL_CONF_PARAMS, "");
    properties.put(UI_MAKE_CHECK, "check");
    properties.put(UI_DEJAGNU_XML_REPLACE_AMP, Boolean.toString(false));
    properties.put(UI_DEJAGNU_XML_REPLACE_CONTROLS, Boolean.toString(true));
    return properties;
  }

  @Override
  public @NotNull List<Requirement> getRunnerSpecificRequirements(final @NotNull Map<String, String> runParameters) {
    final List<Requirement> requirements = new ArrayList<>(4);
    requirements.add(new Requirement(TOOL_AUTOCONF, null, RequirementType.EXISTS));
    requirements.add(new Requirement(TOOL_MAKE, null, RequirementType.EXISTS));
    requirements.add(new Requirement(TOOL_TAR, null, RequirementType.EXISTS));
    requirements.add(new Requirement(TOOL_GZIP, null, RequirementType.EXISTS));
    return requirements;
  }
}

@@ -636,16 +636,22 @@
 ina%0A */%0A
+final 
 public c
@@ -806,21 +806,22 @@
 tor;%0A  p
-ublic
+rivate
  Autotoo
@@ -850,16 +850,151 @@
 NotNull 
+PluginDescriptor descriptor) %7B%0A    myPluginDescriptor = descriptor;%0A  %7D%0A%0A  @NotNull%0A  public static RunType getInstance(final @NotNull 
 RunTypeR
@@ -1065,96 +1065,147 @@
 tor)
- 
 %7B%0A    
-runTypeRegistry.registerRunType(this);%0A    myPluginDescriptor = descriptor
+final RunType runType = new AutotoolsBuildRunType(descriptor);%0A    runTypeRegistry.registerRunType(runType);%0A    return runType
 ;%0A  %7D%0A
-%0A
   @O

add factory-method for AutotoolsBuildRunType