package org.requirementsascode.flowposition;

import java.util.Objects;

import org.requirementsascode.FlowStep;
import org.requirementsascode.ModelRunner;
import org.requirementsascode.Step;
import org.requirementsascode.UseCase;

/**
 * Tests whether the specified step was the last step run.
 * 
 * @author b_muth
 *
 */
public class After extends FlowPosition {
  private String stepName;
  private FlowStep step;

  public After(String stepName, UseCase useCase) {
    super(useCase);
    this.stepName = stepName;
  }

  public After(String[] stepNames, UseCase useCase) {
    this(stepNames[0], useCase);
    afterRemainingSteps(stepNames);
  }

  private void afterRemainingSteps(String[] stepNames) {
    for (int i = 1; i < stepNames.length; i++) {
      orAfter(stepNames[i], getUseCase());
    }
  }

  public static After flowStep(FlowStep flowStep) {
    UseCase useCase = flowStep == null ? null : flowStep.getUseCase();
    String stepName = flowStep == null ? null : flowStep.getName();
    After afterFlowStep = new After(stepName, useCase);
    return afterFlowStep;
  }

  @Override
  protected boolean isRunnerAtRightPositionFor(ModelRunner modelRunner) {
    Step latestStepRun = modelRunner.getLatestStep().orElse(null);
    boolean stepWasRunLast = Objects.equals(step, latestStepRun) || isAfterAnyOtherStep(modelRunner);
    return stepWasRunLast;
  }
  
  private boolean isAfterAnyOtherStep(ModelRunner modelRunner) {
    boolean isAfterStep = false;
    for (After afterOtherStep : getAfterOtherSteps()) {
      if (afterOtherStep.test(modelRunner)) {
        isAfterStep = true;
        break;
      }
    }
    return isAfterStep;
  }
  
  public void resolveSteps() {
    if (step == null) {
      this.step = resolveStep();
    }
  }

  private FlowStep resolveStep() {
    FlowStep resolvedStep = null;

    UseCase useCase = getUseCase();
    String stepName = getStepName();
    if (useCase != null && stepName != null) {
      resolvedStep = (FlowStep) useCase.findStep(stepName);
    }

    return resolvedStep;
  }
  
  public final String getStepName() {
    return stepName;
  }
}

@@ -1746,16 +1746,24 @@
 lveStep(
+stepName
 );%0A    %7D
@@ -1799,16 +1799,31 @@
 lveStep(
+String stepName
 ) %7B%0A    
@@ -1893,45 +1893,8 @@
 ();%0A
-    String stepName = getStepName();%0A
     

Refactor After