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

import com.airlock.iam.admin.api.domain.model.user.AuthenticationMethods;
import com.airlock.iam.admin.application.service.token.vasco.VascoOtpAdminSettings;
import com.airlock.iam.admin.application.service.token.vasco.VascoOtpUserDeviceService;
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 VascoOtpAuthenticationMethod implements AuthenticationMethod {
   public static final AuthenticationMethod.TokenType TOKEN_TYPE = AuthenticationMethod.TokenType.tokenType("VASCO_OTP");
   private final VascoOtpAdminSettings settings;
   private final VascoOtpUserDeviceService service;

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

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

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

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

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

   @Override
   public void handleUserCreated(UserId userId) {
   }

   @Override
   public boolean isVisible(UserId userId) {
      return !this.service.getAllDevicesForUser(userId.getValue()).isEmpty();
   }

   @Override
   public AuthenticationMethods.AuthenticationMethodId getConfiguredId() {
      return AuthenticationMethods.AuthenticationMethodId.authMethodId("VASCO");
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public VascoOtpAuthenticationMethod(VascoOtpAdminSettings settings, VascoOtpUserDeviceService service) {
      this.settings = settings;
      this.service = service;
   }

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

   @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 VascoOtpAuthenticationMethodBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private VascoOtpAdminSettings settings;
      @SuppressFBWarnings(justification = "generated code")
      private VascoOtpUserDeviceService service;

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

      @SuppressFBWarnings(justification = "generated code")
      public VascoOtpAuthenticationMethod.VascoOtpAuthenticationMethodBuilder settings(VascoOtpAdminSettings settings) {
         this.settings = settings;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public VascoOtpAuthenticationMethod.VascoOtpAuthenticationMethodBuilder service(VascoOtpUserDeviceService service) {
         this.service = service;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "VascoOtpAuthenticationMethod.VascoOtpAuthenticationMethodBuilder(settings=" + this.settings + ", service=" + this.service + ")";
      }
   }
}
