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.MapificationUtils;
import com.airlock.iam.base.domain.model.mapification.MapifiedValuesBuilder;
import com.airlock.iam.flow.api.domain.model.step.StepResultData;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.List;
import java.util.Set;
import lombok.NonNull;

public final class OAuth2LocalConsentAttribute implements StepResultData {
   private static final String CLIENT_NAME_KEY = "clientName";
   private static final String GRANTABLE_SCOPES_KEY = "grantableScopes";
   private static final String ALWAYS_GRANTED_SCOPES_KEY = "alwaysGrantedScopes";
   @NonNull
   private final String clientName;
   @NonNull
   private final List<OAuth2ScopeAttribute> grantableScopes;
   @NonNull
   private final Set<OAuth2ScopeAttribute> alwaysGrantedScopes;

   @Override
   public MapifiedValues mapify() {
      return MapifiedValuesBuilder.mapifiedValues()
         .put("clientName", this.clientName)
         .putIfNotEmpty("grantableScopes", MapificationUtils.mapifyCollection(this.grantableScopes, OAuth2ScopeAttributeMapificationUtil::mapify))
         .putIfNotEmpty("alwaysGrantedScopes", MapificationUtils.mapifyCollection(this.alwaysGrantedScopes, OAuth2ScopeAttributeMapificationUtil::mapify))
         .build();
   }

   public static OAuth2LocalConsentAttribute demapify(MapifiedValues values) {
      return new OAuth2LocalConsentAttribute(
         values.get("clientName"),
         values.demapifyList("grantableScopes", OAuth2ScopeAttributeMapificationUtil::demapify),
         values.demapifySet("alwaysGrantedScopes", OAuth2ScopeAttributeMapificationUtil::demapify)
      );
   }

   @SuppressFBWarnings(justification = "generated code")
   public OAuth2LocalConsentAttribute(
      @NonNull String clientName, @NonNull List<OAuth2ScopeAttribute> grantableScopes, @NonNull Set<OAuth2ScopeAttribute> alwaysGrantedScopes
   ) {
      if (clientName == null) {
         throw new NullPointerException("clientName is marked non-null but is null");
      }

      if (grantableScopes == null) {
         throw new NullPointerException("grantableScopes is marked non-null but is null");
      }

      if (alwaysGrantedScopes == null) {
         throw new NullPointerException("alwaysGrantedScopes is marked non-null but is null");
      }

      this.clientName = clientName;
      this.grantableScopes = grantableScopes;
      this.alwaysGrantedScopes = alwaysGrantedScopes;
   }

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public String getClientName() {
      return this.clientName;
   }

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public List<OAuth2ScopeAttribute> getGrantableScopes() {
      return this.grantableScopes;
   }

   @NonNull
   @SuppressFBWarnings(justification = "generated code")
   public Set<OAuth2ScopeAttribute> getAlwaysGrantedScopes() {
      return this.alwaysGrantedScopes;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public String toString() {
      return "OAuth2LocalConsentAttribute(clientName="
         + this.getClientName()
         + ", grantableScopes="
         + this.getGrantableScopes()
         + ", alwaysGrantedScopes="
         + this.getAlwaysGrantedScopes()
         + ")";
   }
}
