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

import com.airlock.iam.common.api.domain.model.claims.Claim;
import com.airlock.iam.oauth2.api.domain.model.claims.UserClaimExtractor;
import com.airlock.iam.oauth2.api.domain.model.claims.conditions.ClaimCondition;
import com.airlock.iam.plugin.api.annotation.property.PluginProperty;
import com.airlock.iam.plugin.api.annotation.property.StringProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;

public abstract class CustomClaimConfig {
   @StringProperty(
      description = "The name (JSON key) of the claim.</p><b>Attention:</b> If a custom claim with this same claim name already exists, it will result in a runtime error.</p>Using a registered claim name (see <a href=\"https://tools.ietf.org/html/rfc7519#section-4.1\" target=\"_blank\">RFC 7519</a>) might cause the claim to be ignored or an exception to be thrown.",
      exampleValues = {"firstname", "street", "zip", "country", "roles"},
      optional = false
   )
   private String claimName;
   public static final String CLAIM_NAME_FIELD = "claimName";
   @PluginProperty(
      description = "<p>This custom claim will only be added to the issued token if the configured condition is satisfied.</p>\n<p>If no condition is configured, the custom claim will always be added.</p>",
      optional = true
   )
   private ClaimConditionConfig claimCondition;
   public static final String CLAIM_CONDITION_PROPERTY = "claimCondition";

   public Optional<ClaimCondition> getClaimCondition() {
      return Optional.ofNullable(this.claimCondition).map(ClaimConditionConfig::logic);
   }

   public String getClaimName() {
      return this.claimName;
   }

   @Deprecated(forRemoval = true)
   public abstract UserClaimExtractor<? extends Claim<?>> logic();

   public UserClaimExtractor<? extends Claim<?>> logic(CustomClaimExtractorSettings settings) {
      return this.logic();
   }

   @SuppressFBWarnings(justification = "generated code")
   public CustomClaimConfig(String claimName, ClaimConditionConfig claimCondition) {
      this.claimName = claimName;
      this.claimCondition = claimCondition;
   }

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