package com.airlock.iam.oauth2.domain.service.tokenexchange.validation;

import com.airlock.iam.oauth2.api.domain.model.tokenexchange.OAuth2TokenExchangeTokenType;
import com.airlock.iam.oauth2.domain.model.tokenexchange.OAuth2TokenExchangeTokenIssuerId;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import lombok.NonNull;

public final class OAuth2TokenExchangeValidationServiceSettings {
   @NonNull
   private final List<OAuth2TokenExchangeTokenIssuerId> allowedIssuers;
   @NonNull
   private final OAuth2TokenExchangeValidationServiceSettings.TokenKind tokenKind;
   @NonNull
   private final List<OAuth2TokenExchangeTokenType> allowedTokenTypes;

   @SuppressFBWarnings(justification = "generated code")
   OAuth2TokenExchangeValidationServiceSettings(
      @NonNull List<OAuth2TokenExchangeTokenIssuerId> allowedIssuers,
      @NonNull OAuth2TokenExchangeValidationServiceSettings.TokenKind tokenKind,
      @NonNull List<OAuth2TokenExchangeTokenType> allowedTokenTypes
   ) {
      if (allowedIssuers == null) {
         throw new NullPointerException("allowedIssuers is marked non-null but is null");
      }

      if (tokenKind == null) {
         throw new NullPointerException("tokenKind is marked non-null but is null");
      }

      if (allowedTokenTypes == null) {
         throw new NullPointerException("allowedTokenTypes is marked non-null but is null");
      }

      this.allowedIssuers = allowedIssuers;
      this.tokenKind = tokenKind;
      this.allowedTokenTypes = allowedTokenTypes;
   }

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

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public List<OAuth2TokenExchangeTokenIssuerId> getAllowedIssuers() {
      return this.allowedIssuers;
   }

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public OAuth2TokenExchangeValidationServiceSettings.TokenKind getTokenKind() {
      return this.tokenKind;
   }

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public List<OAuth2TokenExchangeTokenType> getAllowedTokenTypes() {
      return this.allowedTokenTypes;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof OAuth2TokenExchangeValidationServiceSettings other)) {
         return false;
      } else {
         Object this$allowedIssuers = this.allowedIssuers;
         Object other$allowedIssuers = other.allowedIssuers;
         if (this$allowedIssuers == null ? other$allowedIssuers == null : this$allowedIssuers.equals(other$allowedIssuers)) {
            Object this$tokenKind = this.tokenKind;
            Object other$tokenKind = other.tokenKind;
            if (this$tokenKind == null ? other$tokenKind == null : this$tokenKind.equals(other$tokenKind)) {
               Object this$allowedTokenTypes = this.allowedTokenTypes;
               Object other$allowedTokenTypes = other.allowedTokenTypes;
               return this$allowedTokenTypes == null ? other$allowedTokenTypes == null : this$allowedTokenTypes.equals(other$allowedTokenTypes);
            } else {
               return false;
            }
         } else {
            return false;
         }
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public int hashCode() {
      int PRIME = 59;
      int result = 1;
      Object $allowedIssuers = this.allowedIssuers;
      result = result * 59 + ($allowedIssuers == null ? 43 : $allowedIssuers.hashCode());
      Object $tokenKind = this.tokenKind;
      result = result * 59 + ($tokenKind == null ? 43 : $tokenKind.hashCode());
      Object $allowedTokenTypes = this.allowedTokenTypes;
      return result * 59 + ($allowedTokenTypes == null ? 43 : $allowedTokenTypes.hashCode());
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "OAuth2TokenExchangeValidationServiceSettings(allowedIssuers="
         + this.getAllowedIssuers()
         + ", tokenKind="
         + this.getTokenKind()
         + ", allowedTokenTypes="
         + this.getAllowedTokenTypes()
         + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class OAuth2TokenExchangeValidationServiceSettingsBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private List<OAuth2TokenExchangeTokenIssuerId> allowedIssuers;
      @SuppressFBWarnings(justification = "generated code")
      private OAuth2TokenExchangeValidationServiceSettings.TokenKind tokenKind;
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<OAuth2TokenExchangeTokenType> allowedTokenTypes;

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

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2TokenExchangeValidationServiceSettings.OAuth2TokenExchangeValidationServiceSettingsBuilder allowedIssuers(
         @NonNull List<OAuth2TokenExchangeTokenIssuerId> allowedIssuers
      ) {
         if (allowedIssuers == null) {
            throw new NullPointerException("allowedIssuers is marked non-null but is null");
         }

         this.allowedIssuers = allowedIssuers;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2TokenExchangeValidationServiceSettings.OAuth2TokenExchangeValidationServiceSettingsBuilder tokenKind(
         @NonNull OAuth2TokenExchangeValidationServiceSettings.TokenKind tokenKind
      ) {
         if (tokenKind == null) {
            throw new NullPointerException("tokenKind is marked non-null but is null");
         }

         this.tokenKind = tokenKind;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2TokenExchangeValidationServiceSettings.OAuth2TokenExchangeValidationServiceSettingsBuilder allowedTokenType(
         OAuth2TokenExchangeTokenType allowedTokenType
      ) {
         if (this.allowedTokenTypes == null) {
            this.allowedTokenTypes = new ArrayList<>();
         }

         this.allowedTokenTypes.add(allowedTokenType);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2TokenExchangeValidationServiceSettings.OAuth2TokenExchangeValidationServiceSettingsBuilder allowedTokenTypes(
         Collection<? extends OAuth2TokenExchangeTokenType> allowedTokenTypes
      ) {
         if (allowedTokenTypes == null) {
            throw new NullPointerException("allowedTokenTypes cannot be null");
         }

         if (this.allowedTokenTypes == null) {
            this.allowedTokenTypes = new ArrayList<>();
         }

         this.allowedTokenTypes.addAll(allowedTokenTypes);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2TokenExchangeValidationServiceSettings.OAuth2TokenExchangeValidationServiceSettingsBuilder clearAllowedTokenTypes() {
         if (this.allowedTokenTypes != null) {
            this.allowedTokenTypes.clear();
         }

         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2TokenExchangeValidationServiceSettings build() {
         return new OAuth2TokenExchangeValidationServiceSettings(
            this.allowedIssuers, this.tokenKind, switch (this.allowedTokenTypes == null ? 0 : this.allowedTokenTypes.size()) {
               case 0 -> Collections.emptyList();
               case 1 -> Collections.singletonList(this.allowedTokenTypes.get(0));
               default -> Collections.unmodifiableList(new ArrayList<>(this.allowedTokenTypes));
            }
         );
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "OAuth2TokenExchangeValidationServiceSettings.OAuth2TokenExchangeValidationServiceSettingsBuilder(allowedIssuers="
            + this.allowedIssuers
            + ", tokenKind="
            + this.tokenKind
            + ", allowedTokenTypes="
            + this.allowedTokenTypes
            + ")";
      }
   }

   public enum TokenKind {
      SUBJECT_TOKEN("subject"),
      ACTOR_TOKEN("actor");

      private final String description;

      TokenKind(String description) {
         this.description = description;
      }

      public String getDescription() {
         return this.description;
      }
   }
}
