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

import com.airlock.iam.admin.api.domain.model.user.AuthenticationMethods;
import com.airlock.iam.admin.application.service.token.secretquestions.SecretQuestionsAdminService;
import com.airlock.iam.admin.domain.model.authmethod.AuthenticationMethod;
import com.airlock.iam.base.api.domain.model.user.UserId;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;

public class SecretQuestionAuthenticationMethod implements AuthenticationMethod {
   public static final AuthenticationMethod.TokenType TOKEN_TYPE = AuthenticationMethod.TokenType.tokenType("SECRET_QUESTIONS");
   private final SecretQuestionsAdminService secretQuestionsService;

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

   @Override
   public AuthenticationMethods.AuthenticationMethodId getConfiguredId() {
      return AuthenticationMethods.AuthenticationMethodId.authMethodId(TOKEN_TYPE.getId());
   }

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

   @Override
   public boolean isAllowedAsNextAuthMethod() {
      return false;
   }

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

   @Override
   public boolean isVisible(UserId userId) {
      return this.secretQuestionsService.isSecretQuestionsEnabled(userId.getValue())
         || !this.secretQuestionsService.getAnsweredQuestions(userId.getValue()).isEmpty();
   }

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

   @Override
   public void handleUserCreated(UserId userId) {
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public SecretQuestionAuthenticationMethod(SecretQuestionsAdminService secretQuestionsService) {
      this.secretQuestionsService = secretQuestionsService;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof SecretQuestionAuthenticationMethod other)) {
         return false;
      } else {
         if (!other.canEqual(this)) {
            return false;
         }

         Object this$TOKEN_TYPE = TOKEN_TYPE;
         Object other$TOKEN_TYPE = TOKEN_TYPE;
         return this$TOKEN_TYPE == null ? other$TOKEN_TYPE == null : this$TOKEN_TYPE.equals(other$TOKEN_TYPE);
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   protected boolean canEqual(Object other) {
      return other instanceof SecretQuestionAuthenticationMethod;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public static class SecretQuestionAuthenticationMethodBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private SecretQuestionsAdminService secretQuestionsService;

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

      @SuppressFBWarnings(justification = "generated code")
      public SecretQuestionAuthenticationMethod.SecretQuestionAuthenticationMethodBuilder secretQuestionsService(
         SecretQuestionsAdminService secretQuestionsService
      ) {
         this.secretQuestionsService = secretQuestionsService;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public SecretQuestionAuthenticationMethod build() {
         return new SecretQuestionAuthenticationMethod(this.secretQuestionsService);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "SecretQuestionAuthenticationMethod.SecretQuestionAuthenticationMethodBuilder(secretQuestionsService=" + this.secretQuestionsService + ")";
      }
   }
}
