package com.airlock.iam.flow.application.configuration.selection.step;

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.api.application.configuration.step.FlowStepConfig;
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")
public final class SelectionOptionConfig 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/authentication/selection/options/retrieve</tt>)\nand is used as the \"id\" parameter to select an option\n(<tt>POST /&lt;loginapp-uri&gt;/rest/public/authentication/selection/options/&lt;id&gt;/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>authentication.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>authentication.selection.options.airlock-2fa</tt>\".</p>",
      optional = false,
      suggestedValues = {
            "AIRLOCK_2FA", "MTAN", "CRONTO", "CRONTO_ACTIVATION", "FIDO", "OATH_OTP", "RADIUS_OTP", "VASCO_OTP", "DEVICE_TOKEN", "MATRIX", "SSO_TICKET"
      }
   )
   private String name;
   @PluginListProperty(description = "Steps of this subflow.", minElements = 1, optional = false)
   private List<FlowStepConfig> 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<FlowStepConfig> getSteps() {
      return this.steps;
   }

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

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

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

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

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

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

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

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

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

      @SuppressFBWarnings(justification = "generated code")
      public SelectionOptionConfig.SelectionOptionConfigBuilder 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 SelectionOptionConfig.SelectionOptionConfigBuilder clearSteps() {
         if (this.steps != null) {
            this.steps.clear();
         }

         return this;
      }

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

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