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

import com.airlock.iam.base.api.domain.model.exception.FailureException;
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.accountlink.AccountInfo;
import com.airlock.iam.common.domain.model.accountlink.AccountSub;
import com.airlock.iam.common.domain.model.provider.ProviderId;
import com.airlock.iam.flow.api.domain.model.session.SingleFlowScopedFlowAttribute;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public final class OAuth2MissingAccountLinkFlowSessionAttribute implements SingleFlowScopedFlowAttribute<OAuth2MissingAccountLinkFlowSessionAttribute> {
   private static final String PROVIDER_ID_KEY = "providerId";
   private static final String ACCOUNT_SUB_KEY = "isAccLink";
   private static final String ACCOUNT_INFO_KEY = "nonce";
   private final ProviderId providerId;
   private final AccountSub accountSub;
   private final AccountInfo accountInfo;

   public OAuth2MissingAccountLinkFlowSessionAttribute mergeWith(OAuth2MissingAccountLinkFlowSessionAttribute newAttribute) {
      throw new FailureException("Cannot replace missing account link info session attribute");
   }

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .put("providerId", this.providerId.id())
         .put("isAccLink", this.accountSub.sub())
         .put("nonce", this.accountInfo.info())
         .build();
   }

   public static OAuth2MissingAccountLinkFlowSessionAttribute demapify(MapifiedValues values) {
      return new OAuth2MissingAccountLinkFlowSessionAttribute(
         values.simpleValue("providerId", ProviderId::new), values.simpleValue("isAccLink", AccountSub::new), values.simpleValue("nonce", AccountInfo::new)
      );
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public AccountSub getAccountSub() {
      return this.accountSub;
   }

   @SuppressFBWarnings(justification = "generated code")
   public AccountInfo getAccountInfo() {
      return this.accountInfo;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "OAuth2MissingAccountLinkFlowSessionAttribute(providerId="
         + this.getProviderId()
         + ", accountSub="
         + this.getAccountSub()
         + ", accountInfo="
         + this.getAccountInfo()
         + ")";
   }

   @SuppressFBWarnings(justification = "generated code")
   public OAuth2MissingAccountLinkFlowSessionAttribute(ProviderId providerId, AccountSub accountSub, AccountInfo accountInfo) {
      this.providerId = providerId;
      this.accountSub = accountSub;
      this.accountInfo = accountInfo;
   }
}
