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 FidoRelyingPartyName extends FidoDisplayName {
   private static final ELogger LOG = ELoggerFactory.getLogger(FidoRelyingPartyName.class);
   private static final int MAXIMAL_LENGTH = 255;

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

   public static FidoRelyingPartyName relyingPartyName(String name) {
      return new FidoRelyingPartyName(name);
   }

   public static boolean isValid(String name) {
      try {
         sanitize(name, 255);
         return true;
      } catch (IllegalArgumentException e) {
         LOG.debug(String.format("The given string is not a valid FIDO relying party 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 FidoRelyingPartyName 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 FidoRelyingPartyName;
   }

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