package com.airlock.iam.factor.domain.model.fido;

import com.airlock.iam.elog.api.ELogger;
import com.airlock.iam.elog.api.ELoggerFactory;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import lombok.NonNull;

public final class FidoCredentialDisplayName extends FidoDisplayName {
   private static final ELogger LOG = ELoggerFactory.getLogger(FidoCredentialDisplayName.class);
   public static final int MAXIMAL_LENGTH = 100;

   private FidoCredentialDisplayName(@NonNull String name) {
      super(name, 100);
      if (name == null) {
         throw new NullPointerException("name is marked non-null but is null");
      }
   }

   public static FidoCredentialDisplayName credentialDisplayName(String name) throws IllegalArgumentException {
      return new FidoCredentialDisplayName(name);
   }

   public static boolean isValid(String name) {
      try {
         sanitize(name, 100);
         return true;
      } catch (IllegalArgumentException e) {
         LOG.debug(String.format("The given string is not a valid FIDO credential display name. Cause: %s", e.getMessage()));
         return false;
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof FidoCredentialDisplayName other)) {
         return false;
      } else {
         return !other.canEqual(this) ? false : super.equals(o);
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   protected boolean canEqual(Object other) {
      return other instanceof FidoCredentialDisplayName;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public int hashCode() {
      return super.hashCode();
   }
}
