package com.airlock.iam.login.rest.application.configuration;

import com.airlock.iam.flow.ui.application.configuration.LoginPageTargetConfig;
import com.airlock.iam.flow.ui.application.configuration.RedirectTargetConfig;
import com.airlock.iam.flow.ui.application.configuration.UriConfig;
import com.airlock.iam.login.api.application.configuration.location.propagate.IdentityPropagatorURITransformerConfig;
import com.airlock.iam.login.application.configuration.location.extract.QueryParameterURIValueExtractorConfig;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.property.PluginListProperty;
import com.airlock.iam.plugin.api.annotation.property.PluginProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

@Plugin(
   description = "Defines an URI to be redirected to after completing a flow. Note that this plugin only works when used for a logout target of an authentication UI.",
   displayName = "Parameter-based Target URI"
)
public class ParameterBasedTargetUriConfig implements RedirectTargetConfig {
   @PluginProperty(
      description = "The URI to be redirected to (after applying the URI Transformers). Must result in a valid URI that is absolute or relative to the host.",
      displayName = "Default Target URI",
      defaultValue = LoginPageTargetConfig.class,
      optional = true
   )
   private UriConfig defaultTargetUri;
   private static final String TARGET_URI_FIELD = "defaultTargetUri";
   private static final String TARGET_URI_DISPLAY_NAME = "Default Target URI";
   @PluginProperty(
      description = "Uses the value of a query parameter retrieved by the Loginapp UI upon invocation of <tt>/&lt;loginapp-uri&gt;/ui/app/auth/logout</tt> to determine the target URI. Falls back to the Default Target URI if no valid target URI can be inferred.",
      displayName = "Query Parameter URI Extractor",
      optional = false
   )
   private QueryParameterURIValueExtractorConfig queryParameterUriExtractor;
   @PluginListProperty(
      description = "The chain of URI transformers to transform the chosen target URI. The transformers are applied in the configured order. If any transformer produces a 'veto', the untransformed target URI is used.",
      displayName = "URI Transformers",
      optional = true,
      minElements = 1
   )
   private List<IdentityPropagatorURITransformerConfig> uriTransformers;
   private static final String URI_TRANSFORMERS_DISPLAY_NAME = "URI Transformers";

   public UriConfig getDefaultTargetUri() {
      return this.defaultTargetUri;
   }

   public QueryParameterURIValueExtractorConfig getQueryParameterUriExtractor() {
      return this.queryParameterUriExtractor;
   }

   public List<IdentityPropagatorURITransformerConfig> getUriTransformers() {
      return this.uriTransformers;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public ParameterBasedTargetUriConfig(
      UriConfig defaultTargetUri,
      QueryParameterURIValueExtractorConfig queryParameterUriExtractor,
      List<IdentityPropagatorURITransformerConfig> uriTransformers
   ) {
      this.defaultTargetUri = defaultTargetUri;
      this.queryParameterUriExtractor = queryParameterUriExtractor;
      this.uriTransformers = uriTransformers;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public static class ParameterBasedTargetUriConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private UriConfig defaultTargetUri;
      @SuppressFBWarnings(justification = "generated code")
      private QueryParameterURIValueExtractorConfig queryParameterUriExtractor;
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<IdentityPropagatorURITransformerConfig> uriTransformers;

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

      @SuppressFBWarnings(justification = "generated code")
      public ParameterBasedTargetUriConfig.ParameterBasedTargetUriConfigBuilder defaultTargetUri(UriConfig defaultTargetUri) {
         this.defaultTargetUri = defaultTargetUri;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ParameterBasedTargetUriConfig.ParameterBasedTargetUriConfigBuilder queryParameterUriExtractor(
         QueryParameterURIValueExtractorConfig queryParameterUriExtractor
      ) {
         this.queryParameterUriExtractor = queryParameterUriExtractor;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ParameterBasedTargetUriConfig.ParameterBasedTargetUriConfigBuilder uriTransformer(IdentityPropagatorURITransformerConfig uriTransformer) {
         if (this.uriTransformers == null) {
            this.uriTransformers = new ArrayList<>();
         }

         this.uriTransformers.add(uriTransformer);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ParameterBasedTargetUriConfig.ParameterBasedTargetUriConfigBuilder uriTransformers(
         Collection<? extends IdentityPropagatorURITransformerConfig> uriTransformers
      ) {
         if (uriTransformers == null) {
            throw new NullPointerException("uriTransformers cannot be null");
         }

         if (this.uriTransformers == null) {
            this.uriTransformers = new ArrayList<>();
         }

         this.uriTransformers.addAll(uriTransformers);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ParameterBasedTargetUriConfig.ParameterBasedTargetUriConfigBuilder clearUriTransformers() {
         if (this.uriTransformers != null) {
            this.uriTransformers.clear();
         }

         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public ParameterBasedTargetUriConfig build() {
         return new ParameterBasedTargetUriConfig(
            this.defaultTargetUri, this.queryParameterUriExtractor, switch (this.uriTransformers == null ? 0 : this.uriTransformers.size()) {
               case 0 -> Collections.emptyList();
               case 1 -> Collections.singletonList(this.uriTransformers.get(0));
               default -> Collections.unmodifiableList(new ArrayList<>(this.uriTransformers));
            }
         );
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "ParameterBasedTargetUriConfig.ParameterBasedTargetUriConfigBuilder(defaultTargetUri="
            + this.defaultTargetUri
            + ", queryParameterUriExtractor="
            + this.queryParameterUriExtractor
            + ", uriTransformers="
            + this.uriTransformers
            + ")";
      }
   }
}
