package com.airlock.iam.techclient.auth.domain.model.oauth2;

import com.airlock.iam.base.api.domain.model.exception.FailureException;
import com.airlock.iam.base.api.domain.model.identity.Identity;
import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.common.api.domain.model.locking.LockInfo;
import com.airlock.iam.techclient.api.domain.model.TechnicalClient;
import com.airlock.iam.techclient.api.domain.model.oauth2.OAuth2TechnicalClientMetadata;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.time.Instant;
import java.util.Optional;
import lombok.NonNull;

public final class LoginOAuth2TechClientImpl implements MutableOAuth2TechClient {
   @NonNull
   private final Identity<TechnicalClient> id;
   private String displayName;
   private String description;
   private String label;
   @NonNull
   private Optional<LockInfo> lockInfo;
   @NonNull
   private OAuth2TechnicalClientMetadata metadata;

   @Override
   public Optional<String> getDescription() {
      return Optional.ofNullable(this.description);
   }

   @Override
   public Optional<String> getLabel() {
      return Optional.ofNullable(this.label);
   }

   @Override
   public void lock(String reason) {
      this.lockInfo = Optional.of(new LockInfo(reason, Instant.now()));
   }

   @Override
   public void unlock() {
      this.lockInfo = Optional.empty();
   }

   public static MutableOAuth2TechClient asMutable(OAuth2TechClient client) {
      if (client instanceof LoginOAuth2TechClientImpl) {
         return (LoginOAuth2TechClientImpl)client;
      } else {
         throw new FailureException("Unsupported implementation of OAuth2TechnicalClient.");
      }
   }

   @Override
   public MapifiedValues mapify() {
      throw new UnsupportedOperationException("LoginOAuth2TechClientImpl cannot be mapified");
   }

   public static LoginOAuth2TechClientImpl demapify(MapifiedValues values) {
      throw new UnsupportedOperationException("LoginOAuth2TechClientImpl cannot be demapified");
   }

