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

import com.airlock.iam.base.api.domain.model.event.EventId;
import com.airlock.iam.base.api.domain.model.event.EventType;
import com.airlock.iam.base.api.domain.model.event.metadata.EventMetadata;
import com.airlock.iam.base.api.domain.model.event.source.EventSource;
import com.airlock.iam.base.api.domain.model.user.UserId;
import com.airlock.iam.base.domain.model.event.EventCreationData;
import com.airlock.iam.common.api.domain.model.event.ContextDataChanged;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.time.Instant;
import java.util.Set;
import lombok.NonNull;

public final class ContextDataChangedImpl implements ContextDataChanged {
   private final EventCreationData eventCreationData;
   private final UserId userId;
   private final Set<ContextDataChanged.ContextDataChange> changes;

   public ContextDataChangedImpl(
      @NonNull EventCreationData eventCreationData, @NonNull UserId userId, @NonNull Set<ContextDataChanged.ContextDataChange> changes
   ) {
      if (eventCreationData == null) {
         throw new NullPointerException("eventCreationData is marked non-null but is null");
      }

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

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

      this.eventCreationData = eventCreationData;
      this.userId = userId;
      this.changes = changes;
      if (changes.isEmpty()) {
         throw new IllegalArgumentException("The set of changes must not be empty in a context data changed event.");
      }
   }

   @Override
   public EventId id() {
      return this.eventCreationData.id();
   }

   @Override
   public Instant createdAt() {
      return this.eventCreationData.createdAt();
   }

   @Override
   public EventSource source() {
      return this.eventCreationData.source();
   }

   @Override
   public EventMetadata metadata() {
      return this.eventCreationData.metadata();
   }

   @Override
   public String displayName() {
      return "Context Data Changed";
   }

   @Override
   public EventType type() {
      return CommonEventTypes.CONTEXT_DATA_CHANGED;
   }

   @SuppressFBWarnings(justification = "generated code")
   public EventCreationData eventCreationData() {
      return this.eventCreationData;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public UserId userId() {
      return this.userId;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public Set<ContextDataChanged.ContextDataChange> changes() {
      return this.changes;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "ContextDataChangedImpl(eventCreationData=" + this.eventCreationData() + ", userId=" + this.userId() + ", changes=" + this.changes() + ")";
   }
}
