package com.airlock.iam.flow.shared.application.configuration.condition;

import com.airlock.iam.flow.api.application.configuration.selection.FlowConditionConfig;
import com.airlock.iam.flow.shared.application.configuration.valueprovider.RoleProviderConfig;
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.RegexProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.List;
import java.util.regex.Pattern;

@Plugin(
   description = "Condition that is fulfilled if the user has at least one role that matches the configured regular expression. Note: If the user does not exist (i.e., is not identified) an exception is thrown (configuration error).",
   displayName = "Has Matching Role"
)
public class RoleMatchingConditionConfig implements FlowConditionConfig {
   @PluginListProperty(description = "Role Providers which provide the roles against which the role pattern is checked.", minElements = 1, optional = false)
   private List<RoleProviderConfig> roleProviders;
   @RegexProperty(
      description = "Pattern that is checked against the user's roles. If the user has at least one role that matches the pattern, the condition evaluates to true.",
      optional = false,
      exampleValues = {"WEAK", "(admin|operator)", "developer.*"}
   )
   private Pattern rolePattern;

   public List<RoleProviderConfig> getRoleProviders() {
      return this.roleProviders;
   }

   public Pattern getRolePattern() {
      return this.rolePattern;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public RoleMatchingConditionConfig(List<RoleProviderConfig> roleProviders, Pattern rolePattern) {
      this.roleProviders = roleProviders;
      this.rolePattern = rolePattern;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public static class RoleMatchingConditionConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private List<RoleProviderConfig> roleProviders;
      @SuppressFBWarnings(justification = "generated code")
      private Pattern rolePattern;

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

      @SuppressFBWarnings(justification = "generated code")
      public RoleMatchingConditionConfig.RoleMatchingConditionConfigBuilder roleProviders(List<RoleProviderConfig> roleProviders) {
         this.roleProviders = roleProviders;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RoleMatchingConditionConfig.RoleMatchingConditionConfigBuilder rolePattern(Pattern rolePattern) {
         this.rolePattern = rolePattern;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RoleMatchingConditionConfig build() {
         return new RoleMatchingConditionConfig(this.roleProviders, this.rolePattern);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "RoleMatchingConditionConfig.RoleMatchingConditionConfigBuilder(roleProviders="
            + this.roleProviders
            + ", rolePattern="
            + this.rolePattern
            + ")";
      }
   }
}
