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

import com.airlock.iam.common.domain.model.user.UserToContextDataTransformer;
import com.airlock.iam.core.application.configuration.contextdata.StringContextDataItemNameConfig;
import com.airlock.iam.core.application.configuration.user.UserToPropagatedNameTransformerConfig;
import com.airlock.iam.core.domain.model.user.UserToPropagatedNameTransformer;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.property.BooleanProperty;
import com.airlock.iam.plugin.api.annotation.property.PluginProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

@Plugin(
   description = "This user name transformer returns the context data field value of the user. This allows to propagate a context value like an email address instead of the username."
)
public class UserToContextDataTransformerConfig implements UserToPropagatedNameTransformerConfig {
   @PluginProperty(description = "The string-typed context data item to use as the value to propagate.", optional = false)
   private StringContextDataItemNameConfig contextDataItem;
   @BooleanProperty(
      description = "If the transformation is mandatory, the context data value must be present for this user, otherwise an exception is thrown. If not mandatory and the value is not present, this transformer is ignored (i.e. the name is not transformed and subsequent transformers (if any) are called).",
      optional = true,
      defaultValue = "true"
   )
   private boolean mandatoryTransformation;
   @BooleanProperty(
      description = "With this flag the chaining of user name transformers can be interrupted. If it is enabled and the user name transformer found a value in the context field, this result will be the final result.",
      defaultValue = "true",
      optional = true
   )
   private boolean stopAfterSuccessfulTransformation;

   @Override
   public UserToPropagatedNameTransformer getTransformerLogic() {
      return new UserToContextDataTransformer(this.contextDataItem.getContextDataName(), this.mandatoryTransformation, this.stopAfterSuccessfulTransformation);
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public UserToContextDataTransformerConfig(
      StringContextDataItemNameConfig contextDataItem, boolean mandatoryTransformation, boolean stopAfterSuccessfulTransformation
   ) {
      this.contextDataItem = contextDataItem;
      this.mandatoryTransformation = mandatoryTransformation;
      this.stopAfterSuccessfulTransformation = stopAfterSuccessfulTransformation;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class UserToContextDataTransformerConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private StringContextDataItemNameConfig contextDataItem;
      @SuppressFBWarnings(justification = "generated code")
      private boolean mandatoryTransformation;
      @SuppressFBWarnings(justification = "generated code")
      private boolean stopAfterSuccessfulTransformation;

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

      @SuppressFBWarnings(justification = "generated code")
      public UserToContextDataTransformerConfig.UserToContextDataTransformerConfigBuilder contextDataItem(StringContextDataItemNameConfig contextDataItem) {
         this.contextDataItem = contextDataItem;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserToContextDataTransformerConfig.UserToContextDataTransformerConfigBuilder mandatoryTransformation(boolean mandatoryTransformation) {
         this.mandatoryTransformation = mandatoryTransformation;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserToContextDataTransformerConfig.UserToContextDataTransformerConfigBuilder stopAfterSuccessfulTransformation(
         boolean stopAfterSuccessfulTransformation
      ) {
         this.stopAfterSuccessfulTransformation = stopAfterSuccessfulTransformation;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserToContextDataTransformerConfig build() {
         return new UserToContextDataTransformerConfig(this.contextDataItem, this.mandatoryTransformation, this.stopAfterSuccessfulTransformation);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "UserToContextDataTransformerConfig.UserToContextDataTransformerConfigBuilder(contextDataItem="
            + this.contextDataItem
            + ", mandatoryTransformation="
            + this.mandatoryTransformation
            + ", stopAfterSuccessfulTransformation="
            + this.stopAfterSuccessfulTransformation
            + ")";
      }
   }
}
