package com.airlock.iam.common.application.service.authorization;

import com.airlock.iam.common.api.domain.model.user.UserIdentity;
import com.airlock.iam.core.misc.authorization.AccessControllerException;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;

public final class AccessCheckResult {
   private final boolean success;
   private final Optional<UserIdentity> identity;
   private final Optional<AccessControllerException> exception;

   public static AccessCheckResult notConfiguredAccessCheck() {
      return new AccessCheckResult(true, Optional.empty(), Optional.empty());
   }

   public static AccessCheckResult successfulAccessCheck(UserIdentity identity) {
      return new AccessCheckResult(true, Optional.ofNullable(identity), Optional.empty());
   }

   public static AccessCheckResult failedAccessCheck(UserIdentity identity) {
      return new AccessCheckResult(false, Optional.ofNullable(identity), Optional.empty());
   }

   public static AccessCheckResult faultyAccessCheck(AccessControllerException accessControllerException) {
      return new AccessCheckResult(false, Optional.empty(), Optional.of(accessControllerException));
   }

   private AccessCheckResult(boolean wasSuccess, Optional<UserIdentity> identity, Optional<AccessControllerException> exception) {
      this.success = wasSuccess;
      this.identity = identity;
      this.exception = exception;
   }

   @SuppressFBWarnings(justification = "generated code")
   public boolean isSuccess() {
      return this.success;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public Optional<AccessControllerException> getException() {
      return this.exception;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "AccessCheckResult(success=" + this.isSuccess() + ", identity=" + this.getIdentity() + ", exception=" + this.getException() + ")";
   }
}
