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

import com.airlock.iam.base.api.domain.model.mapification.Mapifiable;
import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import com.airlock.iam.common.api.domain.model.binary.ByteSequence;
import com.airlock.iam.fido.api.domain.model.FidoBinaryValue;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;
import lombok.NonNull;

public final class FidoChallenge implements FidoBinaryValue, Mapifiable {
   public static final String CHALLENGE_FOR_TO_STRING = "***(challenge)***";
   private static final String CHALLENGE_KEY = "challenge";
   private static final String NONCE_KEY = "nonce";
   @NonNull
   private final ByteSequence challenge;
   private final Optional<String> nonce;

   private FidoChallenge(byte @NonNull [] challenge, Optional<String> nonce) {
      if (challenge == null) {
         throw new NullPointerException("challenge is marked non-null but is null");
      }

      this.challenge = ByteSequence.byteSequence(challenge);
      this.nonce = nonce;
   }

   public static FidoChallenge challenge(byte[] challenge) {
      return new FidoChallenge(challenge, Optional.empty());
   }

   public static FidoChallenge challenge(byte[] challenge, Optional<String> nonceForLogging) {
      return new FidoChallenge(challenge, nonceForLogging);
   }

   public static FidoChallenge challenge(String challenge) {
      return new FidoChallenge(ByteSequence.fromBase64Url(challenge).orElseThrow().bytes(), Optional.empty());
   }

   public static FidoChallenge challenge(String challenge, Optional<String> nonceForLogging) {
      return new FidoChallenge(ByteSequence.fromBase64Url(challenge).orElseThrow().bytes(), nonceForLogging);
   }

   public String base64Url() {
      return this.challenge.base64Url();
   }

   @Override
   public String toString() {
      return "***(challenge)***";
   }

   @Override
   public byte[] bytes() {
      return this.challenge.bytes();
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues().put("challenge", this.challenge.base64Url()).putIfPresent("nonce", this.nonce).build();
   }

   public static FidoChallenge demapify(MapifiedValues values) {
      return challenge(values.get("challenge"), values.getOptional("nonce"));
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public Optional<String> getNonce() {
      return this.nonce;
   }
}
