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

import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import com.airlock.iam.flow.api.domain.model.step.StepSession;
import com.airlock.iam.flow.shared.domain.model.session.PollingResultCache;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Set;

public final class CrontoPushActivationStepSession implements StepSession {
   private static final String DEVICES_KEY = "devices";
   private static final String CHALLENGE_KEY = "challenge";
   private final PollingResultCache pollingCache;
   private final Set<String> alreadyActivatedDevices;
   private final CrontoPushActivationChallenge challenge;

   public static CrontoPushActivationStepSession createNewSession(Set<String> alreadyActivatedDevices, CrontoPushActivationChallenge challenge) {
      return new CrontoPushActivationStepSession(new PollingResultCache(), alreadyActivatedDevices, challenge);
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .putIfNotEmpty("devices", this.alreadyActivatedDevices)
         .put("challenge", this.challenge.mapify())
         .putIfPresent("pollingCache", this.pollingCache.mapify())
         .build();
   }

   public static CrontoPushActivationStepSession demapify(MapifiedValues values) {
      return new CrontoPushActivationStepSession(
         PollingResultCache.demapifyPollingCache(values.getOptionalValues("pollingCache")),
         values.demapifySimpleSet("devices", String.class::cast),
         values.demapifyValue("challenge", CrontoPushActivationChallenge::demapify)
      );
   }

   @SuppressFBWarnings(justification = "generated code")
   public CrontoPushActivationStepSession(PollingResultCache pollingCache, Set<String> alreadyActivatedDevices, CrontoPushActivationChallenge challenge) {
      this.pollingCache = pollingCache;
      this.alreadyActivatedDevices = alreadyActivatedDevices;
      this.challenge = challenge;
   }

   @SuppressFBWarnings(justification = "generated code")
   public PollingResultCache getPollingCache() {
      return this.pollingCache;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Set<String> getAlreadyActivatedDevices() {
      return this.alreadyActivatedDevices;
   }

   @SuppressFBWarnings(justification = "generated code")
   public CrontoPushActivationChallenge getChallenge() {
      return this.challenge;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "CrontoPushActivationStepSession(pollingCache="
         + this.getPollingCache()
         + ", alreadyActivatedDevices="
         + this.getAlreadyActivatedDevices()
         + ", challenge="
         + this.getChallenge()
         + ")";
   }
}
