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

import com.airlock.iam.admin.api.domain.model.user.AuthenticationMethods;
import com.airlock.iam.admin.application.service.certificate.CertificateService;
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 CertificateAuthenticationMethod implements AuthenticationMethod {
   public static final AuthenticationMethod.TokenType TOKEN_TYPE = AuthenticationMethod.TokenType.tokenType("CERTIFICATE");
   private CertificateService service;
   private final boolean allowedAsActive;

   @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 this.allowedAsActive && this.targetUserHasCertificates(userId);
   }

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

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

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

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

   @Override
   public void handleUserCreated(UserId userId) {
   }

   private boolean targetUserHasCertificates(UserId userId) {
      return !this.service.getAll(userId).isEmpty();
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public CertificateAuthenticationMethod(CertificateService service, boolean allowedAsActive) {
      this.service = service;
      this.allowedAsActive = allowedAsActive;
   }

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

   @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 CertificateAuthenticationMethodBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private CertificateService service;
      @SuppressFBWarnings(justification = "generated code")
      private boolean allowedAsActive;

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

      @SuppressFBWarnings(justification = "generated code")
      public CertificateAuthenticationMethod.CertificateAuthenticationMethodBuilder service(CertificateService service) {
         this.service = service;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CertificateAuthenticationMethod.CertificateAuthenticationMethodBuilder allowedAsActive(boolean allowedAsActive) {
         this.allowedAsActive = allowedAsActive;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "CertificateAuthenticationMethod.CertificateAuthenticationMethodBuilder(service="
            + this.service
            + ", allowedAsActive="
            + this.allowedAsActive
            + ")";
      }
   }
}
