package com.airlock.iam.common.domain.model.e2ee;

import com.airlock.iam.base.api.domain.model.exception.FailureException;
import com.airlock.iam.base.api.domain.model.mapification.Mapifiable;
import com.airlock.iam.base.api.domain.model.mapification.MapifiedValues;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import com.airlock.iam.common.api.domain.model.e2ee.E2eEncryptionSession;
import com.nimbusds.jose.jwk.JWK;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.text.ParseException;

public final class JweE2eEncryptionSession implements E2eEncryptionSession, Mapifiable {
   private static final String NONCE_KEY = "nonce";
   private static final String JWK_KEY = "jwk";
   private String nonce;
   private final JWK publicKey;

   public JwePublicE2eEncryptionInformation getPublicEncryptionInformation() {
      return new JwePublicE2eEncryptionInformation(this.nonce, this.publicKey.toJSONObject());
   }

   public void resetNonce(String newNonce) {
      this.nonce = newNonce;
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues().put("nonce", this.nonce).put("jwk", this.publicKey.toJSONObject()).build();
   }

   public static JweE2eEncryptionSession demapify(MapifiedValues values) {
      return new JweE2eEncryptionSession(values.get("nonce"), parseKey(values.getValues("jwk")));
   }

   private static JWK parseKey(MapifiedValues values) {
      try {
         return JWK.parse(values.asMap());
      } catch (ParseException e) {
         throw new FailureException(e);
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   String getNonce() {
      return this.nonce;
   }

   @SuppressFBWarnings(justification = "generated code")
   JWK getPublicKey() {
      return this.publicKey;
   }

   @SuppressFBWarnings(justification = "generated code")
   public JweE2eEncryptionSession(String nonce, JWK publicKey) {
      this.nonce = nonce;
      this.publicKey = publicKey;
   }

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

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