package com.airlock.iam.saml2.application.configuration;

import com.airlock.iam.plugin.api.annotation.config.ConfigurationValidator;
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.StringProperty;
import com.airlock.iam.plugin.api.config.ConfigurationContext;
import com.airlock.iam.plugin.api.config.ConfigurationEnvironment;
import com.airlock.iam.plugin.api.config.Environment;
import com.airlock.iam.plugin.api.config.PluginConfiguration;
import com.airlock.iam.plugin.api.exception.ConfigurationException;
import com.airlock.iam.saml2.application.configuration.validation.Saml2MetadataValidator;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.File;

@Plugin(description = "Configures a specific SAML 2.0 Identity Provider (IdP).", displayName = "SAML 2.0 Identity Provider Entity")
@PluginLicenseRestriction(moduleAffiliation = {"SamlIdp", "SamlSp"})
public final class Saml2IDPEntityConfig {
   @StringProperty(
      description = "The entity ID of the Identity Provider. Needs to match the entity ID in the metadata files (see separate \"Metadata File\"  and \"Extended Metadata File\" properties). Must be unique over all entities.",
      optional = false,
      displayName = "IdP Entity ID",
      exampleValues = {"idp", "custom-idp"}
   )
   private String entityId;
   @FileProperty(
      description = "<p>The name of the file containing the standard metadata of this entity.</p><p>Can either be relative to the config root or absolute.</p><p><b>NOTE</b>: When the metadata is changed, the login application needs to be restarted.</p>",
      displayName = "Metadata File",
      isFile = true,
      assureExistence = true,
      assureReadable = true,
      optional = false,
      exampleValues = "instances/test/saml2/idp.xml"
   )
   private File metaDataFile;
   private static final String META_DATA_FILE_PROPERTY = "metaDataFile";
   public static final String META_DATA_FILE_DISPLAY_NAME = "Metadata File";
   @FileProperty(
      description = "<p>The name of the file containing the extended metadata of this entity.</p><p>Can either be relative to the config root or absolute.</p><p><b>NOTE</b>: When the metadata is changed, the login application needs to be restarted.</p>",
      displayName = "Extended Metadata File",
      isFile = true,
      assureExistence = true,
      assureReadable = true,
      optional = false,
      exampleValues = "instances/test/saml2/idp-extended.xml"
   )
   private File extendedMetaDataFile;
   private static final String EXTENDED_META_DATA_FILE_PROPERTY = "extendedMetaDataFile";
   public static final String EXTENDED_META_DATA_FILE_DISPLAY_NAME = "Extended Metadata File";

   @ConfigurationValidator
   static void validateMetadata(PluginConfiguration config, ConfigurationEnvironment env, ConfigurationContext context, Environment validationEnvironment) throws ConfigurationException {
      if (validationEnvironment == Environment.CURRENT_VM) {
         new Saml2MetadataValidator(config, env, context).validateStandardMetadataFile("metaDataFile").validateExtendedMetadataFile("extendedMetaDataFile");
      }
   }

   public String getEntityId() {
      return this.entityId;
   }

   public File getMetaDataFile() {
      return this.metaDataFile;
   }

   public File getExtendedMetaDataFile() {
      return this.extendedMetaDataFile;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public Saml2IDPEntityConfig(String entityId, File metaDataFile, File extendedMetaDataFile) {
      this.entityId = entityId;
      this.metaDataFile = metaDataFile;
      this.extendedMetaDataFile = extendedMetaDataFile;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public static class Saml2IDPEntityConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String entityId;
      @SuppressFBWarnings(justification = "generated code")
      private File metaDataFile;
      @SuppressFBWarnings(justification = "generated code")
      private File extendedMetaDataFile;

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

      @SuppressFBWarnings(justification = "generated code")
      public Saml2IDPEntityConfig.Saml2IDPEntityConfigBuilder entityId(String entityId) {
         this.entityId = entityId;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public Saml2IDPEntityConfig.Saml2IDPEntityConfigBuilder metaDataFile(File metaDataFile) {
         this.metaDataFile = metaDataFile;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public Saml2IDPEntityConfig.Saml2IDPEntityConfigBuilder extendedMetaDataFile(File extendedMetaDataFile) {
         this.extendedMetaDataFile = extendedMetaDataFile;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public Saml2IDPEntityConfig build() {
         return new Saml2IDPEntityConfig(this.entityId, this.metaDataFile, this.extendedMetaDataFile);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "Saml2IDPEntityConfig.Saml2IDPEntityConfigBuilder(entityId="
            + this.entityId
            + ", metaDataFile="
            + this.metaDataFile
            + ", extendedMetaDataFile="
            + this.extendedMetaDataFile
            + ")";
      }
   }
}
