package com.airlock.iam.publicselfservice.domain.model.flow.session;

import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import com.airlock.iam.core.misc.impl.cronto.SimulatedCrontoChallenge;
import com.airlock.iam.flow.api.domain.model.step.StepSession;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class CrontoApprovalStealthStepSession implements StepSession {
   private static final String MAX_RESPONSE_RETRIES_KEY = "maxResponseRetries";
   private static final String CURRENT_CHALLENGE_KEY = "currentChallenge";
   private static final String NUMBER_OF_TRIES_KEY = "numberOfTries";
   private static final String FAILED_DEFINITELY_KEY = "failedDefinitely";
   private final int maxResponseRetries;
   private final SimulatedCrontoChallenge currentChallenge;
   private int numberOfTries;
   private boolean failedDefinitely;

   public void increaseNumberOfTries() {
      this.numberOfTries++;
      if (this.numberOfTries > this.maxResponseRetries) {
         this.failedDefinitely = true;
      }
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .put("maxResponseRetries", this.maxResponseRetries)
         .put("currentChallenge", this.currentChallenge.mapify())
         .put("numberOfTries", this.numberOfTries)
         .put("failedDefinitely", this.failedDefinitely)
         .build();
   }

   public static CrontoApprovalStealthStepSession demapify(MapifiedValues values) {
      return new CrontoApprovalStealthStepSession(
         values.<Integer>get("maxResponseRetries"),
         values.demapifyValue("currentChallenge", SimulatedCrontoChallenge::demapify),
         values.<Integer>get("numberOfTries"),
         values.<Boolean>get("failedDefinitely")
      );
   }

   @SuppressFBWarnings(justification = "generated code")
   public int getMaxResponseRetries() {
      return this.maxResponseRetries;
   }

   @SuppressFBWarnings(justification = "generated code")
   public SimulatedCrontoChallenge getCurrentChallenge() {
      return this.currentChallenge;
   }

   @SuppressFBWarnings(justification = "generated code")
   public int getNumberOfTries() {
      return this.numberOfTries;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public void setNumberOfTries(int numberOfTries) {
      this.numberOfTries = numberOfTries;
   }

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

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

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

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

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

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

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "CrontoApprovalStealthStepSession(maxResponseRetries="
         + this.getMaxResponseRetries()
         + ", currentChallenge="
         + this.getCurrentChallenge()
         + ", numberOfTries="
         + this.getNumberOfTries()
         + ", failedDefinitely="
         + this.isFailedDefinitely()
         + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   public CrontoApprovalStealthStepSession(int maxResponseRetries, SimulatedCrontoChallenge currentChallenge) {
      this.maxResponseRetries = maxResponseRetries;
      this.currentChallenge = currentChallenge;
   }

   @SuppressFBWarnings(justification = "generated code")
   public CrontoApprovalStealthStepSession(int maxResponseRetries, SimulatedCrontoChallenge currentChallenge, int numberOfTries, boolean failedDefinitely) {
      this.maxResponseRetries = maxResponseRetries;
      this.currentChallenge = currentChallenge;
      this.numberOfTries = numberOfTries;
      this.failedDefinitely = failedDefinitely;
   }
}
