package com.airlock.iam.common.domain.model.auth;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import lombok.NonNull;

@SuppressFBWarnings("UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR")
public final class ExternalizationContext {
   private final String jwt;

   @SuppressFBWarnings(justification = "generated code")
   ExternalizationContext(String jwt) {
      this.jwt = jwt;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static ExternalizationContext.ExternalizationContextBuilder builder() {
      return new ExternalizationContext.ExternalizationContextBuilder();
   }

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

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

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

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

   @SuppressFBWarnings(justification = "generated code")
   public static class ExternalizationContextBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String jwt;

      @SuppressFBWarnings(justification = "generated code")
      ExternalizationContextBuilder() {
      }

      @SuppressFBWarnings(justification = "generated code")
      public ExternalizationContext.ExternalizationContextBuilder jwt(String jwt) {
         this.jwt = jwt;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ExternalizationContext build() {
         return new ExternalizationContext(this.jwt);
      }

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

   public static final class ExternalizationContextContent {
      @NonNull
      private final String userId;
      @NonNull
      private final Date expirationDate;
      private final Map<String, String> values;

      public boolean isExpired() {
         return Instant.now().isAfter(this.expirationDate.toInstant());
      }

      public boolean isForUser(String user) {
         return this.userId.equals(user);
      }

      @SuppressFBWarnings(justification = "generated code")
      ExternalizationContextContent(@NonNull String userId, @NonNull Date expirationDate, Map<String, String> values) {
         if (userId == null) {
            throw new NullPointerException("userId is marked non-null but is null");
         }

         if (expirationDate == null) {
            throw new NullPointerException("expirationDate is marked non-null but is null");
         }

         this.userId = userId;
         this.expirationDate = expirationDate;
         this.values = values;
      }

      @SuppressFBWarnings(justification = "generated code")
      public static ExternalizationContext.ExternalizationContextContent.ExternalizationContextContentBuilder builder() {
         return new ExternalizationContext.ExternalizationContextContent.ExternalizationContextContentBuilder();
      }

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

      @NonNull
      @SuppressFBWarnings(justification = "generated code")
      public Date getExpirationDate() {
         return this.expirationDate;
      }

      @SuppressFBWarnings(justification = "generated code")
      public Map<String, String> getValues() {
         return this.values;
      }

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

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "ExternalizationContext.ExternalizationContextContent(userId="
            + this.getUserId()
            + ", expirationDate="
            + this.getExpirationDate()
            + ", values="
            + this.getValues()
            + ")";
      }

      @SuppressFBWarnings(justification = "generated code")
      public static class ExternalizationContextContentBuilder {
         @SuppressFBWarnings(justification = "generated code")
         private String userId;
         @SuppressFBWarnings(justification = "generated code")
         private Date expirationDate;
         @SuppressFBWarnings(justification = "generated code")
         private ArrayList<String> values$key;
         @SuppressFBWarnings(justification = "generated code")
         private ArrayList<String> values$value;

         @SuppressFBWarnings(justification = "generated code")
         ExternalizationContextContentBuilder() {
         }

         @SuppressFBWarnings(justification = "generated code")
         public ExternalizationContext.ExternalizationContextContent.ExternalizationContextContentBuilder userId(@NonNull String userId) {
            if (userId == null) {
               throw new NullPointerException("userId is marked non-null but is null");
            }

            this.userId = userId;
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public ExternalizationContext.ExternalizationContextContent.ExternalizationContextContentBuilder expirationDate(@NonNull Date expirationDate) {
            if (expirationDate == null) {
               throw new NullPointerException("expirationDate is marked non-null but is null");
            }

            this.expirationDate = expirationDate;
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public ExternalizationContext.ExternalizationContextContent.ExternalizationContextContentBuilder value(String valueKey, String valueValue) {
            if (this.values$key == null) {
               this.values$key = new ArrayList<>();
               this.values$value = new ArrayList<>();
            }

            this.values$key.add(valueKey);
            this.values$value.add(valueValue);
            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public ExternalizationContext.ExternalizationContextContent.ExternalizationContextContentBuilder values(Map<? extends String, ? extends String> values) {
            if (values == null) {
               throw new NullPointerException("values cannot be null");
            }

            if (this.values$key == null) {
               this.values$key = new ArrayList<>();
               this.values$value = new ArrayList<>();
            }

            for (Entry<? extends String, ? extends String> $lombokEntry : values.entrySet()) {
               this.values$key.add($lombokEntry.getKey());
               this.values$value.add($lombokEntry.getValue());
            }

            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public ExternalizationContext.ExternalizationContextContent.ExternalizationContextContentBuilder clearValues() {
            if (this.values$key != null) {
               this.values$key.clear();
               this.values$value.clear();
            }

            return this;
         }

         @SuppressFBWarnings(justification = "generated code")
         public ExternalizationContext.ExternalizationContextContent build() {
            return new ExternalizationContext.ExternalizationContextContent(
               this.userId,
               this.expirationDate,
               switch (this.values$key == null ? 0 : this.values$key.size()) {
                  case 0 -> Collections.emptyMap();
                  case 1 -> Collections.singletonMap(this.values$key.get(0), this.values$value.get(0));
                  default -> {
                     Map<String, String> values = new LinkedHashMap<>(
                        this.values$key.size() < 1073741824 ? 1 + this.values$key.size() + (this.values$key.size() - 3) / 3 : Integer.MAX_VALUE
                     );

                     for (int $i = 0; $i < this.values$key.size(); $i++) {
                        values.put(this.values$key.get($i), this.values$value.get($i));
                     }

                     yield Collections.unmodifiableMap(values);
                  }
               }
            );
         }

         @SuppressFBWarnings(justification = "generated code")
         @Override
         public String toString() {
            return "ExternalizationContext.ExternalizationContextContent.ExternalizationContextContentBuilder(userId="
               + this.userId
               + ", expirationDate="
               + this.expirationDate
               + ", values$key="
               + this.values$key
               + ", values$value="
               + this.values$value
               + ")";
         }
      }
   }
}
