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

import com.airlock.iam.common.application.configuration.mtan.BasicMtanSettings;
import com.airlock.iam.core.misc.authen.SecretStringGenerator;
import com.airlock.iam.core.misc.impl.authen.mtan.MtanSettings;
import com.airlock.iam.plugin.api.annotation.config.DeprecatedPlugin;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.lifecycle.AfterConfigActivation;
import com.airlock.iam.plugin.api.annotation.property.PluginProperty;
import com.airlock.iam.plugin.framework.application.service.builder.plugin.PluginPropertyBuilderInitializer;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;

@Plugin(
   description = "The settings for managing mTAN tokens based on the existing mTAN Settings.",
   displayName = "mTAN Self-Service Settings (based on mTAN Settings, Legacy)"
)
@DeprecatedPlugin(
   description = "The session-less mTAN self-service endpoints are deprecated and will be removed in a future IAM version. Use protected self-service flows instead."
)
public class MtanSelfServiceConfig implements MtanSelfManagementConfig {
   @PluginProperty(
      description = "Defines all settings for mTAN.",
      displayName = "mTAN Settings",
      globalPluginReference = "globalMtanSettings",
      optional = false
   )
   private MtanSettings mtanSettings;
   private BasicMtanSettings initializedBasicMtanSettings;

   public MtanSelfServiceConfig(MtanSettings mtanSettings) {
      this.mtanSettings = mtanSettings;
   }

   @AfterConfigActivation
   void initializeBasicSettings() {
      this.initializedBasicMtanSettings = ((BasicMtanSettings.BasicMtanSettingsBuilder)PluginPropertyBuilderInitializer.pluginBuilder(
            BasicMtanSettings.BasicMtanSettingsBuilder.class
         ))
         .mtanHandler(this.mtanSettings.getMtanHandler())
         .smsGateway(this.mtanSettings.getSmsGateway())
         .originator(this.mtanSettings.getOriginatorName())
         .defaultCountryCode(this.mtanSettings.getDefaultCountryCode().getCode())
         .useFlashMessages(this.mtanSettings.isUseFlashMessages())
         .visiblePhoneNumberDigits(this.mtanSettings.getVisiblePhoneNumberDigits())
         .build();
   }

   @Override
   public BasicMtanSettings getMtanSettings() {
      return this.initializedBasicMtanSettings;
   }

   @Override
   public Optional<PhoneNumberChangeSettings> getPhoneNumberChangeSettings() {
      return !this.mtanSettings.isAllowPhoneNumberChange()
         ? Optional.empty()
         : Optional.of(
            new PhoneNumberChangeSettings(this.mtanSettings.getOtpValidity(), Optional.ofNullable(this.mtanSettings.getPhoneNumberValidationRegex()))
         );
   }

   @Override
   public String getMessageTemplateKey() {
      return this.mtanSettings.getMessageTemplateKey();
   }

   @Override
   public SecretStringGenerator getTokenGenerator() {
      return this.mtanSettings.getOtpGenerator();
   }

   @Override
   public boolean isOtpCaseSensitive() {
      return !this.mtanSettings.isIgnoreTokenCase();
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public static class MtanSelfServiceConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private MtanSettings mtanSettings;

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

      @SuppressFBWarnings(justification = "generated code")
      public MtanSelfServiceConfig.MtanSelfServiceConfigBuilder mtanSettings(MtanSettings mtanSettings) {
         this.mtanSettings = mtanSettings;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public MtanSelfServiceConfig build() {
         return new MtanSelfServiceConfig(this.mtanSettings);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "MtanSelfServiceConfig.MtanSelfServiceConfigBuilder(mtanSettings=" + this.mtanSettings + ")";
      }
   }
}
