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

import com.airlock.iam.base.misc.time.TimeProvider;
import com.airlock.iam.common.api.domain.model.otp.OneTimePassword;
import com.airlock.iam.common.api.domain.model.otp.Otp;
import com.airlock.iam.common.api.domain.model.otp.OtpEqualityStrategy;
import com.airlock.iam.common.api.domain.model.otp.UnverifiedOtp;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.time.Duration;
import java.time.Instant;

public final class ExpiringOneTimePassword implements OneTimePassword {
   private final Otp value;
   private final Instant expiresAt;
   private final Duration validity;
   private final TimeProvider timeProvider;

   ExpiringOneTimePassword(Otp value, Duration validity, TimeProvider timeProvider) {
      this.value = value;
      this.validity = validity;
      this.timeProvider = timeProvider;
      this.expiresAt = timeProvider.getCurrentInstant().plus(validity);
   }

   @Override
   public OneTimePassword.OneTimeCodeVerificationResult verify(UnverifiedOtp otherValue, OtpEqualityStrategy strategy) {
      if (this.isExpired()) {
         return OneTimePassword.OneTimeCodeVerificationResult.EXPIRED;
      } else {
         return strategy.equals(this.value, otherValue)
            ? OneTimePassword.OneTimeCodeVerificationResult.SUCCESS
            : OneTimePassword.OneTimeCodeVerificationResult.WRONG_VALUE;
      }
   }

   private boolean isExpired() {
      return this.now().isAfter(this.expiresAt);
   }

   public ExpiringOneTimePassword renewValidity() {
      return new ExpiringOneTimePassword(this.value, this.validity, this.timeProvider);
   }

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

   private Instant now() {
      return this.timeProvider.getCurrentInstant();
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public Instant getExpiresAt() {
      return this.expiresAt;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Duration getValidity() {
      return this.validity;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public ExpiringOneTimePassword(Otp value, Instant expiresAt, Duration validity, TimeProvider timeProvider) {
      this.value = value;
      this.expiresAt = expiresAt;
      this.validity = validity;
      this.timeProvider = timeProvider;
   }

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

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