package org.requirementsascode;

import java.util.Optional;
import java.util.function.Predicate;

import org.requirementsascode.predicate.Anytime;

public class ConditionalStep extends Step {
    private static final long serialVersionUID = 7204738737376844201L;

    ConditionalStep(String stepName, UseCase useCase, Flow useCaseFlow) {
	super(stepName, useCase, useCaseFlow);
    }
    
    public Predicate<UseCaseModelRunner> getPredicate() {
	Predicate<UseCaseModelRunner> predicate;
	Predicate<UseCaseModelRunner> reactWhile = getReactWhile();
	
	if (reactWhile != null) {
	    predicate = reactWhile;
	} else{ 
	    predicate = getFlowPredicate().get();
	}

	return predicate;
    }
    
    public Optional<Predicate<UseCaseModelRunner>> getFlowPredicate() {
	Optional<Predicate<UseCaseModelRunner>> flowPredicate = Optional.empty();
	Optional<Predicate<UseCaseModelRunner>> flowPosition = getFlowPosition();
	Optional<Predicate<UseCaseModelRunner>> when = getWhen();
	
	if (flowPosition.isPresent() || when.isPresent()) {
	    Anytime anytime = new Anytime();
	    Predicate<UseCaseModelRunner> flowPositionOrElseAnytime = flowPosition.orElse(anytime);
	    Predicate<UseCaseModelRunner> whenOrElseAnytime = when.orElse(anytime);
	    flowPredicate = Optional
		    .of(isRunnerInDifferentFlow().and(flowPositionOrElseAnytime).and(whenOrElseAnytime));
	}
	return flowPredicate;
    }
    
    private Predicate<UseCaseModelRunner> isRunnerInDifferentFlow() {
	Predicate<UseCaseModelRunner> isRunnerInDifferentFlow = runner -> runner.getLatestFlow()
		.map(runnerFlow -> !runnerFlow.equals(getFlow())).orElse(true);
	return isRunnerInDifferentFlow;
    }
}

@@ -369,37 +369,33 @@
 aseFlow);%0A    %7D%0A
-    
 %0A
+
     public Predi
@@ -535,26 +535,25 @@
 actWhile();%0A
-%09
 %0A
+
 %09if (reactWh
@@ -607,10 +607,10 @@
 else
-%7B
  
+%7B
 %0A%09  
@@ -674,29 +674,25 @@
 cate;%0A    %7D%0A
-    
 %0A
+
     public O
@@ -964,68 +964,10 @@
 ();%0A
-%09
 %0A%09
-if (flowPosition.isPresent() %7C%7C when.isPresent()) %7B%0A%09    
 Anyt
@@ -992,28 +992,24 @@
 Anytime();%0A%09
-    
 Predicate%3CUs
@@ -1085,20 +1085,16 @@
 time);%0A%09
-    
 Predicat
@@ -1158,20 +1158,16 @@
 time);%0A%09
-    
 flowPred
@@ -1186,15 +1186,8 @@
 onal
-%0A%09%09    
 .of(
@@ -1268,19 +1268,16 @@
 time));%0A
-%09%7D%0A
 %09return 
@@ -1297,20 +1297,16 @@
 ;%0A    %7D%0A
-    
 %0A    pri

Remove unnecessary conditional