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

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import lombok.NonNull;

public final class OAuth2Scope {
   public static final String SCOPES_VALIDATION_REGEX = "[\\x21\\x23-\\x5B\\x5D-\\x7E]+";
   public static final String SCOPES_VALIDATION_EXPLANATION = "<p>Scopes may only contain the following characters: <code>0-9, A-Z, a-z, !, #, $, %, &, ', (, ), *, +, ',', -, ., /, :, ;, &lt;, &gt;, =, ?, @, [, ], ^, _, `, {, }, |, ~</code></p>";
   private static final Pattern SCOPES_VALIDATION_REGEX_PATTERN = Pattern.compile("[\\x21\\x23-\\x5B\\x5D-\\x7E]+");
   @NonNull
   private final String name;

   public static OAuth2Scope of(String name) {
      if (!isAllowedScopeName(name)) {
         throw new IllegalArgumentException("OAuth 2.0 scope is not conform to the RFC6749 specification.");
      } else {
         return new OAuth2Scope(name);
      }
   }

   public static boolean isAllowedScopeName(String name) {
      return SCOPES_VALIDATION_REGEX_PATTERN.matcher(name).matches();
   }

   public static Set<String> convertToScopeNames(Collection<OAuth2Scope> scopes) {
      return scopes.stream().map(OAuth2Scope::name).collect(Collectors.toSet());
   }

   public static List<String> convertToScopeNamesList(Collection<OAuth2Scope> scopes) {
      return scopes.stream().map(OAuth2Scope::name).collect(Collectors.toList());
   }

   public static Set<OAuth2Scope> convertToScopes(Collection<String> scopes) {
      return scopes.stream().map(OAuth2Scope::of).collect(Collectors.toSet());
   }

   public static Set<OAuth2Scope> convertToScopes(String... scopes) {
      return Arrays.stream(scopes).map(OAuth2Scope::of).collect(Collectors.toSet());
   }

   public static List<OAuth2Scope> convertToScopeList(Collection<String> scopes) {
      return scopes.stream().map(OAuth2Scope::of).collect(Collectors.toList());
   }

   public static List<OAuth2Scope> convertToScopeList(String... scopes) {
      return Arrays.stream(scopes).map(OAuth2Scope::of).collect(Collectors.toList());
   }

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

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "OAuth2Scope(name=" + this.name() + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   private OAuth2Scope(@NonNull String name) {
      if (name == null) {
         throw new NullPointerException("name is marked non-null but is null");
      }

      this.name = name;
   }
}
