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

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.BooleanProperty;
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 selects the subflow depending on whether a Cronto activation is required. A Cronto device activation is required if the user has no (active) Cronto device and either has a valid activation letter with remaining activations or if the user is allowed to activate cronto without activation letter (typically in migration use cases).",
   displayName = "Cronto Activation Required"
)
@PluginLicenseRestriction(moduleAffiliation = "Cronto")
public class CrontoActivationRequiredConditionConfig implements FlowConditionConfig {
   @PluginProperty(
      description = "The Cronto Handler to manage the user's Cronto devices and activation letters.",
      globalPluginReference = "globalCrontoSettings",
      optional = false
   )
   private CrontoHandler crontoHandler;
   @BooleanProperty(
      description = "Flag to determine, whether inactive devices should be included when deciding whether the user has a Cronto device.",
      defaultValue = "false",
      optional = true
   )
   private boolean includeInactiveDevices;
   @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;
   }

   public boolean isIncludeInactiveDevices() {
      return this.includeInactiveDevices;
   }

   public TagConfig getStrongAuthenticationTag() {
      return this.strongAuthenticationTag;
   }

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

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

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

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

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

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

      @SuppressFBWarnings(justification = "generated code")
      public CrontoActivationRequiredConditionConfig.CrontoActivationRequiredConditionConfigBuilder includeInactiveDevices(boolean includeInactiveDevices) {
         this.includeInactiveDevices = includeInactiveDevices;
         return this;
      }

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

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

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