package com.airlock.iam.publicselfservice.application.configuration.selection;

import com.airlock.iam.flow.api.application.configuration.selection.CommonSelectionOptionConfig;
import com.airlock.iam.flow.api.application.configuration.selection.FlowConditionConfig;
import com.airlock.iam.flow.types.api.application.configuration.step.StepConfigForPublicSelfServiceFlows;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.property.PluginListProperty;
import com.airlock.iam.plugin.api.annotation.property.PluginProperty;
import com.airlock.iam.plugin.api.annotation.property.StringProperty;
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 selectable subflow.", displayName = "Selection Option For Public Self-Service")
public class SelectionOptionConfigForPublicSelfServiceFlows implements CommonSelectionOptionConfig {
   @StringProperty(
      description = "<p>Name of this option.</p>\n<p>This name appears in the list of available options\n(<tt>POST /&lt;loginapp-uri&gt;/rest/public/self-service/selection/options/retrieve</tt>)\nand is used as the \"id\" parameter to select an option\n(<tt>POST /&lt;loginapp-uri&gt;/rest/public/self-service/selection/options/{id}/select</tt>).</p>\n<p>The name is also used in the UI (single-page application) as \"id\" of the options string resource key\n\"<tt>public-self-service.selection.options.&lt;id&gt;</tt>\".</p>\n<p>Note that the name is converted to lowercase and underscores \"_\" are replaced by a hyphen \"-\",\ne.g. the resource key for AIRLOCK_2FA would be \"<tt>public-self-service.selection.options.airlock-2fa</tt>\".</p>",
      optional = false
   )
   private String name;
   @PluginListProperty(description = "Steps of this subflow.", minElements = 1, optional = false)
   private List<StepConfigForPublicSelfServiceFlows> steps;
   @PluginProperty(description = "Defines the condition under which this option can be selected.", optional = false)
   private FlowConditionConfig condition;

   @Override
   public String getName() {
      return this.name;
   }

   @Override
   public List<StepConfigForPublicSelfServiceFlows> getSteps() {
      return this.steps;
   }

   @Override
   public FlowConditionConfig getCondition() {
      return this.condition;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public SelectionOptionConfigForPublicSelfServiceFlows(String name, List<StepConfigForPublicSelfServiceFlows> steps, FlowConditionConfig condition) {
      this.name = name;
      this.steps = steps;
      this.condition = condition;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class SelectionOptionConfigForPublicSelfServiceFlowsBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String name;
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<StepConfigForPublicSelfServiceFlows> steps;
      @SuppressFBWarnings(justification = "generated code")
      private FlowConditionConfig condition;

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

      @SuppressFBWarnings(justification = "generated code")
      public SelectionOptionConfigForPublicSelfServiceFlows.SelectionOptionConfigForPublicSelfServiceFlowsBuilder name(String name) {
         this.name = name;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SelectionOptionConfigForPublicSelfServiceFlows.SelectionOptionConfigForPublicSelfServiceFlowsBuilder step(StepConfigForPublicSelfServiceFlows step) {
         if (this.steps == null) {
            this.steps = new ArrayList<>();
         }

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

      @SuppressFBWarnings(justification = "generated code")
      public SelectionOptionConfigForPublicSelfServiceFlows.SelectionOptionConfigForPublicSelfServiceFlowsBuilder steps(
         Collection<? extends StepConfigForPublicSelfServiceFlows> 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 SelectionOptionConfigForPublicSelfServiceFlows.SelectionOptionConfigForPublicSelfServiceFlowsBuilder clearSteps() {
         if (this.steps != null) {
            this.steps.clear();
         }

         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SelectionOptionConfigForPublicSelfServiceFlows.SelectionOptionConfigForPublicSelfServiceFlowsBuilder condition(FlowConditionConfig condition) {
         this.condition = condition;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SelectionOptionConfigForPublicSelfServiceFlows build() {
         return new SelectionOptionConfigForPublicSelfServiceFlows(this.name, 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));
         }, this.condition);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "SelectionOptionConfigForPublicSelfServiceFlows.SelectionOptionConfigForPublicSelfServiceFlowsBuilder(name="
            + this.name
            + ", steps="
            + this.steps
            + ", condition="
            + this.condition
            + ")";
      }
   }
}
