package org.pageseeder.ox.util;

import org.pageseeder.ox.api.StepInfo;
import org.pageseeder.ox.core.PackageData;

import java.io.File;

/**
 * The type Step utils.
 *
 * @author ccabral
 * @since 15 January 2021
 */
public class StepUtils {
  /**
   * Get the input from the step definition, but if it is empty then it uses the file uploaded.
   * It accepts glob pattern.
   * @param data
   * @param info
   * @return
   */
  public static File getInput(PackageData data, StepInfo info) {
    // input file
    String input = info.getParameter("input", info.input());

    File finput = null;
    if (!StringUtils.isBlank(input)) {
      finput = data.getFile(input);
    }
    return finput;
  }

  /**
   * Get the output from step definition. If does not find it, then it gets the parent folder of the input.
   * If the input is null then get the package folder.
   * It accepts glob pattern.
   *
   * @param data
   * @param info
   * @param input
   * @return
   */
  public static File getOutput(PackageData data, StepInfo info, File input) {
    // output file
    String output = info.getParameter("output", info.output());
    File foutput = null;
    if (StringUtils.isBlank(output)) {
      if (input != null) {
        foutput = input.getParentFile();
      } else {
        foutput = data.directory();
      }
    } else {
      foutput = data.getFile(output);
    }
    return foutput;
  }

  /**
   * Get the parameter from step definition, if it is not found then gets from the request parameter.
   * Otherwise returns the fallback.
   *
   * @param data PackageData
   * @param info StepInfo
   * @param parameterName The name of the parameter to get from step info or package data
   * @param fallback default value.
   * @return
   */
  public static String getParameter(PackageData data, StepInfo info, String parameterName, String fallback) {
    String parameter = info.getParameter("naming-pattern", data.getParameter("naming-pattern"));

    return !StringUtils.isBlank(parameter) ? parameter : fallback;
  }
}

@@ -1905,32 +1905,29 @@
 rameter(
-%22naming-pattern%22
+parameterName
 , data.g
@@ -1942,24 +1942,21 @@
 ter(
-%22naming-pattern%22
+parameterName
 ));%0A
@@ -2026,11 +2026,685 @@
 ack;%0A  %7D
+%0A%0A  /**%0A   * Get the parameter from step definition, if it is not found then gets from the request parameter.%0A   * Otherwise returns the fallback.%0A   *%0A   * @param data PackageData%0A   * @param info StepInfo%0A   * @param parameterName The name of the parameter to get from step info or package data%0A   * @param fallback default value.%0A   * @return%0A   */%0A  public static int getParameterInt(PackageData data, StepInfo info, String parameterName, int fallback) %7B%0A    String parameter = getParameter(data, info, parameterName, String.valueOf(fallback));%0A    try %7B%0A      return Integer.parseInt(parameter);%0A    %7D catch (NumberFormatException ex) %7B%0A      return fallback;%0A    %7D%0A  %7D
 %0A%7D%0A

add getParameterInt to StepUtils