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

import com.airlock.iam.common.api.domain.model.user.UserIdentity;
import com.airlock.iam.common.domain.model.principal.UserPrincipal;
import com.airlock.iam.common.domain.service.principal.UserPrincipalService;
import com.airlock.iam.core.misc.persistency.AuthUserBean;
import com.airlock.iam.publicselfservice.api.domain.model.PublicSelfServiceRestriction;
import com.airlock.iam.publicselfservice.api.domain.model.PublicSelfServiceUser;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;

public final class PublicSelfServiceUserImpl implements PublicSelfServiceUser {
   private final UserIdentity identity;
   private final Optional<PublicSelfServiceRestriction.RestrictionViolation> restrictionViolation;
   private final UserPrincipal userPrincipal;
   private final UserPrincipalService userPrincipalService;

   @Override
   public void reloadPrincipal() {
      this.userPrincipalService.reloadPrincipal();
   }

   @Override
   public void orderPasswordLetter() {
      ((AuthUserBean)this.userPrincipal.user()).orderNewPassword(this.userPrincipal.getName());
   }

   @Override
   public boolean isPublicSelfServiceAllowed() {
      return this.restrictionViolation.isEmpty();
   }

   @Override
   public Optional<PublicSelfServiceRestriction.ViolationCode> getViolationToReport() {
      return this.restrictionViolation
         .filter(PublicSelfServiceRestriction.RestrictionViolation::isReportViolation)
         .map(PublicSelfServiceRestriction.RestrictionViolation::getCode);
   }

   @Override
   public Optional<PublicSelfServiceRestriction.ViolationCode> getViolationIfPresent() {
      return this.restrictionViolation.map(PublicSelfServiceRestriction.RestrictionViolation::getCode);
   }

   @SuppressFBWarnings(justification = "generated code")
   PublicSelfServiceUserImpl(
      UserIdentity identity,
      Optional<PublicSelfServiceRestriction.RestrictionViolation> restrictionViolation,
      UserPrincipal userPrincipal,
      UserPrincipalService userPrincipalService
   ) {
      this.identity = identity;
      this.restrictionViolation = restrictionViolation;
      this.userPrincipal = userPrincipal;
      this.userPrincipalService = userPrincipalService;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public Optional<PublicSelfServiceRestriction.RestrictionViolation> getRestrictionViolation() {
      return this.restrictionViolation;
   }

   @SuppressFBWarnings(justification = "generated code")
   public UserPrincipal getUserPrincipal() {
      return this.userPrincipal;
   }

   @SuppressFBWarnings(justification = "generated code")
   public UserPrincipalService getUserPrincipalService() {
      return this.userPrincipalService;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof PublicSelfServiceUserImpl other)) {
         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$restrictionViolation = this.restrictionViolation;
            Object other$restrictionViolation = other.restrictionViolation;
            if (this$restrictionViolation == null ? other$restrictionViolation == null : this$restrictionViolation.equals(other$restrictionViolation)) {
               Object this$userPrincipal = this.userPrincipal;
               Object other$userPrincipal = other.userPrincipal;
               if (this$userPrincipal == null ? other$userPrincipal == null : this$userPrincipal.equals(other$userPrincipal)) {
                  Object this$userPrincipalService = this.userPrincipalService;
                  Object other$userPrincipalService = other.userPrincipalService;
                  return this$userPrincipalService == null ? other$userPrincipalService == null : this$userPrincipalService.equals(other$userPrincipalService);
               } else {
                  return false;
               }
            } else {
               return false;
            }
         } else {
            return false;
         }
      }
   }

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "PublicSelfServiceUserImpl(identity="
         + this.getIdentity()
         + ", restrictionViolation="
         + this.getRestrictionViolation()
         + ", userPrincipal="
         + this.getUserPrincipal()
         + ", userPrincipalService="
         + this.getUserPrincipalService()
         + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class PublicSelfServiceUserImplBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private UserIdentity identity;
      @SuppressFBWarnings(justification = "generated code")
      private Optional<PublicSelfServiceRestriction.RestrictionViolation> restrictionViolation;
      @SuppressFBWarnings(justification = "generated code")
      private UserPrincipal userPrincipal;
      @SuppressFBWarnings(justification = "generated code")
      private UserPrincipalService userPrincipalService;

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

      @SuppressFBWarnings(justification = "generated code")
      public PublicSelfServiceUserImpl.PublicSelfServiceUserImplBuilder identity(UserIdentity identity) {
         this.identity = identity;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public PublicSelfServiceUserImpl.PublicSelfServiceUserImplBuilder restrictionViolation(
         Optional<PublicSelfServiceRestriction.RestrictionViolation> restrictionViolation
      ) {
         this.restrictionViolation = restrictionViolation;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public PublicSelfServiceUserImpl.PublicSelfServiceUserImplBuilder userPrincipal(UserPrincipal userPrincipal) {
         this.userPrincipal = userPrincipal;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public PublicSelfServiceUserImpl.PublicSelfServiceUserImplBuilder userPrincipalService(UserPrincipalService userPrincipalService) {
         this.userPrincipalService = userPrincipalService;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public PublicSelfServiceUserImpl build() {
         return new PublicSelfServiceUserImpl(this.identity, this.restrictionViolation, this.userPrincipal, this.userPrincipalService);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "PublicSelfServiceUserImpl.PublicSelfServiceUserImplBuilder(identity="
            + this.identity
            + ", restrictionViolation="
            + this.restrictionViolation
            + ", userPrincipal="
            + this.userPrincipal
            + ", userPrincipalService="
            + this.userPrincipalService
            + ")";
      }
   }
}
