package com.airlock.iam.flow.shared.domain.model.termsofservice;

import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import com.airlock.iam.common.domain.model.termsofservice.TermsOfServiceTag;
import com.airlock.iam.flow.api.domain.model.step.StepSession;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.NonNull;

public class TermsOfServiceStepSession implements StepSession {
   private static final String RETRIEVED_TAGS_KEY = "retrievedTags";
   private static final String RETRIEVE_CALLED_KEY = "retrieveCalled";
   @NonNull
   private final Set<TermsOfServiceTag> retrievedTags;
   private boolean retrieveCalled;

   public TermsOfServiceStepSession() {
      this.retrievedTags = new HashSet<>();
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .put("retrievedTags", this.retrievedTags.stream().map(TermsOfServiceTag::getName).toList())
         .put("retrieveCalled", this.retrieveCalled)
         .build();
   }

   public static TermsOfServiceStepSession demapify(MapifiedValues values) {
      return new TermsOfServiceStepSession(
         values.<String>getList("retrievedTags").stream().map(TermsOfServiceTag::new).collect(Collectors.toSet()), values.<Boolean>get("retrieveCalled")
      );
   }

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public Set<TermsOfServiceTag> getRetrievedTags() {
      return this.retrievedTags;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public void setRetrieveCalled(boolean retrieveCalled) {
      this.retrieveCalled = retrieveCalled;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "TermsOfServiceStepSession(retrievedTags=" + this.getRetrievedTags() + ", retrieveCalled=" + this.isRetrieveCalled() + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   public TermsOfServiceStepSession(@NonNull Set<TermsOfServiceTag> retrievedTags) {
      if (retrievedTags == null) {
         throw new NullPointerException("retrievedTags is marked non-null but is null");
      }

      this.retrievedTags = retrievedTags;
   }

   @SuppressFBWarnings(justification = "generated code")
   public TermsOfServiceStepSession(@NonNull Set<TermsOfServiceTag> retrievedTags, boolean retrieveCalled) {
      if (retrievedTags == null) {
         throw new NullPointerException("retrievedTags is marked non-null but is null");
      }

      this.retrievedTags = retrievedTags;
      this.retrieveCalled = retrieveCalled;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof TermsOfServiceStepSession other)) {
         return false;
      } else {
         if (!other.canEqual(this)) {
            return false;
         }

         if (this.retrieveCalled != other.retrieveCalled) {
            return false;
         }

         Object this$retrievedTags = this.retrievedTags;
         Object other$retrievedTags = other.retrievedTags;
         return this$retrievedTags == null ? other$retrievedTags == null : this$retrievedTags.equals(other$retrievedTags);
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   protected boolean canEqual(Object other) {
      return other instanceof TermsOfServiceStepSession;
   }

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