package com.airlock.iam.flow.domain.model.statemachine.spring;

import com.airlock.iam.flow.domain.model.event.StateMachineFlowEvent;
import com.airlock.iam.flow.domain.model.statemachine.FlowState;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.springframework.statemachine.StateContext;
import org.springframework.statemachine.action.Action;

public class SequentialAction implements Action<FlowState, StateMachineFlowEvent> {
   private List<Action<FlowState, StateMachineFlowEvent>> actions;

   public void execute(StateContext<FlowState, StateMachineFlowEvent> context) {
      this.actions.stream().forEachOrdered(action -> action.execute(context));
   }

   @SuppressFBWarnings(justification = "generated code")
   SequentialAction(List<Action<FlowState, StateMachineFlowEvent>> actions) {
      this.actions = actions;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static SequentialAction.SequentialActionBuilder builder() {
      return new SequentialAction.SequentialActionBuilder();
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class SequentialActionBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<Action<FlowState, StateMachineFlowEvent>> actions;

      @SuppressFBWarnings(justification = "generated code")
      SequentialActionBuilder() {
      }

      @SuppressFBWarnings(justification = "generated code")
      public SequentialAction.SequentialActionBuilder action(Action<FlowState, StateMachineFlowEvent> action) {
         if (this.actions == null) {
            this.actions = new ArrayList<>();
         }

         this.actions.add(action);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SequentialAction.SequentialActionBuilder actions(Collection<? extends Action<FlowState, StateMachineFlowEvent>> actions) {
         if (actions == null) {
            throw new NullPointerException("actions cannot be null");
         }

         if (this.actions == null) {
            this.actions = new ArrayList<>();
         }

         this.actions.addAll(actions);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SequentialAction.SequentialActionBuilder clearActions() {
         if (this.actions != null) {
            this.actions.clear();
         }

         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SequentialAction build() {
         return new SequentialAction(switch (this.actions == null ? 0 : this.actions.size()) {
            case 0 -> Collections.emptyList();
            case 1 -> Collections.singletonList(this.actions.get(0));
            default -> Collections.unmodifiableList(new ArrayList<>(this.actions));
         });
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "SequentialAction.SequentialActionBuilder(actions=" + this.actions + ")";
      }
   }
}
