package com.airlock.iam.oauth2.domain.model.token;

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

public final class OAuth2TokenBinding {
   private static final String METHOD_GROUP = "method";
   private static final String PROOF_GROUP = "proof";
   private static final Pattern STRING_FORMAT_PATTERN = Pattern.compile("(?<method>[^:]+):(?<proof>[^:]+)");
   private final String method;
   private final String proof;

   public static OAuth2TokenBinding tokenBinding(String method, String proof) {
      if (method.contains(":")) {
         throw new IllegalArgumentException("Invalid char for token binding method: \":\"");
      } else if (proof.contains(":")) {
         throw new IllegalArgumentException("Invalid char for token binding proof: \":\"");
      } else {
         return new OAuth2TokenBinding(method, proof);
      }
   }

   public static OAuth2TokenBinding fromString(String tokenBinding) {
      Matcher matcher = STRING_FORMAT_PATTERN.matcher(tokenBinding);
      if (!matcher.matches()) {
         throw new IllegalArgumentException("Invalid token binding string");
      } else {
         return new OAuth2TokenBinding(matcher.group("method"), matcher.group("proof"));
      }
   }

   public String asString() {
      return this.method + ":" + this.proof;
   }

   @SuppressFBWarnings(justification = "generated code")
   public String getMethod() {
      return this.method;
   }

   @SuppressFBWarnings(justification = "generated code")
   public String getProof() {
      return this.proof;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "OAuth2TokenBinding(method=" + this.getMethod() + ", proof=" + this.getProof() + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   private OAuth2TokenBinding(String method, String proof) {
      this.method = method;
      this.proof = proof;
   }
}
