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

import com.airlock.iam.oauth2.api.application.configuration.claims.ClaimConditionConfig;
import com.airlock.iam.oauth2.api.domain.model.claims.UserClaimExtractor;
import com.airlock.iam.oauth2.domain.model.claims.custom.CustomStringClaimSettings;
import com.airlock.iam.oauth2.domain.model.claims.custom.CustomStringFormatClaimExtractor;
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.StringProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

@Plugin(description = "A custom formatted string claim.", displayName = "String Format Custom Claim")
@PluginLicenseRestriction(moduleAffiliation = "OAuthServer")
public final class CustomStringFormatClaimConfig extends GenericCustomClaimConfig {
   @StringProperty(
      description = "The value is defined using a format string consisting of one or multiple context data values.\nContext data field items can be referenced with ${contextDataField} and will be replaced with the value of\nthe referenced context data field.",
      suggestedValues = {"${surname} ${givenname}", "${street} ${streetNumber}", "${zipcode} ${town}"},
      displayName = "Value Format",
      isMultiLineText = true,
      optional = false
   )
   private String format;

   @Override
   public UserClaimExtractor<?> logic() {
      CustomStringClaimSettings settings = new CustomStringClaimSettings(this.getClaimName(), this.getClaimCondition(), this.format);
      return new CustomStringFormatClaimExtractor(() -> settings);
   }

   public String getFormat() {
      return this.format;
   }

   public CustomStringFormatClaimConfig(String claimName, String format, ClaimConditionConfig claimCondition) {
      super(claimName, claimCondition);
      this.format = format;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public static class CustomStringFormatClaimConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String claimName;
      @SuppressFBWarnings(justification = "generated code")
      private String format;
      @SuppressFBWarnings(justification = "generated code")
      private ClaimConditionConfig claimCondition;

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

      @SuppressFBWarnings(justification = "generated code")
      public CustomStringFormatClaimConfig.CustomStringFormatClaimConfigBuilder claimName(String claimName) {
         this.claimName = claimName;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CustomStringFormatClaimConfig.CustomStringFormatClaimConfigBuilder format(String format) {
         this.format = format;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CustomStringFormatClaimConfig.CustomStringFormatClaimConfigBuilder claimCondition(ClaimConditionConfig claimCondition) {
         this.claimCondition = claimCondition;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CustomStringFormatClaimConfig build() {
         return new CustomStringFormatClaimConfig(this.claimName, this.format, this.claimCondition);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "CustomStringFormatClaimConfig.CustomStringFormatClaimConfigBuilder(claimName="
            + this.claimName
            + ", format="
            + this.format
            + ", claimCondition="
            + this.claimCondition
            + ")";
      }
   }
}
