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

import com.airlock.iam.elog.api.ELogger;
import com.airlock.iam.elog.api.ELoggerFactory;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;

public abstract class Airlock2FADisplayName {
   private static final ELogger LOG = ELoggerFactory.getLogger(Airlock2FADisplayName.class);
   private static final UnaryOperator<Character> LENGTH_PRESERVING_INVALID_CHARACTER_REPLACEMENT_STRATEGY = c -> '_';
   public static final int MAX_DISPLAY_NAME_LENGTH = 50;
   private static final Pattern PUNCTUATION_PATTERN = Pattern.compile("\\p{IsPunct}");
   private static final Pattern ALLOWED_SPECIAL_CHARACTERS_PATTERN = Pattern.compile("[=@#$+]");
   private final String value;

   protected Airlock2FADisplayName(String value) {
      if (!isValid(value)) {
         LOG.debug("Invalid display name: " + value);
         throw new IllegalArgumentException("Invalid display name");
      }

      this.value = value;
   }

   public static boolean isValid(String displayName) {
      if (displayName == null) {
         return false;
      }

      if (displayName.isBlank()) {
         return false;
      }

      if (displayName.length() > 50) {
         return false;
      }

      for (int i = 0; i < displayName.length(); i++) {
         if (!isValidCharacter(displayName.charAt(i))) {
            return false;
         }
      }

      return true;
   }

   private static boolean isValidCharacter(char character) {
      return Character.isSpaceChar(character)
         || Character.isLetterOrDigit(character)
         || isPunctuationCharacter(character)
         || isAllowedSpecialCharacter(character);
   }

   private static boolean isPunctuationCharacter(char character) {
      return PUNCTUATION_PATTERN.matcher(String.valueOf(character)).matches();
   }

   private static boolean isAllowedSpecialCharacter(char character) {
      return ALLOWED_SPECIAL_CHARACTERS_PATTERN.matcher(String.valueOf(character)).matches();
   }

   protected static String formatDisplayName(String providedDisplayName) {
      String truncatedDisplayName = StringUtils.truncate(providedDisplayName, 50);
      return replaceInvalidCharacters(truncatedDisplayName);
   }

   private static String replaceInvalidCharacters(String truncatedDisplayName) {
      char[] characters = truncatedDisplayName.toCharArray();

      for (int i = 0; i < characters.length; i++) {
         if (!isValidCharacter(characters[i])) {
            characters[i] = LENGTH_PRESERVING_INVALID_CHARACTER_REPLACEMENT_STRATEGY.apply(characters[i]);
         }
      }

      return String.valueOf(characters);
   }

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof Airlock2FADisplayName other)) {
         return false;
      } else {
         if (!other.canEqual(this)) {
            return false;
         }

         Object this$value = this.value;
         Object other$value = other.value;
         return this$value == null ? other$value == null : this$value.equals(other$value);
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   protected boolean canEqual(Object other) {
      return other instanceof Airlock2FADisplayName;
   }

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

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