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

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.RegexProperty;
import com.airlock.iam.plugin.api.annotation.property.RegexProperty.CaseSensitivitySelector;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.regex.Pattern;

@Plugin(description = "A matcher that allows to match one or more scopes in the context of OAuth2 / OpenID Connect.", displayName = "OAuth 2.0 Scope Matcher")
@PluginLicenseRestriction(moduleAffiliation = "OAuthServer")
public final class OAuth2ScopeMatcherConfig {
   @RegexProperty(
      description = "The regular expression the scope name is matched against.",
      optional = false,
      exampleValues = {"write", "users:write", ".*:write", "users:.*"},
      caseSensitivity = CaseSensitivitySelector.CONFIGURED_BY_PROPERTY,
      caseSensitivityProperty = "caseSensitive"
   )
   private Pattern scopeNamePattern;
   @BooleanProperty(
      description = "If enabled, the case of characters is considered when matching the scope name against the pattern.",
      optional = true,
      defaultValue = "true"
   )
   private boolean caseSensitive;

   public Pattern compile() {
      return Pattern.compile(this.scopeNamePattern.pattern(), this.caseSensitive ? 0 : 2);
   }

   public Pattern getScopeNamePattern() {
      return this.scopeNamePattern;
   }

   public boolean isCaseSensitive() {
      return this.caseSensitive;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public OAuth2ScopeMatcherConfig(Pattern scopeNamePattern, boolean caseSensitive) {
      this.scopeNamePattern = scopeNamePattern;
      this.caseSensitive = caseSensitive;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public static class OAuth2ScopeMatcherConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private Pattern scopeNamePattern;
      @SuppressFBWarnings(justification = "generated code")
      private boolean caseSensitive;

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

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ScopeMatcherConfig.OAuth2ScopeMatcherConfigBuilder scopeNamePattern(Pattern scopeNamePattern) {
         this.scopeNamePattern = scopeNamePattern;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ScopeMatcherConfig.OAuth2ScopeMatcherConfigBuilder caseSensitive(boolean caseSensitive) {
         this.caseSensitive = caseSensitive;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ScopeMatcherConfig build() {
         return new OAuth2ScopeMatcherConfig(this.scopeNamePattern, this.caseSensitive);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "OAuth2ScopeMatcherConfig.OAuth2ScopeMatcherConfigBuilder(scopeNamePattern="
            + this.scopeNamePattern
            + ", caseSensitive="
            + this.caseSensitive
            + ")";
      }
   }
}
