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

import com.airlock.iam.admin.api.domain.model.user.AuthenticationMethods;
import com.airlock.iam.admin.application.configuration.oathotp.OathOtpTokenController;
import com.airlock.iam.admin.application.service.token.oathotp.AdminOathOtpTokenServiceImpl;
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 OathOtpAuthenticationMethod implements AuthenticationMethod {
   public static final AuthenticationMethod.TokenType TOKEN_TYPE = AuthenticationMethod.TokenType.tokenType("OATH_OTP");
   private final OathOtpTokenController tokenController;
   private final AdminOathOtpTokenServiceImpl service;

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

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

   @Override
   public boolean isAllowedAsNextAuthMethod() {
      return this.tokenController.getOathOtpSettings().isSelectableAsNextAuthMethod();
   }

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

   @Override
   public Optional<AuthenticationMethod.AddAuthMethodErrorCode> addToUser(UserId userId) {
      this.service.createToken(userId);
      if (this.tokenController.isAutoOrderCredential()) {
         this.service.orderActivationLetter(userId);
      }

      return Optional.empty();
   }

   @Override
   public void handleUserCreated(UserId userId) {
      if (this.tokenController.isAutoOrderForNewUsers()) {
         this.service.createToken(userId);
         if (this.tokenController.isAutoOrderCredential()) {
            this.service.orderActivationLetter(userId);
         }
      }
   }

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

   @Override
   public AuthenticationMethods.AuthenticationMethodId getConfiguredId() {
      return this.tokenController.getConfiguredId();
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public OathOtpAuthenticationMethod(OathOtpTokenController tokenController, AdminOathOtpTokenServiceImpl service) {
      this.tokenController = tokenController;
      this.service = service;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof OathOtpAuthenticationMethod 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 OathOtpAuthenticationMethod;
   }

   @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 OathOtpTokenController getTokenController() {
      return this.tokenController;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class OathOtpAuthenticationMethodBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private OathOtpTokenController tokenController;
      @SuppressFBWarnings(justification = "generated code")
      private AdminOathOtpTokenServiceImpl service;

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

      @SuppressFBWarnings(justification = "generated code")
      public OathOtpAuthenticationMethod.OathOtpAuthenticationMethodBuilder tokenController(OathOtpTokenController tokenController) {
         this.tokenController = tokenController;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OathOtpAuthenticationMethod.OathOtpAuthenticationMethodBuilder service(AdminOathOtpTokenServiceImpl service) {
         this.service = service;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OathOtpAuthenticationMethod build() {
         return new OathOtpAuthenticationMethod(this.tokenController, this.service);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "OathOtpAuthenticationMethod.OathOtpAuthenticationMethodBuilder(tokenController=" + this.tokenController + ", service=" + this.service + ")";
      }
   }
}
