package com.airlock.iam.saml2.application.configuration.sp;

import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.license.PluginLicenseRestriction;
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;
import java.util.Optional;

@Plugin(
   description = "<p>Import a single-value String attribute from a SAML 2.0 assertion into a flow session.</p><p>If the provided attribute is multi-valued, the <i>last</i> value is used (for backward compatibility) and a warning is logged (if there are multiple attributes with the same name, the last value of the last attribute is used).</p><p>An imported attribute can be used afterwards in any plugin that uses a String value provider by configuring a 'Generic Session Attribute String Provider'.</p>",
   displayName = "SAML 2.0 Assertion String Attribute Importer"
)
@PluginLicenseRestriction(moduleAffiliation = "SamlSp")
public final class Saml2SpAssertionStringAttributeImportConfig {
   @StringProperty(description = "Name of the SAML attribute in the assertion to import.", optional = false, minLength = 1, exampleValues = "email")
   private String attributeName;
   public static final String ATTRIBUTE_NAME_PROPERTY = "attributeName";
   @StringProperty(
      description = "Key under which the value of the imported attribute will be available in the flow session. If none is configured, the name of the attribute will be used as key.",
      optional = true
   )
   private String key;
   public static final String FLOW_KEY_PROPERTY = "key";
   @BooleanProperty(
      description = "Fail if the SAML 2.0 assertion does not contain the configured attribute or the value provided is blank. When false, no value will be imported in such a case.",
      defaultValue = "false",
      optional = true
   )
   private boolean failIfEmpty;

   public String getKey() {
      return Optional.ofNullable(this.key).orElse(this.attributeName);
   }

   public String getAttributeName() {
      return this.attributeName;
   }

   public boolean isFailIfEmpty() {
      return this.failIfEmpty;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public Saml2SpAssertionStringAttributeImportConfig(String attributeName, String key, boolean failIfEmpty) {
      this.attributeName = attributeName;
      this.key = key;
      this.failIfEmpty = failIfEmpty;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public static class Saml2SpAssertionStringAttributeImportConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String attributeName;
      @SuppressFBWarnings(justification = "generated code")
      private String key;
      @SuppressFBWarnings(justification = "generated code")
      private boolean failIfEmpty;

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

      @SuppressFBWarnings(justification = "generated code")
      public Saml2SpAssertionStringAttributeImportConfig.Saml2SpAssertionStringAttributeImportConfigBuilder attributeName(String attributeName) {
         this.attributeName = attributeName;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public Saml2SpAssertionStringAttributeImportConfig.Saml2SpAssertionStringAttributeImportConfigBuilder key(String key) {
         this.key = key;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public Saml2SpAssertionStringAttributeImportConfig.Saml2SpAssertionStringAttributeImportConfigBuilder failIfEmpty(boolean failIfEmpty) {
         this.failIfEmpty = failIfEmpty;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public Saml2SpAssertionStringAttributeImportConfig build() {
         return new Saml2SpAssertionStringAttributeImportConfig(this.attributeName, this.key, this.failIfEmpty);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "Saml2SpAssertionStringAttributeImportConfig.Saml2SpAssertionStringAttributeImportConfigBuilder(attributeName="
            + this.attributeName
            + ", key="
            + this.key
            + ", failIfEmpty="
            + this.failIfEmpty
            + ")";
      }
   }
}
