package com.airlock.iam.oauth2.application.configuration.clientauthentication;

import com.airlock.iam.core.domain.model.http.HttpRequestHeader;
import com.airlock.iam.core.domain.model.http.HttpRequestParameter;
import com.airlock.iam.oauth2.api.domain.model.clientauthentication.ClientAuthenticationMethod;
import com.airlock.iam.oauth2.domain.model.client.OAuth2ClientSideCredentials;
import com.airlock.iam.oauth2.domain.model.client.communication.OAuth2TokenRequestClientSecretProcessor;
import com.airlock.iam.oauth2.domain.model.clientauthentication.method.KnownClientAuthenticationMethod;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.license.PluginLicenseRestriction;
import com.airlock.iam.plugin.api.annotation.property.StringProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.net.URI;
import java.util.List;

@Plugin(
   description = "Specifies the format of the client secret passed as a parameter of OAuth 2.0 requests.",
   displayName = "OAuth 2.0 Parameter Client Secret"
)
@PluginLicenseRestriction(moduleAffiliation = "OAuthClient")
public class OAuth2ParameterClientSecretConfig implements OAuth2TokenRequestClientSecretProcessor {
   @StringProperty(description = "Parameter name of the request parameter containing the Client ID.", optional = true, defaultValue = "client_id")
   private String clientIdName;
   @StringProperty(description = "Parameter name of the request parameter containing the client secret.", optional = true, defaultValue = "client_secret")
   private String parameterName;

   @Override
   public void putClientSecretInRequestData(
      URI baseUri, List<HttpRequestParameter> params, List<HttpRequestHeader> headers, OAuth2ClientSideCredentials clientCredentials
   ) {
      params.add(
         HttpRequestParameter.httpRequestParameter(
            this.parameterName,
            clientCredentials.clientSecret()
               .orElseThrow(() -> new IllegalArgumentException("Cannot use OAuth2Client with absent client secret in this context."))
         )
      );
   }

   @Override
   public ClientAuthenticationMethod clientAuthenticationMethod() {
      return KnownClientAuthenticationMethod.POST_PARAMETERS_CLIENT_SECRET;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static OAuth2ParameterClientSecretConfig.OAuth2ParameterClientSecretConfigBuilder builder() {
      return new OAuth2ParameterClientSecretConfig.OAuth2ParameterClientSecretConfigBuilder();
   }

   @SuppressFBWarnings(justification = "generated code")
   public OAuth2ParameterClientSecretConfig(String clientIdName, String parameterName) {
      this.clientIdName = clientIdName;
      this.parameterName = parameterName;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public static class OAuth2ParameterClientSecretConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String clientIdName;
      @SuppressFBWarnings(justification = "generated code")
      private String parameterName;

      @SuppressFBWarnings(justification = "generated code")
      OAuth2ParameterClientSecretConfigBuilder() {
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ParameterClientSecretConfig.OAuth2ParameterClientSecretConfigBuilder clientIdName(String clientIdName) {
         this.clientIdName = clientIdName;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ParameterClientSecretConfig.OAuth2ParameterClientSecretConfigBuilder parameterName(String parameterName) {
         this.parameterName = parameterName;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public OAuth2ParameterClientSecretConfig build() {
         return new OAuth2ParameterClientSecretConfig(this.clientIdName, this.parameterName);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "OAuth2ParameterClientSecretConfig.OAuth2ParameterClientSecretConfigBuilder(clientIdName="
            + this.clientIdName
            + ", parameterName="
            + this.parameterName
            + ")";
      }
   }
}
