package com.airlock.iam.common.application.configuration.event;

import com.airlock.iam.plugin.api.annotation.config.ConfigurationValidator;
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 com.airlock.iam.plugin.api.config.ConfigurationContext;
import com.airlock.iam.plugin.api.config.ConfigurationEnvironment;
import com.airlock.iam.plugin.api.config.Environment;
import com.airlock.iam.plugin.api.config.PluginConfiguration;
import com.airlock.iam.plugin.api.exception.PropertyConfigurationException;
import com.airlock.iam.plugin.api.valuereader.RegexPropertyValueReader;
import com.airlock.iam.plugin.api.valuereader.StringPropertyValueReader;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;

@Plugin(
   description = "<p>Event that is published when a user is created through the Adminapp or Loginapp.</p><p>Use the \"Matching Pattern\" and the \"Context Data Key\" properties to only handle events for users with specific context-data values.</p>",
   displayName = "User Created"
)
public final class UserCreatedSubscribedEventConfig implements LoginappUserSubscribedEventConfig, AdminappUserSubscribedEventConfig {
   @RegexProperty(
      description = "<p>A pattern for matching the user context data value.</p><p>To assert an empty or absent context data item, use the pattern '^$'.</p>",
      optional = true,
      exampleValues = {".*", "write_.*", "^$"}
   )
   private Pattern matchingPattern;
   static final String MATCHING_PATTERN_FIELD = "matchingPattern";
   @StringProperty(
      description = "<p>The key to identify the value in the context data.</p><p>Note: per default, the username is not contained in context data.</p>",
      optional = true,
      exampleValues = {"surname", "email"}
   )
   private String contextDataKey;
   static final String CONTEXT_DATA_KEY_FIELD = "contextDataKey";

   public Pattern getMatchingPattern() {
      return this.matchingPattern;
   }

   public String getContextDataKey() {
      return this.contextDataKey;
   }

   @ConfigurationValidator
   static void checkPropertyCombination(
      PluginConfiguration config, ConfigurationEnvironment env, ConfigurationContext context, Environment validationEnvironment
   ) throws PropertyConfigurationException {
      String matchingPattern = RegexPropertyValueReader.effectiveRegexConfigValue(config, "matchingPattern", env, context);
      String contextDataKey = StringPropertyValueReader.effectiveStringConfigValue(config, "contextDataKey", env, context);
      if (StringUtils.isBlank(matchingPattern) ^ StringUtils.isBlank(contextDataKey)) {
         throw new PropertyConfigurationException(
            config,
            env,
            context,
            "\"Matching Pattern\" and \"Context Data Key\" have to both be either set or unset.",
            new String[]{"matchingPattern", "contextDataKey"}
         );
      }
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public UserCreatedSubscribedEventConfig(Pattern matchingPattern, String contextDataKey) {
      this.matchingPattern = matchingPattern;
      this.contextDataKey = contextDataKey;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class UserCreatedSubscribedEventConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private Pattern matchingPattern;
      @SuppressFBWarnings(justification = "generated code")
      private String contextDataKey;

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

      @SuppressFBWarnings(justification = "generated code")
      public UserCreatedSubscribedEventConfig.UserCreatedSubscribedEventConfigBuilder matchingPattern(Pattern matchingPattern) {
         this.matchingPattern = matchingPattern;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserCreatedSubscribedEventConfig.UserCreatedSubscribedEventConfigBuilder contextDataKey(String contextDataKey) {
         this.contextDataKey = contextDataKey;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserCreatedSubscribedEventConfig build() {
         return new UserCreatedSubscribedEventConfig(this.matchingPattern, this.contextDataKey);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "UserCreatedSubscribedEventConfig.UserCreatedSubscribedEventConfigBuilder(matchingPattern="
            + this.matchingPattern
            + ", contextDataKey="
            + this.contextDataKey
            + ")";
      }
   }
}
