package com.airlock.iam.authentication.application.configuration.idpropagation;

import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.property.BooleanProperty;
import com.airlock.iam.plugin.api.annotation.property.StringProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

@Plugin(description = "Provides a username from a context data field.", displayName = "Context Data Username Provider")
public class ContextDataUsernameProviderConfig implements UsernameProviderConfig {
   @StringProperty(
      description = "Name of the context data property from which the username is taken.",
      exampleValues = {"email", "applA-name"},
      optional = false
   )
   private String propertyName;
   @BooleanProperty(
      description = "If enabled, it is mandatory that the context data field be non-empty, otherwise an exception is thrown at identity propagation time. If disabled and the context data field is empty, then no username is supplied and subsequent username providers are asked to supply a name (or the authenticated user ID is used if no provider supplies a username).",
      optional = true,
      defaultValue = "false"
   )
   private boolean mandatory;

   public String getPropertyName() {
      return this.propertyName;
   }

   public boolean isMandatory() {
      return this.mandatory;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public ContextDataUsernameProviderConfig(String propertyName, boolean mandatory) {
      this.propertyName = propertyName;
      this.mandatory = mandatory;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public static class ContextDataUsernameProviderConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String propertyName;
      @SuppressFBWarnings(justification = "generated code")
      private boolean mandatory;

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

      @SuppressFBWarnings(justification = "generated code")
      public ContextDataUsernameProviderConfig.ContextDataUsernameProviderConfigBuilder propertyName(String propertyName) {
         this.propertyName = propertyName;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ContextDataUsernameProviderConfig.ContextDataUsernameProviderConfigBuilder mandatory(boolean mandatory) {
         this.mandatory = mandatory;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ContextDataUsernameProviderConfig build() {
         return new ContextDataUsernameProviderConfig(this.propertyName, this.mandatory);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "ContextDataUsernameProviderConfig.ContextDataUsernameProviderConfigBuilder(propertyName="
            + this.propertyName
            + ", mandatory="
            + this.mandatory
            + ")";
      }
   }
}
