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

import com.airlock.iam.base.api.domain.model.exception.FailureException;
import com.airlock.iam.common.api.domain.model.user.UserIdentity;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;

public final class PrincipalInfoImpl implements PrincipalInfo {
   private final Optional<UserIdentity> authenticatedIdentity;
   private final Optional<UserIdentity> tentativeIdentity;

   @Override
   public UserIdentity tryGetAuthenticatedIdentity() {
      return this.authenticatedIdentity.orElseThrow(() -> new FailureException("No authenticated user available."));
   }

   @Override
   public Optional<UserIdentity> getKnownIdentity() {
      return this.authenticatedIdentity.or(() -> this.tentativeIdentity);
   }

   public static PrincipalInfo forUnknownIdentity() {
      return new PrincipalInfoImpl(Optional.empty(), Optional.empty());
   }

   public static PrincipalInfo forAuthenticatedIdentity(UserIdentity authenticatedIdentity) {
      return new PrincipalInfoImpl(Optional.of(authenticatedIdentity), Optional.empty());
   }

   public static PrincipalInfo forTentativeIdentity(UserIdentity tentativeIdentity) {
      return new PrincipalInfoImpl(Optional.empty(), Optional.of(tentativeIdentity));
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public Optional<UserIdentity> getAuthenticatedIdentity() {
      return this.authenticatedIdentity;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Optional<UserIdentity> getTentativeIdentity() {
      return this.tentativeIdentity;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "PrincipalInfoImpl(authenticatedIdentity=" + this.getAuthenticatedIdentity() + ", tentativeIdentity=" + this.getTentativeIdentity() + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   PrincipalInfoImpl(Optional<UserIdentity> authenticatedIdentity, Optional<UserIdentity> tentativeIdentity) {
      this.authenticatedIdentity = authenticatedIdentity;
      this.tentativeIdentity = tentativeIdentity;
   }
}
