package com.airlock.iam.flow.api.application.configuration.sequence;

import com.airlock.iam.flow.api.application.configuration.step.FlowStepConfig;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.property.PluginListProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Plugin(description = "Configuration of a sequence of flow steps.")
public final class FlowStepSequence {
   @PluginListProperty(description = "Steps of the flow.", minElements = 1, optional = false)
   private List<FlowStepConfig> steps;

   public List<FlowStepConfig> getSteps() {
      return this.steps;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public FlowStepSequence(List<FlowStepConfig> steps) {
      this.steps = steps;
   }

   @SuppressFBWarnings(justification = "generated code")
   public FlowStepSequence() {
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class FlowStepSequenceBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<FlowStepConfig> steps;

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

      @SuppressFBWarnings(justification = "generated code")
      public FlowStepSequence.FlowStepSequenceBuilder step(FlowStepConfig step) {
         if (this.steps == null) {
            this.steps = new ArrayList<>();
         }

         this.steps.add(step);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public FlowStepSequence.FlowStepSequenceBuilder steps(Collection<? extends FlowStepConfig> steps) {
         if (steps == null) {
            throw new NullPointerException("steps cannot be null");
         }

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

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

      @SuppressFBWarnings(justification = "generated code")
      public FlowStepSequence.FlowStepSequenceBuilder clearSteps() {
         if (this.steps != null) {
            this.steps.clear();
         }

         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "FlowStepSequence.FlowStepSequenceBuilder(steps=" + this.steps + ")";
      }
   }
}
