package com.airlock.iam.admin.application.service.settings;

import com.airlock.iam.common.api.domain.model.valueprovider.ValueProvider;
import com.airlock.iam.common.domain.model.user.profileitems.UserProfileItemDetails;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class UserProfileItemSettings {
   private String type;
   private String name;
   private String resourceKey;
   private boolean optional;
   private String prefill;
   private boolean modifiable;
   private boolean sortable;
   private List<String> options;
   private boolean optionsAreResourceKeys;
   private String resourceKeyPrefix;

   private static UserProfileItemSettings of(UserProfileItemDetails column, Map<String, Object> userContextData) {
      return builder()
         .type(column.getType().name())
         .name(column.getName())
         .resourceKey(column.getResourceKey())
         .optional(column.isOptional())
         .prefill(column.getPrefill().<String>map(ValueProvider::getValue).orElse(null))
         .modifiable(column.isModifiable(userContextData))
         .sortable(column.isSortable())
         .options(column.getOptions())
         .optionsAreResourceKeys(column.isOptionsAreResourceKeys())
         .resourceKeyPrefix(column.getResourceKeyPrefix())
         .build();
   }

   public static List<UserProfileItemSettings> createWithUserData(List<UserProfileItemDetails> columns, Map<String, Object> userContextData) {
      return columns.stream().map(item -> of(item, userContextData)).collect(Collectors.toList());
   }

   public static List<UserProfileItemSettings> createGeneric(List<UserProfileItemDetails> columns) {
      return columns.stream().map(item -> of(item, Map.of())).collect(Collectors.toList());
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public String getType() {
      return this.type;
   }

   @SuppressFBWarnings(justification = "generated code")
   public String getName() {
      return this.name;
   }

   @SuppressFBWarnings(justification = "generated code")
   public String getResourceKey() {
      return this.resourceKey;
   }

   @SuppressFBWarnings(justification = "generated code")
   public boolean isOptional() {
      return this.optional;
   }

   @SuppressFBWarnings(justification = "generated code")
   public String getPrefill() {
      return this.prefill;
   }

   @SuppressFBWarnings(justification = "generated code")
   public boolean isModifiable() {
      return this.modifiable;
   }

   @SuppressFBWarnings(justification = "generated code")
   public boolean isSortable() {
      return this.sortable;
   }

   @SuppressFBWarnings(justification = "generated code")
   public List<String> getOptions() {
      return this.options;
   }

   @SuppressFBWarnings(justification = "generated code")
   public boolean isOptionsAreResourceKeys() {
      return this.optionsAreResourceKeys;
   }

   @SuppressFBWarnings(justification = "generated code")
   public String getResourceKeyPrefix() {
      return this.resourceKeyPrefix;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   private UserProfileItemSettings(
      String type,
      String name,
      String resourceKey,
      boolean optional,
      String prefill,
      boolean modifiable,
      boolean sortable,
      List<String> options,
      boolean optionsAreResourceKeys,
      String resourceKeyPrefix
   ) {
      this.type = type;
      this.name = name;
      this.resourceKey = resourceKey;
      this.optional = optional;
      this.prefill = prefill;
      this.modifiable = modifiable;
      this.sortable = sortable;
      this.options = options;
      this.optionsAreResourceKeys = optionsAreResourceKeys;
      this.resourceKeyPrefix = resourceKeyPrefix;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class UserProfileItemSettingsBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String type;
      @SuppressFBWarnings(justification = "generated code")
      private String name;
      @SuppressFBWarnings(justification = "generated code")
      private String resourceKey;
      @SuppressFBWarnings(justification = "generated code")
      private boolean optional;
      @SuppressFBWarnings(justification = "generated code")
      private String prefill;
      @SuppressFBWarnings(justification = "generated code")
      private boolean modifiable;
      @SuppressFBWarnings(justification = "generated code")
      private boolean sortable;
      @SuppressFBWarnings(justification = "generated code")
      private List<String> options;
      @SuppressFBWarnings(justification = "generated code")
      private boolean optionsAreResourceKeys;
      @SuppressFBWarnings(justification = "generated code")
      private String resourceKeyPrefix;

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

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemSettings.UserProfileItemSettingsBuilder type(String type) {
         this.type = type;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemSettings.UserProfileItemSettingsBuilder name(String name) {
         this.name = name;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemSettings.UserProfileItemSettingsBuilder resourceKey(String resourceKey) {
         this.resourceKey = resourceKey;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemSettings.UserProfileItemSettingsBuilder optional(boolean optional) {
         this.optional = optional;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemSettings.UserProfileItemSettingsBuilder prefill(String prefill) {
         this.prefill = prefill;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemSettings.UserProfileItemSettingsBuilder modifiable(boolean modifiable) {
         this.modifiable = modifiable;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemSettings.UserProfileItemSettingsBuilder sortable(boolean sortable) {
         this.sortable = sortable;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemSettings.UserProfileItemSettingsBuilder options(List<String> options) {
         this.options = options;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemSettings.UserProfileItemSettingsBuilder optionsAreResourceKeys(boolean optionsAreResourceKeys) {
         this.optionsAreResourceKeys = optionsAreResourceKeys;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemSettings.UserProfileItemSettingsBuilder resourceKeyPrefix(String resourceKeyPrefix) {
         this.resourceKeyPrefix = resourceKeyPrefix;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemSettings build() {
         return new UserProfileItemSettings(
            this.type,
            this.name,
            this.resourceKey,
            this.optional,
            this.prefill,
            this.modifiable,
            this.sortable,
            this.options,
            this.optionsAreResourceKeys,
            this.resourceKeyPrefix
         );
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "UserProfileItemSettings.UserProfileItemSettingsBuilder(type="
            + this.type
            + ", name="
            + this.name
            + ", resourceKey="
            + this.resourceKey
            + ", optional="
            + this.optional
            + ", prefill="
            + this.prefill
            + ", modifiable="
            + this.modifiable
            + ", sortable="
            + this.sortable
            + ", options="
            + this.options
            + ", optionsAreResourceKeys="
            + this.optionsAreResourceKeys
            + ", resourceKeyPrefix="
            + this.resourceKeyPrefix
            + ")";
      }
   }
}
