package com.airlock.iam.oauth2.domain.model.client;

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.domain.model.provider.ProviderId;
import com.airlock.iam.oauth2.domain.model.State;
import com.airlock.iam.oauth2.domain.model.session.PkceCodeVerifier;
import com.airlock.iam.security.util.crypto.random.AlphanumericRandomStringGenerator;
import com.nimbusds.openid.connect.sdk.Nonce;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;

public final class OAuth2ClientSession implements Mapifiable {
   private static final String STATE_KEY = "state";
   private static final String PROVIDER_ID_KEY = "providerId";
   private static final String NONCE_KEY = "nonce";
   private static final String PKCE_VERIFIER = "pkce";
   private final State state;
   private final ProviderId providerId;
   private final Nonce nonce;
   private Optional<PkceCodeVerifier> pkceVerifier = Optional.empty();

   public OAuth2ClientSession(ProviderId providerId) {
      this.state = State.random();
      this.nonce = new Nonce(AlphanumericRandomStringGenerator.generateAlphanumericRandomString(44));
      this.providerId = providerId;
   }

   public boolean isStateValid(State stateFromRequest) {
      return this.state.isValid(stateFromRequest);
   }

   public String getNonce() {
      return this.nonce.getValue();
   }

   public boolean hasNonce(String otherNonce) {
      return this.nonce.equals(Nonce.parse(otherNonce));
   }

   public void setPkceCodeVerifier(PkceCodeVerifier challenge) {
      this.pkceVerifier = Optional.of(challenge);
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .put("state", this.state.getState())
         .put("providerId", this.providerId.id())
         .put("nonce", this.nonce.getValue())
         .putIfPresent("pkce", this.pkceVerifier.map(PkceCodeVerifier::verifier))
         .build();
   }

   public static OAuth2ClientSession demapify(MapifiedValues values) {
      return new OAuth2ClientSession(
         values.simpleValue("state", State::state),
         values.simpleValue("providerId", ProviderId::new),
         values.simpleValue("nonce", Nonce::parse),
         values.<String>getOptional("pkce").map(PkceCodeVerifier::new)
      );
   }

   @SuppressFBWarnings(justification = "generated code")
   public State getState() {
      return this.state;
   }

   @SuppressFBWarnings(justification = "generated code")
   public ProviderId getProviderId() {
      return this.providerId;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Optional<PkceCodeVerifier> getPkceVerifier() {
      return this.pkceVerifier;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   private OAuth2ClientSession(State state, ProviderId providerId, Nonce nonce, Optional<PkceCodeVerifier> pkceVerifier) {
      this.state = state;
      this.providerId = providerId;
      this.nonce = nonce;
      this.pkceVerifier = pkceVerifier;
   }
}
