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

import com.airlock.iam.common.api.application.configuration.valueprovider.StringValueProviderConfig;
import com.airlock.iam.flow.api.application.configuration.valueprovider.ValueMapProviderConfig;
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.StringProperty;
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 a string value to be provided from a Value Map Provider and a key. If the key is not present, an empty value (null) is returned.\n<p>If the provided value is not a string object, then the value map provider will attempt to convert it to a string.</p>",
   displayName = "String From Map Value Provider"
)
public final class StringFromMapValueProviderConfig implements StringValueProviderConfig {
   @PluginListProperty(
      description = "Value maps from which the string to be provided is selected. If a key occurs in more than one value map, the providers further down in the list take precedence.",
      optional = false
   )
   private List<ValueMapProviderConfig> valueMaps;
   @StringProperty(
      description = "Case-sensitive key to select the string in the value map.",
      exampleValues = {"identifying_attribute", "PASSWORD"},
      optional = false
   )
   private String key;

   public List<ValueMapProviderConfig> getValueMaps() {
      return this.valueMaps;
   }

   public String getKey() {
      return this.key;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public StringFromMapValueProviderConfig(List<ValueMapProviderConfig> valueMaps, String key) {
      this.valueMaps = valueMaps;
      this.key = key;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class StringFromMapValueProviderConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private ArrayList<ValueMapProviderConfig> valueMaps;
      @SuppressFBWarnings(justification = "generated code")
      private String key;

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

      @SuppressFBWarnings(justification = "generated code")
      public StringFromMapValueProviderConfig.StringFromMapValueProviderConfigBuilder valueMap(ValueMapProviderConfig valueMap) {
         if (this.valueMaps == null) {
            this.valueMaps = new ArrayList<>();
         }

         this.valueMaps.add(valueMap);
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public StringFromMapValueProviderConfig.StringFromMapValueProviderConfigBuilder valueMaps(Collection<? extends ValueMapProviderConfig> valueMaps) {
         if (valueMaps == null) {
            throw new NullPointerException("valueMaps cannot be null");
         }

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

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

      @SuppressFBWarnings(justification = "generated code")
      public StringFromMapValueProviderConfig.StringFromMapValueProviderConfigBuilder clearValueMaps() {
         if (this.valueMaps != null) {
            this.valueMaps.clear();
         }

         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public StringFromMapValueProviderConfig.StringFromMapValueProviderConfigBuilder key(String key) {
         this.key = key;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "StringFromMapValueProviderConfig.StringFromMapValueProviderConfigBuilder(valueMaps=" + this.valueMaps + ", key=" + this.key + ")";
      }
   }
}
