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

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.service.mapification.Mapifier;
import com.airlock.iam.flow.api.domain.model.step.StepSession;
import com.airlock.iam.login.api.domain.model.oauth2.OAuth2Credential;
import com.airlock.iam.oauth2.domain.model.client.OAuth2ClientSession;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;

public final class OAuth2SsoStepSession implements StepSession {
   private static final String SESSION_KEY = "session";
   private static final String CREDENTIAL_KEY = "credential";
   private Optional<OAuth2ClientSession> clientSession = Optional.empty();
   private Optional<OAuth2Credential> credential = Optional.empty();

   public void setClientSession(OAuth2ClientSession clientSession) {
      this.clientSession = Optional.of(clientSession);
   }

   public void setCredential(OAuth2Credential credential) {
      this.credential = Optional.of(credential);
   }

   public void clearClientSession() {
      this.clientSession = Optional.empty();
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .putIfPresent("session", this.clientSession.map(OAuth2ClientSession::mapify))
         .putIfPresent("credential", this.credential.map(Mapifier.getInstance()::mapify))
         .build();
   }

   public static OAuth2SsoStepSession demapify(MapifiedValues values) {
      return new OAuth2SsoStepSession(
         values.demapifyOptional("session", OAuth2ClientSession::demapify), values.demapifyOptional("credential", Mapifier.getInstance()::demapify)
      );
   }

   @SuppressFBWarnings(justification = "generated code")
   public Optional<OAuth2ClientSession> getClientSession() {
      return this.clientSession;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Optional<OAuth2Credential> getCredential() {
      return this.credential;
   }

   @SuppressFBWarnings(justification = "generated code")
   public OAuth2SsoStepSession() {
   }

   @SuppressFBWarnings(justification = "generated code")
   private OAuth2SsoStepSession(Optional<OAuth2ClientSession> clientSession, Optional<OAuth2Credential> credential) {
      this.clientSession = clientSession;
      this.credential = credential;
   }

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

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