package com.airlock.iam.techclient.domain.model.apikey;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class ApiKeySecret {
   private static final String SEPARATOR = ".";
   private static final String SECRET_PATTERN = "([0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]{43})\\.([0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]{21})";
   private static final Pattern SECRET_PATTERN_COMPILED = Pattern.compile(
      "([0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]{43})\\.([0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz]{21})"
   );
   private final ApiKeySecretValue value;
   private final ApiKeySecretFingerprint fingerprint;

   private ApiKeySecret(ApiKeySecretValue value, ApiKeySecretFingerprint fingerprint) {
      this.value = value;
      this.fingerprint = fingerprint;
   }

   public static ApiKeySecret apiKeySecret(ApiKeySecretValue value, ApiKeySecretFingerprint fingerprint) {
      return new ApiKeySecret(value, fingerprint);
   }

   public static ApiKeySecret randomApiKeySecret() {
      return apiKeySecret(ApiKeySecretValue.randomApiKeySecretValue(), ApiKeySecretFingerprint.randomApiKeySecretFingerprint());
   }

   public static Optional<ApiKeySecret> parse(String secretString) {
      Matcher matcher = SECRET_PATTERN_COMPILED.matcher(secretString);
      if (!matcher.matches()) {
         return Optional.empty();
      }

      Optional<ApiKeySecretValue> apiKeySecretValue = ApiKeySecretValue.parseApiKeySecretValue(matcher.group(1));
      if (apiKeySecretValue.isEmpty()) {
         return Optional.empty();
      }

      Optional<ApiKeySecretFingerprint> apiKeySecretFingerprint = ApiKeySecretFingerprint.parseApiKeySecretFingerprint(matcher.group(2));
      return apiKeySecretFingerprint.isEmpty() ? Optional.empty() : Optional.of(apiKeySecret(apiKeySecretValue.get(), apiKeySecretFingerprint.get()));
   }

   public boolean matches(ApiKeySecret otherSecret) {
      return this.getFingerprint().matches(otherSecret.getFingerprint()) && this.getValue().matches(otherSecret.getValue());
   }

   public String asString() {
      return this.value.asString().concat(".").concat(this.fingerprint.asString());
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public ApiKeySecretValue getValue() {
      return this.value;
   }

   @SuppressFBWarnings(justification = "generated code")
   public ApiKeySecretFingerprint getFingerprint() {
      return this.fingerprint;
   }

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

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