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

import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import com.airlock.iam.factor.domain.model.airlock2fa.activation.Airlock2FAActivationCode;
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.Optional;
import lombok.NonNull;

public class Airlock2FAActivationFlowBindingStepSession implements StepSession {
   private static final String ACTIVATION_CODE_KEY = "activationCode";
   private final PollingResultCache pollingCache;
   private Optional<Airlock2FAActivationCode> activationCode = Optional.empty();

   public static Airlock2FAActivationFlowBindingStepSession emptySession() {
      return new Airlock2FAActivationFlowBindingStepSession(new PollingResultCache());
   }

   public void setActivationCode(@NonNull Airlock2FAActivationCode activationCode) {
      if (activationCode == null) {
         throw new NullPointerException("activationCode is marked non-null but is null");
      }

      this.activationCode = Optional.of(activationCode);
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .putIfPresent("activationCode", this.activationCode.map(Airlock2FAActivationCode::getValue))
         .putIfPresent("pollingCache", this.pollingCache.mapify())
         .build();
   }

   public static Airlock2FAActivationFlowBindingStepSession demapify(MapifiedValues values) {
      return new Airlock2FAActivationFlowBindingStepSession(
         PollingResultCache.demapifyPollingCache(values.getOptionalValues("pollingCache")),
         values.<String>getOptional("activationCode").map(Airlock2FAActivationCode::airlock2FAActivationCode)
      );
   }

   @SuppressFBWarnings(justification = "generated code")
   public Airlock2FAActivationFlowBindingStepSession(PollingResultCache pollingCache, Optional<Airlock2FAActivationCode> activationCode) {
      this.pollingCache = pollingCache;
      this.activationCode = activationCode;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public Optional<Airlock2FAActivationCode> getActivationCode() {
      return this.activationCode;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof Airlock2FAActivationFlowBindingStepSession other)) {
         return false;
      } else if (!other.canEqual(this)) {
         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$activationCode = this.activationCode;
            Object other$activationCode = other.activationCode;
            return this$activationCode == null ? other$activationCode == null : this$activationCode.equals(other$activationCode);
         } else {
            return false;
         }
      }
   }

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

   @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 $activationCode = this.activationCode;
      return result * 59 + ($activationCode == null ? 43 : $activationCode.hashCode());
   }
}
