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

import com.airlock.iam.admin.api.domain.model.user.AuthenticationMethods;
import com.airlock.iam.admin.application.service.token.mtan.MtanTokenService;
import com.airlock.iam.admin.domain.model.authmethod.AuthenticationMethod;
import com.airlock.iam.base.api.domain.model.user.UserId;
import com.airlock.iam.common.domain.model.mtan.Iak;
import com.airlock.iam.common.domain.model.mtan.MtanItemView;
import com.airlock.iam.core.misc.impl.authen.mtan.MtanSettings;
import com.airlock.iam.core.misc.impl.token.MtanItem;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class MtanAuthenticationMethod implements AuthenticationMethod {
   public static final AuthenticationMethod.TokenType TOKEN_TYPE = AuthenticationMethod.TokenType.tokenType("MTAN");
   private final AuthenticationMethods.AuthenticationMethodId configuredId;
   private final boolean autoOrderOnAuthMethodAdd;
   private final boolean autoOrderOnUserCreate;
   private final MtanSettings settings;
   private final MtanTokenService service;

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

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

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

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

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

      return Optional.empty();
   }

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

   @Override
   public boolean isVisible(UserId userId) {
      List<MtanItem> items = this.service.retrieveMtanItems(userId).stream().<MtanItem>map(MtanItemView::getMtanItem).collect(Collectors.toList());
      Optional<Iak> iak = this.service.getIak(userId);
      return iak.isPresent() || this.settings.getMtanHandler().areMtanItemsAvailableForAdministration(items);
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public MtanAuthenticationMethod(
      AuthenticationMethods.AuthenticationMethodId configuredId,
      boolean autoOrderOnAuthMethodAdd,
      boolean autoOrderOnUserCreate,
      MtanSettings settings,
      MtanTokenService service
   ) {
      this.configuredId = configuredId;
      this.autoOrderOnAuthMethodAdd = autoOrderOnAuthMethodAdd;
      this.autoOrderOnUserCreate = autoOrderOnUserCreate;
      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 MtanAuthenticationMethod 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 MtanAuthenticationMethod;
   }

   @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 MtanAuthenticationMethodBuilder {
      @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 MtanSettings settings;
      @SuppressFBWarnings(justification = "generated code")
      private MtanTokenService service;

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

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

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

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

      @SuppressFBWarnings(justification = "generated code")
      public MtanAuthenticationMethod.MtanAuthenticationMethodBuilder settings(MtanSettings settings) {
         this.settings = settings;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public MtanAuthenticationMethod.MtanAuthenticationMethodBuilder service(MtanTokenService service) {
         this.service = service;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "MtanAuthenticationMethod.MtanAuthenticationMethodBuilder(configuredId="
            + this.configuredId
            + ", autoOrderOnAuthMethodAdd="
            + this.autoOrderOnAuthMethodAdd
            + ", autoOrderOnUserCreate="
            + this.autoOrderOnUserCreate
            + ", settings="
            + this.settings
            + ", service="
            + this.service
            + ")";
      }
   }
}
