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

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

public final class FidoRelyingPartyId {
   private static final ELogger LOG = ELoggerFactory.getLogger(FidoRelyingPartyId.class);
   @NonNull
   private final String id;

   private FidoRelyingPartyId(@NonNull String id) {
      if (id == null) {
         throw new NullPointerException("id is marked non-null but is null");
      }

      if (!isValid(id)) {
         throw new IllegalArgumentException(String.format("The string '%s' is not a valid relying party id", id));
      }

      this.id = id;
   }

   public static FidoRelyingPartyId relyingPartyId(String rpId) {
      return new FidoRelyingPartyId(rpId);
   }

   public static boolean isValid(@NonNull String rpId) {
      if (rpId == null) {
         throw new NullPointerException("rpId is marked non-null but is null");
      } else {
         return InternetDomainName.isValid(rpId);
      }
   }

   @Override
   public String toString() {
      return this.id;
   }

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public String getId() {
      return this.id;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public int hashCode() {
      int PRIME = 59;
      int result = 1;
      Object $id = this.id;
      return result * 59 + ($id == null ? 43 : $id.hashCode());
   }
}
