package com.airlock.iam.oauth2.application.configuration.claims;

import com.airlock.iam.common.api.application.configuration.valueprovider.StringValueProviderConfig;
import com.airlock.iam.oauth2.api.application.configuration.claims.ClaimConditionConfig;
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.PluginProperty;
import com.airlock.iam.plugin.api.annotation.property.RegexProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.regex.Pattern;

@Plugin(
   description = "A custom boolean claim whose value is determined by whether the value of a String Provider matches a specific regex.\n",
   displayName = "Dynamic Boolean Custom Claim"
)
@PluginLicenseRestriction(moduleAffiliation = "OAuthServer")
public final class CustomDynamicBooleanClaimConfig extends PersistedCustomClaimConfig {
   @PluginProperty(
      description = "Value provider used to get a string value to be matched against the regular expression below.\n<p>Note: If the string value provider returns null, it is interpreted as an empty string. In case the Regex matches empty strings, the value would be true.</p>\n",
      optional = false
   )
   private StringValueProviderConfig stringProvider;
   @RegexProperty(
      description = "The regex used to match the String from the String Provider. A match results in a boolean value of true; otherwise, false.",
      optional = false,
      exampleValues = {"true", ".*TENANT.*", "^$"}
   )
   private Pattern regex;

   public CustomDynamicBooleanClaimConfig(String claimName, ClaimConditionConfig claimCondition, StringValueProviderConfig stringProvider, Pattern regex) {
      super(claimName, claimCondition);
      this.stringProvider = stringProvider;
      this.regex = regex;
   }

   public StringValueProviderConfig getStringProvider() {
      return this.stringProvider;
   }

   public Pattern getRegex() {
      return this.regex;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public static class CustomDynamicBooleanClaimConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String claimName;
      @SuppressFBWarnings(justification = "generated code")
      private ClaimConditionConfig claimCondition;
      @SuppressFBWarnings(justification = "generated code")
      private StringValueProviderConfig stringProvider;
      @SuppressFBWarnings(justification = "generated code")
      private Pattern regex;

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

      @SuppressFBWarnings(justification = "generated code")
      public CustomDynamicBooleanClaimConfig.CustomDynamicBooleanClaimConfigBuilder claimName(String claimName) {
         this.claimName = claimName;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CustomDynamicBooleanClaimConfig.CustomDynamicBooleanClaimConfigBuilder claimCondition(ClaimConditionConfig claimCondition) {
         this.claimCondition = claimCondition;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CustomDynamicBooleanClaimConfig.CustomDynamicBooleanClaimConfigBuilder stringProvider(StringValueProviderConfig stringProvider) {
         this.stringProvider = stringProvider;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CustomDynamicBooleanClaimConfig.CustomDynamicBooleanClaimConfigBuilder regex(Pattern regex) {
         this.regex = regex;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CustomDynamicBooleanClaimConfig build() {
         return new CustomDynamicBooleanClaimConfig(this.claimName, this.claimCondition, this.stringProvider, this.regex);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "CustomDynamicBooleanClaimConfig.CustomDynamicBooleanClaimConfigBuilder(claimName="
            + this.claimName
            + ", claimCondition="
            + this.claimCondition
            + ", stringProvider="
            + this.stringProvider
            + ", regex="
            + this.regex
            + ")";
      }
   }
}
