package com.airlock.iam.common.application.configuration.userprofile;

import com.airlock.iam.common.domain.model.user.profileitems.UserProfileItemLogic;
import com.airlock.iam.core.misc.persistency.UserIterator;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;

@Plugin(description = "A user profile item that displays the username. The item is always read-only.")
public class UsernameUserProfileItemConfig extends UserProfileItemConfigBase {
   public static final String USERNAME_KEY = "MedusaUsernameUserProfileItem";
   private String propertyName;
   private boolean optional;
   private boolean modifiable;
   private boolean validateOnlyChangedValues = true;

   public UsernameUserProfileItemConfig(String stringResourceKey, boolean sortable, UserIterator checkUniqueness, boolean checkUniquenessAgainstUsername) {
      super(stringResourceKey, "MedusaUsernameUserProfileItem", false, false, true, sortable);
   }

   @Override
   public String getPropertyName() {
      return "MedusaUsernameUserProfileItem";
   }

   @Override
   public UserProfileItemLogic getProfileItemLogic() {
      return new UserProfileItemLogic() {
         @Override
         public UserProfileItemLogic.CheckResult validate(Object newValue, Object originalValue, String uid) {
            return UserProfileItemLogic.CheckResult.IS_OK;
         }

         @Override
         public Object formatForPersistence(Object value) {
            return StringUtils.trimToEmpty(Objects.toString(value, ""));
         }

         @Override
         public Object transformPersistentValue(Object value) {
            return Objects.toString(value, null);
         }

         @Override
         public boolean isModifiable(Object value) {
            return false;
         }
      };
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public static class UsernameUserProfileItemConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String stringResourceKey;
      @SuppressFBWarnings(justification = "generated code")
      private boolean sortable;
      @SuppressFBWarnings(justification = "generated code")
      private UserIterator checkUniqueness;
      @SuppressFBWarnings(justification = "generated code")
      private boolean checkUniquenessAgainstUsername;

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

      @SuppressFBWarnings(justification = "generated code")
      public UsernameUserProfileItemConfig.UsernameUserProfileItemConfigBuilder stringResourceKey(String stringResourceKey) {
         this.stringResourceKey = stringResourceKey;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      public UsernameUserProfileItemConfig.UsernameUserProfileItemConfigBuilder checkUniqueness(UserIterator checkUniqueness) {
         this.checkUniqueness = checkUniqueness;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UsernameUserProfileItemConfig.UsernameUserProfileItemConfigBuilder checkUniquenessAgainstUsername(boolean checkUniquenessAgainstUsername) {
         this.checkUniquenessAgainstUsername = checkUniquenessAgainstUsername;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public UsernameUserProfileItemConfig build() {
         return new UsernameUserProfileItemConfig(this.stringResourceKey, this.sortable, this.checkUniqueness, this.checkUniquenessAgainstUsername);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "UsernameUserProfileItemConfig.UsernameUserProfileItemConfigBuilder(stringResourceKey="
            + this.stringResourceKey
            + ", sortable="
            + this.sortable
            + ", checkUniqueness="
            + this.checkUniqueness
            + ", checkUniquenessAgainstUsername="
            + this.checkUniquenessAgainstUsername
            + ")";
      }
   }
}
