package org.pageseeder.ox.step;

import org.pageseeder.ox.api.Result;
import org.pageseeder.ox.api.Step;
import org.pageseeder.ox.core.Model;
import org.pageseeder.ox.core.PackageData;
import org.pageseeder.ox.core.StepInfoImpl;
import org.pageseeder.ox.util.StringUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ccabral
 * @since 08 January 2021
 */
public class StepSimulator {
  private final Model model;
  private final PackageData data;

  /**
   *
   * The
   *
   * @param modelName          The model name will be used just to create a model. It will not use to read a
   *                           configuration.
   * @param uploadedFile       This does not necessary need to be the uploaded file, it can be the file/folder that will
   *                           be used as input for the step. This file is used to create the PackageData.
   * @oaram requestParameters  These are the parameters sent in the HTTP request. They are also used to create the
   *                           PackageData.
   */
  public StepSimulator(String modelName, File uploadedFile, Map<String, String> requestParameters) {
    if (StringUtils.isBlank(modelName)) {
      throw new IllegalArgumentException("The model name cannot be blank (null or empty)");
    }

    if (uploadedFile == null) {
      throw new IllegalArgumentException("The uploaded file cannot be null.");
    }

    this.model = new Model(modelName);
    this.data = PackageData.newPackageData(modelName, uploadedFile);

    if (requestParameters != null) {
      for (Map.Entry<String, String> parameter: requestParameters.entrySet()) {
        this.data.setParameter(parameter.getKey(), parameter.getValue());
      }
    }
  }

  /**
   *
   * @param step         is an instance of the step we are going to process.
   * @param input        The input file/folder for this step. It can be null if it is the uploadedFile used in this class
   *                     constructor.
   * @param output       The output file/folder for this step. It can be null.
   * @param stepName     The step name cannot be null or empty.
   * @param parameters   The parameters that exclusively belongs to this step.
   * @return
   */
  public Result process (Step step, String input, String output, String stepName, Map<String, String> parameters) {
    if (StringUtils.isBlank(stepName)) {
      throw new IllegalArgumentException("The step name cannot be blank (null or empty)");
    }

    if (input == null) {
      input = this.data.getPath(this.data.getOriginal());
    }

    // use input as output if output is null
    if (output == null) {
      output = input;
    }

    if (parameters == null) {
      parameters = new HashMap<String, String>();
    }

    // step info
    StepInfoImpl info = new StepInfoImpl(this.data.id(), stepName, input, output, parameters);
    // process the step
    return step.process(this.model, this.data, info);
  }
}

@@ -2733,27 +2733,13 @@
 Map%3C
-String, String
 %3E();%0A
+
     
@@ -2853,16 +2853,17 @@
 eters);%0A
+%0A
     // p
@@ -2878,16 +2878,16 @@
 he step%0A
-
     retu
@@ -2935,11 +2935,152 @@
 fo);%0A  %7D
+%0A%0A  /**%0A   *%0A   * @return The directory containing the package%0A   */%0A  public File getDataDirectory() %7B%0A    return this.data.directory();%0A  %7D
 %0A%7D%0A

add method StepSimulator.getDataDirectory