package com.airlock.iam.ssi.domain.model.step;

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 com.airlock.iam.ssi.domain.model.SsiVerificationSession;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.URI;
import java.util.Optional;

public final class SsiVerificationStepSession implements StepSession {
   private static final String INVITATION_QR_CODE_STRING_KEY = "qrCode";
   private static final String INVITATION_URL_KEY = "invitationUrl";
   private static final String VERIFICATION_SESSION_KEY = "verificationSession";
   private final PollingResultCache verificationCache;
   private Optional<URI> invitationLink = Optional.empty();
   private Optional<String> invitationQrCode = Optional.empty();
   private Optional<SsiVerificationSession> verificationSession = Optional.empty();

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

   public void proofRequested(SsiVerificationSession verificationSession, URI link, String qrCode) {
      this.verificationSession = Optional.of(verificationSession);
      this.invitationLink = Optional.of(link);
      this.invitationQrCode = Optional.of(qrCode);
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .putIfPresent("qrCode", this.invitationQrCode)
         .putIfPresent("invitationUrl", this.invitationLink.map(URI::toString))
         .putIfPresent("verificationSession", this.verificationSession.map(SsiSessionMapifier::mapifySsiVerificationSession))
         .putIfPresent("pollingCache", this.verificationCache.mapify())
         .build();
   }

   public static SsiVerificationStepSession demapify(MapifiedValues values) {
      return new SsiVerificationStepSession(
         PollingResultCache.demapifyPollingCache(values.getOptionalValues("pollingCache")),
         values.<Object>getOptional("invitationUrl").map(String.class::cast).map(URI::create),
         values.getOptional("qrCode"),
         values.demapifyOptional("verificationSession", SsiSessionMapifier::demapifySsiVerificationSession)
      );
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public Optional<URI> getInvitationLink() {
      return this.invitationLink;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Optional<String> getInvitationQrCode() {
      return this.invitationQrCode;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Optional<SsiVerificationSession> getVerificationSession() {
      return this.verificationSession;
   }

   @SuppressFBWarnings(justification = "generated code")
   public SsiVerificationStepSession(
      PollingResultCache verificationCache,
      Optional<URI> invitationLink,
      Optional<String> invitationQrCode,
      Optional<SsiVerificationSession> verificationSession
   ) {
      this.verificationCache = verificationCache;
      this.invitationLink = invitationLink;
      this.invitationQrCode = invitationQrCode;
      this.verificationSession = verificationSession;
   }

   @SuppressFBWarnings(justification = "generated code")
   public SsiVerificationStepSession(PollingResultCache verificationCache) {
      this.verificationCache = verificationCache;
   }

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

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