package com.airlock.iam.common.domain.model.user.profileitems;

import com.airlock.iam.base.api.domain.model.validation.ValidationResult;
import com.airlock.iam.common.api.domain.model.valueprovider.ValueProvider;
import com.airlock.iam.common.application.configuration.userprofile.UserProfileItemConfig;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class UserProfileItemDetails {
   private final UserProfileItemConfig.ProfileItemType type;
   private final String name;
   private final String resourceKey;
   private final Optional<ValueProvider<String>> prefill;
   private final boolean optional;
   private boolean editable;
   private final boolean sortable;
   private final List<String> options;
   private final boolean optionsAreResourceKeys;
   private final String resourceKeyPrefix;
   private final UserProfileItemLogic logic;

   public boolean isModifiable(Map<String, Object> contextData) {
      return this.editable && this.logic.isModifiable(contextData.get(this.name));
   }

   public ValidationResult validate(Object newValue, Object oldValue, String userId) {
      UserProfileItemLogic.CheckResult res = this.logic.validate(newValue, oldValue, userId);
      return res == UserProfileItemLogic.CheckResult.IS_OK
         ? ValidationResult.valid()
         : ValidationResult.singleError(this.name, res.convertToValidationResultType());
   }

   public Object transformToPersistency(Object value) {
      return this.logic.formatForPersistence(value);
   }

   public Object transformFromPersistency(Object value) {
      return this.logic.transformPersistentValue(value);
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public UserProfileItemConfig.ProfileItemType 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 Optional<ValueProvider<String>> getPrefill() {
      return this.prefill;
   }

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

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

   @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 UserProfileItemLogic getLogic() {
      return this.logic;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public boolean equals(Object o) {
      if (o == this) {
         return true;
      } else if (!(o instanceof UserProfileItemDetails other)) {
         return false;
      } else {
         if (!other.canEqual(this)) {
            return false;
         }

         Object this$name = this.name;
         Object other$name = other.name;
         return this$name == null ? other$name == null : this$name.equals(other$name);
      }
   }

   @SuppressFBWarnings(justification = "generated code")
   protected boolean canEqual(Object other) {
      return other instanceof UserProfileItemDetails;
   }

   @SuppressFBWarnings(justification = "generated code")
   @Override
   public int hashCode() {
      int PRIME = 59;
      int result = 1;
      Object $name = this.name;
      return result * 59 + ($name == null ? 43 : $name.hashCode());
   }

   @SuppressFBWarnings(justification = "generated code")
   private UserProfileItemDetails(
      UserProfileItemConfig.ProfileItemType type,
      String name,
      String resourceKey,
      Optional<ValueProvider<String>> prefill,
      boolean optional,
      boolean editable,
      boolean sortable,
      List<String> options,
      boolean optionsAreResourceKeys,
      String resourceKeyPrefix,
      UserProfileItemLogic logic
   ) {
      this.type = type;
      this.name = name;
      this.resourceKey = resourceKey;
      this.prefill = prefill;
      this.optional = optional;
      this.editable = editable;
      this.sortable = sortable;
      this.options = options;
      this.optionsAreResourceKeys = optionsAreResourceKeys;
      this.resourceKeyPrefix = resourceKeyPrefix;
      this.logic = logic;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class UserProfileItemDetailsBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private UserProfileItemConfig.ProfileItemType type;
      @SuppressFBWarnings(justification = "generated code")
      private String name;
      @SuppressFBWarnings(justification = "generated code")
      private String resourceKey;
      @SuppressFBWarnings(justification = "generated code")
      private Optional<ValueProvider<String>> prefill;
      @SuppressFBWarnings(justification = "generated code")
      private boolean optional;
      @SuppressFBWarnings(justification = "generated code")
      private boolean editable;
      @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")
      private UserProfileItemLogic logic;

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

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemDetails.UserProfileItemDetailsBuilder type(UserProfileItemConfig.ProfileItemType type) {
         this.type = type;
         return this;
      }

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

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

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemDetails.UserProfileItemDetailsBuilder prefill(Optional<ValueProvider<String>> prefill) {
         this.prefill = prefill;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemDetails.UserProfileItemDetailsBuilder editable(boolean editable) {
         this.editable = editable;
         return this;
      }

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

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

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

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

      @SuppressFBWarnings(justification = "generated code")
      public UserProfileItemDetails.UserProfileItemDetailsBuilder logic(UserProfileItemLogic logic) {
         this.logic = logic;
         return this;
      }

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

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