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.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.IntegerProperty;
import com.airlock.iam.plugin.api.annotation.property.PluginProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.time.Duration;

@Plugin(description = "A condition to decide whether a user can order a (new) Cronto activation letter.")
@PluginLicenseRestriction(moduleAffiliation = "Cronto")
public final class CrontoLetterOrderConditionConfig implements FlowConditionConfig {
   @PluginProperty(description = "Plugin to handle all Cronto-specific actions.", globalPluginReference = "globalCrontoSettings", optional = false)
   private CrontoHandler crontoHandler;
   @BooleanProperty(
      description = "If enabled, only users that already have a Cronto account are allowed to order an activation letter.",
      optional = true,
      defaultValue = "true"
   )
   private boolean crontoAccountRequired;
   @BooleanProperty(
      description = "If enabled, only users that already have a Cronto activation letter are allowed to order a new letter.",
      optional = true,
      defaultValue = "false"
   )
   private boolean crontoLetterRequired;
   @IntegerProperty(
      description = "<p>Number of hours that must at least have passed since the last Cronto activation letter has been ordered.</p><p>By setting this value to 0, no waiting time until an additional letter can be ordered is required. However, it is recommended to set a different value to prevent a letter being ordered while another one is still being printed or being delivered.</p>",
      displayName = "Minimum Letter Order Interval [h]",
      optional = true,
      minValue = 0,
      defaultValue = 24,
      exampleValues = {12, 24, 48}
   )
   private int minimalLetterOrderInterval;

   public Duration getMinimalLetterOrderInterval() {
      return Duration.ofHours(this.minimalLetterOrderInterval);
   }

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

   public boolean isCrontoAccountRequired() {
      return this.crontoAccountRequired;
   }

   public boolean isCrontoLetterRequired() {
      return this.crontoLetterRequired;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public CrontoLetterOrderConditionConfig(
      CrontoHandler crontoHandler, boolean crontoAccountRequired, boolean crontoLetterRequired, int minimalLetterOrderInterval
   ) {
      this.crontoHandler = crontoHandler;
      this.crontoAccountRequired = crontoAccountRequired;
      this.crontoLetterRequired = crontoLetterRequired;
      this.minimalLetterOrderInterval = minimalLetterOrderInterval;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class CrontoLetterOrderConditionConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private CrontoHandler crontoHandler;
      @SuppressFBWarnings(justification = "generated code")
      private boolean crontoAccountRequired;
      @SuppressFBWarnings(justification = "generated code")
      private boolean crontoLetterRequired;
      @SuppressFBWarnings(justification = "generated code")
      private int minimalLetterOrderInterval;

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

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

      @SuppressFBWarnings(justification = "generated code")
      public CrontoLetterOrderConditionConfig.CrontoLetterOrderConditionConfigBuilder crontoAccountRequired(boolean crontoAccountRequired) {
         this.crontoAccountRequired = crontoAccountRequired;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoLetterOrderConditionConfig.CrontoLetterOrderConditionConfigBuilder crontoLetterRequired(boolean crontoLetterRequired) {
         this.crontoLetterRequired = crontoLetterRequired;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoLetterOrderConditionConfig.CrontoLetterOrderConditionConfigBuilder minimalLetterOrderInterval(int minimalLetterOrderInterval) {
         this.minimalLetterOrderInterval = minimalLetterOrderInterval;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CrontoLetterOrderConditionConfig build() {
         return new CrontoLetterOrderConditionConfig(this.crontoHandler, this.crontoAccountRequired, this.crontoLetterRequired, this.minimalLetterOrderInterval);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "CrontoLetterOrderConditionConfig.CrontoLetterOrderConditionConfigBuilder(crontoHandler="
            + this.crontoHandler
            + ", crontoAccountRequired="
            + this.crontoAccountRequired
            + ", crontoLetterRequired="
            + this.crontoLetterRequired
            + ", minimalLetterOrderInterval="
            + this.minimalLetterOrderInterval
            + ")";
      }
   }
}
