package com.airlock.iam.login.rest.application.configuration;

import com.airlock.iam.core.application.configuration.store.user.DatabaseUserStoreProvider;
import com.airlock.iam.core.misc.impl.persistency.ldap.LdapConnector;
import com.airlock.iam.plugin.api.config.ConfigurationContext;
import com.airlock.iam.plugin.api.config.ConfigurationEnvironment;
import com.airlock.iam.plugin.api.config.PluginConfiguration;
import com.airlock.iam.plugin.api.exception.ConfigurationException;
import com.airlock.iam.plugin.api.exception.PropertyConstraintViolationException;
import com.airlock.iam.plugin.api.valuereader.PluginPropertyValueReader;
import com.airlock.iam.plugin.api.valuereader.StringPropertyValueReader;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public final class LoginappRestFailedTokenCountsValidator {
   private static final String FAILED_TOKEN_COUNT_ERROR_MESSAGE = "The authentication REST API requires that the failed token counts can be persisted.";
   private final PluginConfiguration storeConfig;
   private final ConfigurationEnvironment env;
   private final ConfigurationContext context;

   public void validateFailedTokenCountsConfigured() throws ConfigurationException {
      if (LdapConnector.class.getCanonicalName().equals(this.storeConfig.getPluginClassName())) {
         String attributeConfig = StringPropertyValueReader.effectiveStringConfigValue(this.storeConfig, "failedTokenCountsAttribute", this.env, this.context);
         if (attributeConfig == null) {
            throw new PropertyConstraintViolationException(
               "failedTokenCountsAttribute",
               this.storeConfig,
               this.env,
               this.context,
               "The authentication REST API requires that the failed token counts can be persisted."
            );
         }
      } else if (DatabaseUserStoreProvider.class.getCanonicalName().equals(this.storeConfig.getPluginClassName())) {
         PluginConfiguration persisterConfig = PluginPropertyValueReader.effectivePluginPropertyValue(
            this.storeConfig, "databaseUserPersister", this.env, this.context
         );
         if (persisterConfig == null) {
            return;
         }

         String columnConfig = StringPropertyValueReader.effectiveStringConfigValue(persisterConfig, "colFailedTokenCounts", this.env, this.context);
         if (columnConfig == null) {
            throw new PropertyConstraintViolationException(
               "colFailedTokenCounts",
               persisterConfig,
               this.env,
               this.context,
               "The authentication REST API requires that the failed token counts can be persisted."
            );
         }
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   public LoginappRestFailedTokenCountsValidator(PluginConfiguration storeConfig, ConfigurationEnvironment env, ConfigurationContext context) {
      this.storeConfig = storeConfig;
      this.env = env;
      this.context = context;
   }
}
