package com.airlock.iam.authentication.domain.model.rememberme;

import com.airlock.iam.base.api.domain.model.user.UserId;
import com.airlock.iam.base.misc.time.TimeProvider;
import com.airlock.iam.elog.api.ELogger;
import com.airlock.iam.elog.api.ELoggerFactory;
import com.airlock.iam.flow.shared.domain.model.rememberme.RememberMeTokenLifetime;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;
import lombok.NonNull;

public final class RememberMeJspToken {
   private static final ELogger LOG = ELoggerFactory.getLogger(RememberMeJspToken.class);
   @NonNull
   private final UserId iamUserId;
   @NonNull
   private final Optional<Instant> generationTimestamp;
   @NonNull
   private final Optional<Instant> lastUsedTimestamp;
   @NonNull
   private final TimeProvider time;

   public boolean isExpired(RememberMeTokenLifetime tokenLifetime) {
      Instant currentTime = this.time().getCurrentInstant();
      return this.isLifetimeExceeded(currentTime, tokenLifetime.lifetime()) || this.isIdleLifetimeExceeded(currentTime, tokenLifetime.idleLifetime());
   }

   private boolean isLifetimeExceeded(Instant currentTime, Duration lifetime) {
      return this.generationTimestamp().get().isAfter(currentTime) || currentTime.isAfter(this.generationTimestamp().get().plus(lifetime));
   }

   private boolean isIdleLifetimeExceeded(Instant currentTime, Optional<Duration> idleLifetime) {
      return this.lastUsedTimestamp().isPresent() && idleLifetime.isPresent()
         ? this.lastUsedTimestamp().get().isAfter(currentTime) || this.lastUsedTimestamp().get().plus(idleLifetime.get()).isBefore(currentTime)
         : false;
   }

   @SuppressFBWarnings(justification = "generated code")
   RememberMeJspToken(
      @NonNull UserId iamUserId, @NonNull Optional<Instant> generationTimestamp, @NonNull Optional<Instant> lastUsedTimestamp, @NonNull TimeProvider time
   ) {
      if (iamUserId == null) {
         throw new NullPointerException("iamUserId is marked non-null but is null");
      }

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

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

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

      this.iamUserId = iamUserId;
      this.generationTimestamp = generationTimestamp;
      this.lastUsedTimestamp = lastUsedTimestamp;
      this.time = time;
   }

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

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

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public Optional<Instant> generationTimestamp() {
      return this.generationTimestamp;
   }

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public Optional<Instant> lastUsedTimestamp() {
      return this.lastUsedTimestamp;
   }

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

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "RememberMeJspToken(iamUserId="
         + this.iamUserId()
         + ", generationTimestamp="
         + this.generationTimestamp()
         + ", lastUsedTimestamp="
         + this.lastUsedTimestamp()
         + ", time="
         + this.time()
         + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class RememberMeJspTokenBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private UserId iamUserId;
      @SuppressFBWarnings(justification = "generated code")
      private Optional<Instant> generationTimestamp;
      @SuppressFBWarnings(justification = "generated code")
      private Optional<Instant> lastUsedTimestamp;
      @SuppressFBWarnings(justification = "generated code")
      private TimeProvider time;

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

      @SuppressFBWarnings(justification = "generated code")
      public RememberMeJspToken.RememberMeJspTokenBuilder iamUserId(@NonNull UserId iamUserId) {
         if (iamUserId == null) {
            throw new NullPointerException("iamUserId is marked non-null but is null");
         }

         this.iamUserId = iamUserId;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RememberMeJspToken.RememberMeJspTokenBuilder generationTimestamp(@NonNull Optional<Instant> generationTimestamp) {
         if (generationTimestamp == null) {
            throw new NullPointerException("generationTimestamp is marked non-null but is null");
         }

         this.generationTimestamp = generationTimestamp;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RememberMeJspToken.RememberMeJspTokenBuilder lastUsedTimestamp(@NonNull Optional<Instant> lastUsedTimestamp) {
         if (lastUsedTimestamp == null) {
            throw new NullPointerException("lastUsedTimestamp is marked non-null but is null");
         }

         this.lastUsedTimestamp = lastUsedTimestamp;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RememberMeJspToken.RememberMeJspTokenBuilder time(@NonNull TimeProvider time) {
         if (time == null) {
            throw new NullPointerException("time is marked non-null but is null");
         }

         this.time = time;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public RememberMeJspToken build() {
         return new RememberMeJspToken(this.iamUserId, this.generationTimestamp, this.lastUsedTimestamp, this.time);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "RememberMeJspToken.RememberMeJspTokenBuilder(iamUserId="
            + this.iamUserId
            + ", generationTimestamp="
            + this.generationTimestamp
            + ", lastUsedTimestamp="
            + this.lastUsedTimestamp
            + ", time="
            + this.time
            + ")";
      }
   }
}
