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

import com.airlock.iam.common.api.application.configuration.valueprovider.IntegerValueProviderConfig;
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 an integer 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 a string, the provider will attempt to convert the value to integer.</p>\n<p>If the value is an incompatible type, or the string cannot be converted, an error is thrown.</p>",
   displayName = "Integer From Map Value Provider"
)
public final class IntegerFromMapValueProviderConfig implements IntegerValueProviderConfig {
   @PluginListProperty(
      description = "Value maps from which the integer 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 integer in the value map.",
      exampleValues = {"failed_logins", "total_logins"},
      optional = false
   )
   private String key;

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

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

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

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

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

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

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

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

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

      @SuppressFBWarnings(justification = "generated code")
      public IntegerFromMapValueProviderConfig.IntegerFromMapValueProviderConfigBuilder 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 IntegerFromMapValueProviderConfig.IntegerFromMapValueProviderConfigBuilder clearValueMaps() {
         if (this.valueMaps != null) {
            this.valueMaps.clear();
         }

         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      public IntegerFromMapValueProviderConfig build() {
         return new IntegerFromMapValueProviderConfig(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 "IntegerFromMapValueProviderConfig.IntegerFromMapValueProviderConfigBuilder(valueMaps=" + this.valueMaps + ", key=" + this.key + ")";
      }
   }
}
