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

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.oauth2.domain.model.remoteconsent.RemoteConsentSessionNonce;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;

public class OAuth2ConsentStepSession implements StepSession {
   private static final String LOCAL_CONSENT_SESSION_DATA_KEY = "localConsentSessionData";
   private static final String REMOTE_CONSENT_SESSION_DATA_KEY = "remoteConsentSessionData";
   private Optional<OAuth2ConsentStepSession.LocalConsentSessionData> localConsentSessionData = Optional.empty();
   private Optional<OAuth2ConsentStepSession.RemoteConsentSessionData> remoteConsentSessionData = Optional.empty();

   public void setLocalConsentSessionData(OAuth2ConsentStepSession.LocalConsentSessionData localConsentSessionData) {
      this.localConsentSessionData = Optional.of(localConsentSessionData);
   }

   public void setRemoteConsentSessionData(OAuth2ConsentStepSession.RemoteConsentSessionData remoteConsentSessionData) {
      this.remoteConsentSessionData = Optional.ofNullable(remoteConsentSessionData);
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .putIfPresent("localConsentSessionData", this.localConsentSessionData.map(lcsd -> lcsd.getLocalConsentWithScopes().mapify()))
         .putIfPresent("remoteConsentSessionData", this.remoteConsentSessionData.map(rcsd -> rcsd.nonce.getNonce()))
         .build();
   }

   public static OAuth2ConsentStepSession demapify(MapifiedValues values) {
      return new OAuth2ConsentStepSession(
         values.transformOptional(
            "localConsentSessionData",
            v -> new OAuth2ConsentStepSession.LocalConsentSessionData(values.demapifyValue("localConsentSessionData", OAuth2LocalConsentAttribute::demapify))
         ),
         values.transformOptional(
            "remoteConsentSessionData", v -> new OAuth2ConsentStepSession.RemoteConsentSessionData(new RemoteConsentSessionNonce((String)v))
         )
      );
   }

   @SuppressFBWarnings(justification = "generated code")
   public Optional<OAuth2ConsentStepSession.LocalConsentSessionData> getLocalConsentSessionData() {
      return this.localConsentSessionData;
   }

   @SuppressFBWarnings(justification = "generated code")
   public Optional<OAuth2ConsentStepSession.RemoteConsentSessionData> getRemoteConsentSessionData() {
      return this.remoteConsentSessionData;
   }

   @SuppressFBWarnings(justification = "generated code")
   public OAuth2ConsentStepSession(
      Optional<OAuth2ConsentStepSession.LocalConsentSessionData> localConsentSessionData,
      Optional<OAuth2ConsentStepSession.RemoteConsentSessionData> remoteConsentSessionData
   ) {
      this.localConsentSessionData = localConsentSessionData;
      this.remoteConsentSessionData = remoteConsentSessionData;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   protected boolean canEqual(Object other) {
      return other instanceof OAuth2ConsentStepSession;
   }

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

   public static final class LocalConsentSessionData {
      private final OAuth2LocalConsentAttribute localConsentWithScopes;

      @SuppressFBWarnings(justification = "generated code")
      public LocalConsentSessionData(OAuth2LocalConsentAttribute localConsentWithScopes) {
         this.localConsentWithScopes = localConsentWithScopes;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2LocalConsentAttribute getLocalConsentWithScopes() {
         return this.localConsentWithScopes;
      }

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

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "OAuth2ConsentStepSession.LocalConsentSessionData(localConsentWithScopes=" + this.getLocalConsentWithScopes() + ")";
      }
   }

   public static final class RemoteConsentSessionData {
      private final RemoteConsentSessionNonce nonce;

      @SuppressFBWarnings(justification = "generated code")
      public RemoteConsentSessionData(RemoteConsentSessionNonce nonce) {
         this.nonce = nonce;
      }

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

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

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "OAuth2ConsentStepSession.RemoteConsentSessionData(nonce=" + this.getNonce() + ")";
      }
   }
}
