package com.airlock.iam.servicecontainer.app.application.configuration;

import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.property.RegexProperty;
import com.airlock.iam.plugin.api.annotation.property.StringProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.regex.Pattern;

@Plugin(
   description = "Rule based on regular expression patterns and substitutions used to transform user roles before processing them.",
   displayName = "Role Transformation Rule (Radius)",
   typeShortName = "RadiusRoleTransformationRule"
)
public final class RoleTransformationRuleConfig {
   @RegexProperty(
      description = "Regular expression pattern of the rule. Each role that matches the regular expression is transformed using the corresponding template.",
      optional = false,
      exampleValues = "([^:]*).*",
      matchAgainstWholeInput = false
   )
   private Pattern pattern;
   @StringProperty(
      description = "Template expression pattern of the rule. Each role that matches the regular expression is transformed using the corresponding template.",
      optional = false,
      exampleValues = "$1"
   )
   private String template;

   public String transformRole(String role) {
      return this.pattern.matcher(role).replaceAll(this.template);
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public RoleTransformationRuleConfig(Pattern pattern, String template) {
      this.pattern = pattern;
      this.template = template;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class RoleTransformationRuleConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private Pattern pattern;
      @SuppressFBWarnings(justification = "generated code")
      private String template;

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

      @SuppressFBWarnings(justification = "generated code")
      public RoleTransformationRuleConfig.RoleTransformationRuleConfigBuilder pattern(Pattern pattern) {
         this.pattern = pattern;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RoleTransformationRuleConfig.RoleTransformationRuleConfigBuilder template(String template) {
         this.template = template;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RoleTransformationRuleConfig build() {
         return new RoleTransformationRuleConfig(this.pattern, this.template);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "RoleTransformationRuleConfig.RoleTransformationRuleConfigBuilder(pattern=" + this.pattern + ", template=" + this.template + ")";
      }
   }
}