   @SuppressFBWarnings(justification = "generated code")
   LoginOAuth2TechClientImpl(
      @NonNull Identity<TechnicalClient> id,
      String displayName,
      String description,
      String label,
      @NonNull Optional<LockInfo> lockInfo,
      @NonNull OAuth2TechnicalClientMetadata metadata
   ) {
      if (id == null) {
         throw new NullPointerException("id is marked non-null but is null");
      }

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

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

      this.id = id;
      this.displayName = displayName;
      this.description = description;
      this.label = label;
      this.lockInfo = lockInfo;
      this.metadata = metadata;
   }

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

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   @Override
   public Identity<TechnicalClient> getId() {
      return this.id;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String getDisplayName() {
      return this.displayName;
   }

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   @Override
   public Optional<LockInfo> getLockInfo() {
      return this.lockInfo;
   }

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   @Override
   public OAuth2TechnicalClientMetadata getMetadata() {
      return this.metadata;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public void setDisplayName(String displayName) {
      this.displayName = displayName;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public void setDescription(String description) {
      this.description = description;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public void setLabel(String label) {
      this.label = label;
   }

   @SuppressFBWarnings(justification = "generated code")
   public void setLockInfo(@NonNull Optional<LockInfo> lockInfo) {
      if (lockInfo == null) {
         throw new NullPointerException("lockInfo is marked non-null but is null");
      }

      this.lockInfo = lockInfo;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public void setMetadata(@NonNull OAuth2TechnicalClientMetadata metadata) {
      if (metadata == null) {
         throw new NullPointerException("metadata is marked non-null but is null");
      }

      this.metadata = metadata;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof LoginOAuth2TechClientImpl other)) {
         return false;
      } else {
         Object this$id = this.id;
         Object other$id = other.id;
         if (this$id == null ? other$id == null : this$id.equals(other$id)) {
            Object this$displayName = this.displayName;
            Object other$displayName = other.displayName;
            if (this$displayName == null ? other$displayName == null : this$displayName.equals(other$displayName)) {
               Object this$description = this.description;
               Object other$description = other.description;
               if (this$description == null ? other$description == null : this$description.equals(other$description)) {
                  Object this$label = this.label;
                  Object other$label = other.label;
                  if (this$label == null ? other$label == null : this$label.equals(other$label)) {
                     Object this$lockInfo = this.lockInfo;
                     Object other$lockInfo = other.lockInfo;
                     if (this$lockInfo == null ? other$lockInfo == null : this$lockInfo.equals(other$lockInfo)) {
                        Object this$metadata = this.metadata;
                        Object other$metadata = other.metadata;
                        return this$metadata == null ? other$metadata == null : this$metadata.equals(other$metadata);
                     } else {
                        return false;
                     }
                  } else {
                     return false;
                  }
               } else {
                  return false;
               }
            } else {
               return false;
            }
         } else {
            return false;
         }
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public int hashCode() {
      int PRIME = 59;
      int result = 1;
      Object $id = this.id;
      result = result * 59 + ($id == null ? 43 : $id.hashCode());
      Object $displayName = this.displayName;
      result = result * 59 + ($displayName == null ? 43 : $displayName.hashCode());
      Object $description = this.description;
      result = result * 59 + ($description == null ? 43 : $description.hashCode());
      Object $label = this.label;
      result = result * 59 + ($label == null ? 43 : $label.hashCode());
      Object $lockInfo = this.lockInfo;
      result = result * 59 + ($lockInfo == null ? 43 : $lockInfo.hashCode());
      Object $metadata = this.metadata;
      return result * 59 + ($metadata == null ? 43 : $metadata.hashCode());
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "LoginOAuth2TechClientImpl(id="
         + this.getId()
         + ", displayName="
         + this.getDisplayName()
         + ", description="
         + this.getDescription()
         + ", label="
         + this.getLabel()
         + ", lockInfo="
         + this.getLockInfo()
         + ", metadata="
         + this.getMetadata()
         + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class LoginOAuth2TechClientImplBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private Identity<TechnicalClient> id;
      @SuppressFBWarnings(justification = "generated code")
      private String displayName;
      @SuppressFBWarnings(justification = "generated code")
      private String description;
      @SuppressFBWarnings(justification = "generated code")
      private String label;
      @SuppressFBWarnings(justification = "generated code")
      private Optional<LockInfo> lockInfo;
      @SuppressFBWarnings(justification = "generated code")
      private OAuth2TechnicalClientMetadata metadata;

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

      @SuppressFBWarnings(justification = "generated code")
      public LoginOAuth2TechClientImpl.LoginOAuth2TechClientImplBuilder id(@NonNull Identity<TechnicalClient> id) {
         if (id == null) {
            throw new NullPointerException("id is marked non-null but is null");
         }

         this.id = id;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LoginOAuth2TechClientImpl.LoginOAuth2TechClientImplBuilder displayName(String displayName) {
         this.displayName = displayName;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LoginOAuth2TechClientImpl.LoginOAuth2TechClientImplBuilder description(String description) {
         this.description = description;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LoginOAuth2TechClientImpl.LoginOAuth2TechClientImplBuilder label(String label) {
         this.label = label;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LoginOAuth2TechClientImpl.LoginOAuth2TechClientImplBuilder lockInfo(@NonNull Optional<LockInfo> lockInfo) {
         if (lockInfo == null) {
            throw new NullPointerException("lockInfo is marked non-null but is null");
         }

         this.lockInfo = lockInfo;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LoginOAuth2TechClientImpl.LoginOAuth2TechClientImplBuilder metadata(@NonNull OAuth2TechnicalClientMetadata metadata) {
         if (metadata == null) {
            throw new NullPointerException("metadata is marked non-null but is null");
         }

         this.metadata = metadata;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LoginOAuth2TechClientImpl build() {
         return new LoginOAuth2TechClientImpl(this.id, this.displayName, this.description, this.label, this.lockInfo, this.metadata);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "LoginOAuth2TechClientImpl.LoginOAuth2TechClientImplBuilder(id="
            + this.id
            + ", displayName="
            + this.displayName
            + ", description="
            + this.description
            + ", label="
            + this.label
            + ", lockInfo="
            + this.lockInfo
            + ", metadata="
            + this.metadata
            + ")";
      }
   }
}
