package com.airlock.iam.admin.domain.model.token.fido;

import com.airlock.iam.admin.api.domain.model.user.AuthenticationMethods;
import com.airlock.iam.admin.domain.model.authmethod.AuthenticationMethod;
import com.airlock.iam.base.api.domain.model.user.UserId;
import com.airlock.iam.elog.api.ELogger;
import com.airlock.iam.elog.api.ELoggerFactory;
import com.airlock.iam.elog.api.LogTypes;
import com.airlock.iam.factor.domain.model.fido.FidoUserCredentialRepository;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;
import lombok.NonNull;

public final class FidoAuthenticationMethod implements AuthenticationMethod {
   private static final ELogger LOG = ELoggerFactory.getLogger(FidoAuthenticationMethod.class);
   public static final String AUTH_METHOD_ID = "FIDO";
   private static final AuthenticationMethod.TokenType TOKEN_TYPE = AuthenticationMethod.TokenType.tokenType("FIDO");
   @NonNull
   private final FidoUserCredentialRepository repository;
   private final boolean selectableAsNextAuthMethod;
   private final boolean selectableAsActiveAuthMethod;
   private final AuthenticationMethods.AuthenticationMethodId configuredId;

   @Override
   public AuthenticationMethod.TokenType getTokenType() {
      return TOKEN_TYPE;
   }

   @Override
   public boolean isAllowedAsActiveAuthMethodForUser(UserId userId) {
      return this.selectableAsActiveAuthMethod && this.isVisible(userId);
   }

   @Override
   public boolean isAllowedAsNextAuthMethod() {
      return this.selectableAsNextAuthMethod;
   }

   @Override
   public boolean isAllowedAsNewAuthMethod(UserId userId) {
      return false;
   }

   @Override
   public boolean isVisible(UserId userId) {
      return this.hasAtLeastOneFidoCredential(userId);
   }

   private boolean hasAtLeastOneFidoCredential(UserId userId) {
      try {
         return !this.repository.findBy(userId).isEmpty();
      } catch (RuntimeException e) {
         LOG.warn(LogTypes.SERVER_INFO_SYS, String.format("Exception while determining whether user '%s' has any FIDO credentials", userId), e);
         return false;
      }
   }

   @Override
   public Optional<AuthenticationMethod.AddAuthMethodErrorCode> addToUser(UserId userId) {
      return Optional.empty();
   }

   @Override
   public void handleUserCreated(UserId userId) {
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public FidoAuthenticationMethod(
      @NonNull FidoUserCredentialRepository repository,
      boolean selectableAsNextAuthMethod,
      boolean selectableAsActiveAuthMethod,
      AuthenticationMethods.AuthenticationMethodId configuredId
   ) {
      if (repository == null) {
         throw new NullPointerException("repository is marked non-null but is null");
      }

      this.repository = repository;
      this.selectableAsNextAuthMethod = selectableAsNextAuthMethod;
      this.selectableAsActiveAuthMethod = selectableAsActiveAuthMethod;
      this.configuredId = configuredId;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public AuthenticationMethods.AuthenticationMethodId getConfiguredId() {
      return this.configuredId;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class FidoAuthenticationMethodBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private FidoUserCredentialRepository repository;
      @SuppressFBWarnings(justification = "generated code")
      private boolean selectableAsNextAuthMethod;
      @SuppressFBWarnings(justification = "generated code")
      private boolean selectableAsActiveAuthMethod;
      @SuppressFBWarnings(justification = "generated code")
      private AuthenticationMethods.AuthenticationMethodId configuredId;

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

      @SuppressFBWarnings(justification = "generated code")
      public FidoAuthenticationMethod.FidoAuthenticationMethodBuilder repository(@NonNull FidoUserCredentialRepository repository) {
         if (repository == null) {
            throw new NullPointerException("repository is marked non-null but is null");
         }

         this.repository = repository;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public FidoAuthenticationMethod.FidoAuthenticationMethodBuilder selectableAsNextAuthMethod(boolean selectableAsNextAuthMethod) {
         this.selectableAsNextAuthMethod = selectableAsNextAuthMethod;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public FidoAuthenticationMethod.FidoAuthenticationMethodBuilder selectableAsActiveAuthMethod(boolean selectableAsActiveAuthMethod) {
         this.selectableAsActiveAuthMethod = selectableAsActiveAuthMethod;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public FidoAuthenticationMethod.FidoAuthenticationMethodBuilder configuredId(AuthenticationMethods.AuthenticationMethodId configuredId) {
         this.configuredId = configuredId;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public FidoAuthenticationMethod build() {
         return new FidoAuthenticationMethod(this.repository, this.selectableAsNextAuthMethod, this.selectableAsActiveAuthMethod, this.configuredId);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "FidoAuthenticationMethod.FidoAuthenticationMethodBuilder(repository="
            + this.repository
            + ", selectableAsNextAuthMethod="
            + this.selectableAsNextAuthMethod
            + ", selectableAsActiveAuthMethod="
            + this.selectableAsActiveAuthMethod
            + ", configuredId="
            + this.configuredId
            + ")";
      }
   }
}
