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

import com.airlock.iam.base.api.domain.model.exception.FailureException;
import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.base.api.domain.model.user.UserId;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import com.airlock.iam.factor.domain.model.fido.FidoPublicKeyCredentialRequestOptions;
import com.airlock.iam.factor.domain.model.fido.user.FidoUserHandle;
import com.airlock.iam.factor.domain.model.fido.user.FidoUserPrincipal;
import com.airlock.iam.fido.api.domain.model.FidoRelyingPartyId;
import com.airlock.iam.flow.api.domain.model.step.StepSession;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;
import org.apache.commons.lang3.Validate;

public final class FidoCheckingStepSession implements StepSession {
   private static final String OPTIONS_KEY = "options";
   private static final String USER_ID_KEY = "userId";
   private static final String USER_HANDLE_KEY = "userHandle";
   private static final String RP_KEY = "rp";
   private Optional<FidoPublicKeyCredentialRequestOptions> challenge = Optional.empty();
   private Optional<FidoUserPrincipal> fidoUser = Optional.empty();

   static FidoCheckingStepSession emptySession() {
      return new FidoCheckingStepSession();
   }

   public FidoPublicKeyCredentialRequestOptions getChallenge() {
      return this.challenge.orElseThrow(() -> new FailureException("Challenge not initialized"));
   }

   public void saveChallenge(FidoPublicKeyCredentialRequestOptions challenge) {
      Validate.isTrue(this.challenge.isEmpty(), "challenge can only be set once", new Object[0]);
      this.challenge = Optional.of(challenge);
   }

   public void saveUser(FidoUserPrincipal user) {
      this.fidoUser = Optional.of(user);
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .putIfPresent("options", this.challenge.map(FidoPublicKeyCredentialRequestOptions::mapify))
         .putIfPresent("userId", this.fidoUser.map(FidoUserPrincipal::getIamUserId).map(UserId::getValue))
         .putIfPresent("userHandle", this.fidoUser.map(FidoUserPrincipal::getUserHandle).map(FidoUserHandle::valueAsString))
         .putIfPresent("rp", this.fidoUser.map(FidoUserPrincipal::getRelyingPartyId).map(FidoRelyingPartyId::getId))
         .build();
   }

   public static FidoCheckingStepSession demapify(MapifiedValues values) {
      return new FidoCheckingStepSession(values.demapifyOptional("options", FidoPublicKeyCredentialRequestOptions::demapify), demapifyUser(values));
   }

   private static Optional<FidoUserPrincipal> demapifyUser(MapifiedValues values) {
      return !values.containsKey("userId")
         ? Optional.empty()
         : Optional.of(
            FidoUserPrincipal.fidoUser(
               values.simpleValue("rp", FidoRelyingPartyId::relyingPartyId),
               values.simpleValue("userId", UserId::userId),
               values.simpleValue("userHandle", FidoUserHandle::userHandleFromBase64Url)
            )
         );
   }

   @SuppressFBWarnings(justification = "generated code")
   public Optional<FidoUserPrincipal> getFidoUser() {
      return this.fidoUser;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   private FidoCheckingStepSession() {
   }

   @SuppressFBWarnings(justification = "generated code")
   private FidoCheckingStepSession(Optional<FidoPublicKeyCredentialRequestOptions> challenge, Optional<FidoUserPrincipal> fidoUser) {
      this.challenge = challenge;
      this.fidoUser = fidoUser;
   }
}
