package com.airlock.iam.admin.application.configuration.airlock2fa;

import com.airlock.iam.core.misc.util.report.ReportRenderer;
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.FileProperty;
import com.airlock.iam.plugin.api.annotation.property.PluginProperty;
import com.airlock.iam.plugin.api.annotation.property.StringProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.File;
import java.util.Optional;

@Plugin(displayName = "Print Airlock 2FA Activation Letters", description = "Settings for printing a user's device activation letter to a file.")
@PluginLicenseRestriction(moduleAffiliation = "Airlock2FA")
public final class Airlock2FAActivationLetterPrintingConfig {
   @PluginProperty(
      description = "Defines how activation letters (e.g. PDFs) are rendered.<p>The following placeholders can be used in the templates<ul><li><code>${<i>User Context Data Name</i>}</code> - context data of the user.</li><li><code>${activationQRCode}</code> - QR code image for the activation. Image size in document can be adjusted: <code>${activationQRCode,imageSize,<i>width in points</i>,<i>height in points</i>}</code></li><li><code>${expires}</code> - expiring date of the activation. Can be used with extended format (e.g. <code>${expires,date,short}</code>)</li><li><code>${activationCodeShort}</code> - Short activation code as an additional option for the activation.</li></ul></p>",
      optional = false
   )
   private ReportRenderer renderer;
   @FileProperty(
      description = "A writable directory used to store a partially rendered activation letter.<br/>If this property is defined, activation letters are not directly generated into the output directory (see other property) but they are generated into this working directory and are then moved into the output directory once they are done.<br/>This helps to solve problems with processes that automatically read the rendered activation letters and therefore might not see the fully rendered result. Make sure that the working directory and the output directory reside in the same file system (otherwise the moving of the generated file will not be atomic).<br/>The directory is either absolute or relative to the JVMs current directory.",
      optional = true,
      isDirectory = true,
      assureWritable = true,
      assureExistence = true,
      exampleValues = {"tmp", "under-construction", "working"}
   )
   private File workingDirectory;
   @FileProperty(
      description = "The directory where the printable letters will be stored.",
      optional = false,
      exampleValues = {"/var/airlock/letters", "./pdfs"},
      assureExistence = true
   )
   private File outputDirectory;
   @StringProperty(
      displayName = "Language Context Data Name",
      description = "The user's context data attribute containing its language. The language is used to choose the template in the renderer. If left empty, the default template will be used.",
      optional = true,
      suggestedValues = "language"
   )
   private String languageContextDataName;

   Airlock2FAActivationLetterPrintingConfig(
      ReportRenderer renderer, File workingDirectory, File outputDirectory, String languageContextDataName, int enrollmentValidityInSeconds
   ) {
      this.renderer = renderer;
      this.workingDirectory = workingDirectory;
      this.outputDirectory = outputDirectory;
      this.languageContextDataName = languageContextDataName;
   }

   public ReportRenderer getRenderer() {
      return this.renderer;
   }

   public Optional<File> getWorkingDirectory() {
      return Optional.ofNullable(this.workingDirectory);
   }

   public File getOutputDirectory() {
      return this.outputDirectory;
   }

   public Optional<String> getLanguageContextDataName() {
      return Optional.ofNullable(this.languageContextDataName);
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public static class Airlock2FAActivationLetterPrintingConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private ReportRenderer renderer;
      @SuppressFBWarnings(justification = "generated code")
      private File workingDirectory;
      @SuppressFBWarnings(justification = "generated code")
      private File outputDirectory;
      @SuppressFBWarnings(justification = "generated code")
      private String languageContextDataName;
      @SuppressFBWarnings(justification = "generated code")
      private int enrollmentValidityInSeconds;

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

      @SuppressFBWarnings(justification = "generated code")
      public Airlock2FAActivationLetterPrintingConfig.Airlock2FAActivationLetterPrintingConfigBuilder renderer(ReportRenderer renderer) {
         this.renderer = renderer;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public Airlock2FAActivationLetterPrintingConfig.Airlock2FAActivationLetterPrintingConfigBuilder workingDirectory(File workingDirectory) {
         this.workingDirectory = workingDirectory;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public Airlock2FAActivationLetterPrintingConfig.Airlock2FAActivationLetterPrintingConfigBuilder outputDirectory(File outputDirectory) {
         this.outputDirectory = outputDirectory;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public Airlock2FAActivationLetterPrintingConfig.Airlock2FAActivationLetterPrintingConfigBuilder languageContextDataName(String languageContextDataName) {
         this.languageContextDataName = languageContextDataName;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public Airlock2FAActivationLetterPrintingConfig.Airlock2FAActivationLetterPrintingConfigBuilder enrollmentValidityInSeconds(
         int enrollmentValidityInSeconds
      ) {
         this.enrollmentValidityInSeconds = enrollmentValidityInSeconds;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public Airlock2FAActivationLetterPrintingConfig build() {
         return new Airlock2FAActivationLetterPrintingConfig(
            this.renderer, this.workingDirectory, this.outputDirectory, this.languageContextDataName, this.enrollmentValidityInSeconds
         );
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "Airlock2FAActivationLetterPrintingConfig.Airlock2FAActivationLetterPrintingConfigBuilder(renderer="
            + this.renderer
            + ", workingDirectory="
            + this.workingDirectory
            + ", outputDirectory="
            + this.outputDirectory
            + ", languageContextDataName="
            + this.languageContextDataName
            + ", enrollmentValidityInSeconds="
            + this.enrollmentValidityInSeconds
            + ")";
      }
   }
}
