package com.airlock.iam.flow.shared.application.configuration.cronto;

import com.airlock.iam.core.misc.cronto.CrontoHandler;
import com.airlock.iam.flow.api.application.configuration.selection.FlowConditionConfig;
import com.airlock.iam.flow.api.application.configuration.tag.TagConfig;
import com.airlock.iam.flow.api.domain.model.tag.TagName;
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.PluginProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Optional;

@Plugin(
   description = "Flow selection condition that determines whether the user\ncan activate a Cronto device. A Cronto device activation is possible if the user either has\na valid activation letter with remaining activations or, if configured, the user is allowed\nto activate Cronto without a letter (e.g. in migration use cases).",
   displayName = "Cronto Activation Possible"
)
@PluginLicenseRestriction(moduleAffiliation = "Cronto")
public class CrontoActivationPossibleConditionConfig implements FlowConditionConfig {
   @PluginProperty(
      description = "The Cronto Handler to load the user's Cronto activation letters.",
      globalPluginReference = "globalCrontoSettings",
      optional = false
   )
   private CrontoHandler crontoHandler;
   @PluginProperty(
      description = "This tag indicates strong authentication (typically two factors) and thus considers Cronto activation without a letter as a possible Cronto activation method. If the tag is not configured, Cronto activation without a letter is not considered as a possible Cronto activation method.",
      optional = true
   )
   private TagConfig strongAuthenticationTag;

   public Optional<TagName> getStrongAuthTagName() {
      return Optional.ofNullable(this.strongAuthenticationTag).map(TagConfig::name);
   }

   public CrontoHandler getCrontoHandler() {
      return this.crontoHandler;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public CrontoActivationPossibleConditionConfig(CrontoHandler crontoHandler, TagConfig strongAuthenticationTag) {
      this.crontoHandler = crontoHandler;
      this.strongAuthenticationTag = strongAuthenticationTag;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class CrontoActivationPossibleConditionConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private CrontoHandler crontoHandler;
      @SuppressFBWarnings(justification = "generated code")
      private TagConfig strongAuthenticationTag;

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

      @SuppressFBWarnings(justification = "generated code")
      public CrontoActivationPossibleConditionConfig.CrontoActivationPossibleConditionConfigBuilder crontoHandler(CrontoHandler crontoHandler) {
         this.crontoHandler = crontoHandler;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoActivationPossibleConditionConfig.CrontoActivationPossibleConditionConfigBuilder strongAuthenticationTag(TagConfig strongAuthenticationTag) {
         this.strongAuthenticationTag = strongAuthenticationTag;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoActivationPossibleConditionConfig build() {
         return new CrontoActivationPossibleConditionConfig(this.crontoHandler, this.strongAuthenticationTag);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "CrontoActivationPossibleConditionConfig.CrontoActivationPossibleConditionConfigBuilder(crontoHandler="
            + this.crontoHandler
            + ", strongAuthenticationTag="
            + this.strongAuthenticationTag
            + ")";
      }
   }
}
