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

import com.airlock.iam.admin.api.domain.model.administrator.CurrentAdministrator;
import com.airlock.iam.admin.api.domain.model.user.AuthenticationMethods;
import com.airlock.iam.admin.application.service.matrixcard.MatrixCardAdminService;
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 jakarta.inject.Provider;
import java.util.Optional;

public class MatrixAuthenticationMethod implements AuthenticationMethod {
   public static final AuthenticationMethod.TokenType TOKEN_TYPE = AuthenticationMethod.TokenType.tokenType("MATRIX");
   private final AuthenticationMethods.AuthenticationMethodId configuredId;
   private final boolean autoOrderOnAuthMethodAdd;
   private final boolean autoOrderOnUserCreate;
   private final boolean selectableAsAuthMethod;
   private final boolean selectableAsNextAuthMethod;
   private final Provider<MatrixCardAdminService> service;
   private final CurrentAdministrator administrator;

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

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

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

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

   @Override
   public boolean isVisible(UserId userId) {
      MatrixCards matrixCards = ((MatrixCardAdminService)this.service.get()).get(userId);
      return matrixCards.getCurrentCard().isPresent() || matrixCards.getNextCard().isPresent() || matrixCards.getOrder().isPresent();
   }

   @Override
   public Optional<AuthenticationMethod.AddAuthMethodErrorCode> addToUser(UserId userId) {
      if (this.autoOrderOnAuthMethodAdd) {
         this.orderMatrixCards(userId);
      }

      return Optional.empty();
   }

   @Override
   public void handleUserCreated(UserId userId) {
      if (this.autoOrderOnUserCreate) {
         this.orderMatrixCards(userId);
      }
   }

   private void orderMatrixCards(UserId userId) {
      ((MatrixCardAdminService)this.service.get()).order(userId, this.administrator.getName());
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public MatrixAuthenticationMethod(
      AuthenticationMethods.AuthenticationMethodId configuredId,
      boolean autoOrderOnAuthMethodAdd,
      boolean autoOrderOnUserCreate,
      boolean selectableAsAuthMethod,
      boolean selectableAsNextAuthMethod,
      Provider<MatrixCardAdminService> service,
      CurrentAdministrator administrator
   ) {
      this.configuredId = configuredId;
      this.autoOrderOnAuthMethodAdd = autoOrderOnAuthMethodAdd;
      this.autoOrderOnUserCreate = autoOrderOnUserCreate;
      this.selectableAsAuthMethod = selectableAsAuthMethod;
      this.selectableAsNextAuthMethod = selectableAsNextAuthMethod;
      this.service = service;
      this.administrator = administrator;
   }

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

   @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")
   @Override
   public AuthenticationMethods.AuthenticationMethodId getConfiguredId() {
      return this.configuredId;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class MatrixAuthenticationMethodBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private AuthenticationMethods.AuthenticationMethodId configuredId;
      @SuppressFBWarnings(justification = "generated code")
      private boolean autoOrderOnAuthMethodAdd;
      @SuppressFBWarnings(justification = "generated code")
      private boolean autoOrderOnUserCreate;
      @SuppressFBWarnings(justification = "generated code")
      private boolean selectableAsAuthMethod;
      @SuppressFBWarnings(justification = "generated code")
      private boolean selectableAsNextAuthMethod;
      @SuppressFBWarnings(justification = "generated code")
      private Provider<MatrixCardAdminService> service;
      @SuppressFBWarnings(justification = "generated code")
      private CurrentAdministrator administrator;

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

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

      @SuppressFBWarnings(justification = "generated code")
      public MatrixAuthenticationMethod.MatrixAuthenticationMethodBuilder autoOrderOnAuthMethodAdd(boolean autoOrderOnAuthMethodAdd) {
         this.autoOrderOnAuthMethodAdd = autoOrderOnAuthMethodAdd;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public MatrixAuthenticationMethod.MatrixAuthenticationMethodBuilder autoOrderOnUserCreate(boolean autoOrderOnUserCreate) {
         this.autoOrderOnUserCreate = autoOrderOnUserCreate;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public MatrixAuthenticationMethod.MatrixAuthenticationMethodBuilder selectableAsAuthMethod(boolean selectableAsAuthMethod) {
         this.selectableAsAuthMethod = selectableAsAuthMethod;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      public MatrixAuthenticationMethod.MatrixAuthenticationMethodBuilder service(Provider<MatrixCardAdminService> service) {
         this.service = service;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public MatrixAuthenticationMethod.MatrixAuthenticationMethodBuilder administrator(CurrentAdministrator administrator) {
         this.administrator = administrator;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public MatrixAuthenticationMethod build() {
         return new MatrixAuthenticationMethod(
            this.configuredId,
            this.autoOrderOnAuthMethodAdd,
            this.autoOrderOnUserCreate,
            this.selectableAsAuthMethod,
            this.selectableAsNextAuthMethod,
            this.service,
            this.administrator
         );
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "MatrixAuthenticationMethod.MatrixAuthenticationMethodBuilder(configuredId="
            + this.configuredId
            + ", autoOrderOnAuthMethodAdd="
            + this.autoOrderOnAuthMethodAdd
            + ", autoOrderOnUserCreate="
            + this.autoOrderOnUserCreate
            + ", selectableAsAuthMethod="
            + this.selectableAsAuthMethod
            + ", selectableAsNextAuthMethod="
            + this.selectableAsNextAuthMethod
            + ", service="
            + this.service
            + ", administrator="
            + this.administrator
            + ")";
      }
   }
}
