package com.airlock.iam.admin.application.configuration.certificate;

import com.airlock.iam.admin.api.domain.model.user.AuthenticationMethods;
import com.airlock.iam.admin.domain.model.token.TokenController;
import com.airlock.iam.core.misc.persistency.token.TokenDataProvider;
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.BooleanProperty;
import com.airlock.iam.plugin.api.annotation.property.PluginProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

@Plugin(
   description = "Token controller to manage X.509 certificates based on the token model. Currently supported operations are adding and removing certificates."
)
@PluginLicenseRestriction(moduleAffiliation = "ClientCertificate")
public class CertificateTokenController implements TokenController {
   @TokenController.TokenControllerId
   public static final String ID = "CERTIFICATE";
   @PluginProperty(
      description = "Token data provider for creating, loading, updating and deleting certificates.",
      optional = false,
      globalPluginReference = "globalTokenDataProvider"
   )
   private TokenDataProvider tokenDataProvider;
   @BooleanProperty(description = "Whether or not certificates can be chosen as active authentication method.", optional = true, defaultValue = "true")
   private boolean allowedAsActive;

   @Override
   public AuthenticationMethods.AuthenticationMethodId getConfiguredId() {
      return AuthenticationMethods.AuthenticationMethodId.authMethodId("CERTIFICATE");
   }

   @Override
   public String getTokenControllerId() {
      return "CERTIFICATE";
   }

   public TokenDataProvider getTokenDataProvider() {
      return this.tokenDataProvider;
   }

   public boolean isAllowedAsActive() {
      return this.allowedAsActive;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public CertificateTokenController(TokenDataProvider tokenDataProvider, boolean allowedAsActive) {
      this.tokenDataProvider = tokenDataProvider;
      this.allowedAsActive = allowedAsActive;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public static class CertificateTokenControllerBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private TokenDataProvider tokenDataProvider;
      @SuppressFBWarnings(justification = "generated code")
      private boolean allowedAsActive;

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

      @SuppressFBWarnings(justification = "generated code")
      public CertificateTokenController.CertificateTokenControllerBuilder tokenDataProvider(TokenDataProvider tokenDataProvider) {
         this.tokenDataProvider = tokenDataProvider;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CertificateTokenController.CertificateTokenControllerBuilder allowedAsActive(boolean allowedAsActive) {
         this.allowedAsActive = allowedAsActive;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CertificateTokenController build() {
         return new CertificateTokenController(this.tokenDataProvider, this.allowedAsActive);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "CertificateTokenController.CertificateTokenControllerBuilder(tokenDataProvider="
            + this.tokenDataProvider
            + ", allowedAsActive="
            + this.allowedAsActive
            + ")";
      }
   }
}
