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

import com.airlock.iam.base.api.domain.model.exception.FailureException;
import com.airlock.iam.base.api.domain.model.identity.Identity;
import com.airlock.iam.common.api.domain.model.certificate.ClientCertificate;
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.certificate.ClientCertificates;
import com.airlock.iam.techclient.api.domain.model.certificate.MutableClientCertificates;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Supplier;
import lombok.NonNull;

public final class LoginClientCertificateTechClientImpl implements MutableClientCertificateTechClient {
   @NonNull
   private final Identity<TechnicalClient> id;
   private String displayName;
   private String description;
   private String label;
   @NonNull
   private Optional<LockInfo> lockInfo;
   private List<ClientCertificate> clientCertificates;

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

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

   private LoginClientCertificateTechClientImpl(
      Identity<TechnicalClient> id,
      String displayName,
      String description,
      String label,
      Optional<LockInfo> lockInfo,
      List<ClientCertificate> clientCertificates
   ) {
      if (clientCertificates.isEmpty()) {
         throw new FailureException("A LoginClientCertificateTechnicalClientImpl must have at least one client certificate.");
      }

      this.id = Objects.requireNonNull(id);
      this.displayName = Objects.requireNonNull(displayName);
      this.description = description;
      this.label = label;
      this.lockInfo = Objects.requireNonNull(lockInfo);
      this.clientCertificates = new ArrayList<>(clientCertificates);
   }

   @Override
   public Set<ClientCertificate> getClientCertificates() {
      return Set.copyOf(this.clientCertificates);
   }

   @Override
   public void addCertificate(ClientCertificate certificate) {
      this.clientCertificates.add(certificate);
   }

   @Override
   public void removeCertificate(ClientCertificate certificate) {
      this.clientCertificates.remove(certificate);
   }

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

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

   @Override
   public void removeAllCertificates() {
      this.clientCertificates.clear();
   }

   public static MutableClientCertificateTechClient asMutable(ClientCertificateTechClient client) {
      if (client instanceof LoginClientCertificateTechClientImpl) {
         return (LoginClientCertificateTechClientImpl)client;
      } else {
         throw new FailureException("Unsupported implementation of ClientCertificateTechnicalClient.");
      }
   }

   public static MutableClientCertificates mutableCertificatesFor(final MutableClientCertificateTechClient client) {
      return new MutableClientCertificates() {
         @Override
         public void clear() {
            client.removeAllCertificates();
         }

         @Override
         public void remove(ClientCertificate clientCertificate) {
            client.removeCertificate(clientCertificate);
         }

         @Override
         public void add(ClientCertificate clientCertificate) {
            client.addCertificate(clientCertificate);
         }

         @Override
         public Set<ClientCertificate> getClientCertificates() {
            return client.getClientCertificates();
         }

         @Override
         public int hashCode() {
            return client.getClientCertificates().hashCode();
         }

         @Override
         public boolean equals(Object obj) {
            return obj instanceof ClientCertificates ? client.getClientCertificates().equals(((ClientCertificates)obj).getClientCertificates()) : false;
         }
      };
   }

   public static ClientCertificates immutableCertificatesFor(final ClientCertificateTechClient client) {
      return new ClientCertificates() {
         @Override
         public Set<ClientCertificate> getClientCertificates() {
            return Collections.unmodifiableSet(client.getClientCertificates());
         }

         @Override
         public int hashCode() {
            return client.getClientCertificates().hashCode();
         }

         @Override
         public boolean equals(Object obj) {
            return obj instanceof ClientCertificates ? client.getClientCertificates().equals(((ClientCertificates)obj).getClientCertificates()) : false;
         }
      };
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public LoginClientCertificateTechClientImpl.LoginClientCertificateTechClientImplBuilder toBuilder() {
      LoginClientCertificateTechClientImpl.LoginClientCertificateTechClientImplBuilder builder = new LoginClientCertificateTechClientImpl.LoginClientCertificateTechClientImplBuilder()
         .id(this.id)
         .displayName(this.displayName)
         .description(this.description)
         .label(this.label)
         .lockInfo(this.lockInfo);
      if (this.clientCertificates != null) {
         builder.clientCertificates(this.clientCertificates);
      }

      return builder;
   }

   @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;
   }

   @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")
   public void setClientCertificates(List<ClientCertificate> clientCertificates) {
      this.clientCertificates = clientCertificates;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof LoginClientCertificateTechClientImpl 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$clientCertificates = this.clientCertificates;
                        Object other$clientCertificates = other.clientCertificates;
                        return this$clientCertificates == null ? other$clientCertificates == null : this$clientCertificates.equals(other$clientCertificates);
                     } 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 $clientCertificates = this.clientCertificates;
      return result * 59 + ($clientCertificates == null ? 43 : $clientCertificates.hashCode());
   }

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

   public static class LoginClientCertificateTechClientImplBuilder implements Supplier<ClientCertificateTechClient> {
      @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 ArrayList<ClientCertificate> clientCertificates;
      private Optional<LockInfo> lockInfo = Optional.empty();

      public LoginClientCertificateTechClientImpl.LoginClientCertificateTechClientImplBuilder lockInfo(LockInfo lockInformation) {
         this.lockInfo = Optional.of(lockInformation);
         return this;
      }

      public LoginClientCertificateTechClientImpl.LoginClientCertificateTechClientImplBuilder lockInfo(Optional<LockInfo> lockInformation) {
         this.lockInfo = lockInformation;
         return this;
      }

      public ClientCertificateTechClient get() {
         return this.build();
      }

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

      @SuppressFBWarnings(justification = "generated code")
      public LoginClientCertificateTechClientImpl.LoginClientCertificateTechClientImplBuilder 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 LoginClientCertificateTechClientImpl.LoginClientCertificateTechClientImplBuilder displayName(String displayName) {
         this.displayName = displayName;
         return this;
      }

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

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

      @SuppressFBWarnings(justification = "generated code")
      public LoginClientCertificateTechClientImpl.LoginClientCertificateTechClientImplBuilder clientCertificate(ClientCertificate clientCertificate) {
         if (this.clientCertificates == null) {
            this.clientCertificates = new ArrayList<>();
         }

         this.clientCertificates.add(clientCertificate);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LoginClientCertificateTechClientImpl.LoginClientCertificateTechClientImplBuilder clientCertificates(
         Collection<? extends ClientCertificate> clientCertificates
      ) {
         if (clientCertificates == null) {
            throw new NullPointerException("clientCertificates cannot be null");
         }

         if (this.clientCertificates == null) {
            this.clientCertificates = new ArrayList<>();
         }

         this.clientCertificates.addAll(clientCertificates);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LoginClientCertificateTechClientImpl.LoginClientCertificateTechClientImplBuilder clearClientCertificates() {
         if (this.clientCertificates != null) {
            this.clientCertificates.clear();
         }

         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public LoginClientCertificateTechClientImpl build() {
         return new LoginClientCertificateTechClientImpl(
            this.id,
            this.displayName,
            this.description,
            this.label,
            this.lockInfo,
            switch (this.clientCertificates == null ? 0 : this.clientCertificates.size()) {
               case 0 -> Collections.emptyList();
               case 1 -> Collections.singletonList(this.clientCertificates.get(0));
               default -> Collections.unmodifiableList(new ArrayList<>(this.clientCertificates));
            }
         );
      }

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