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

import com.airlock.iam.common.domain.model.user.profileitems.BooleanProfileItemLogic;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.property.BooleanProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

@Plugin(
   description = "A configurable user profile item of type boolean. This will be represented as a checkbox in input form. The selected value is added to the user's context data, provided that the property name matches the property name in the configured user data. If the item is configured as not optional, the user is forced to check the field before they are allowed to continue. This can be used to require the user to accept terms and conditions."
)
public class BooleanUserProfileItemConfig extends UserProfileItemConfigBase {
   @BooleanProperty(
      description = "Determines whether the value should be saved as a java.lang.Boolean object. If this is not selected,  the value is saved as a String.",
      optional = true,
      defaultValue = "true"
   )
   private boolean formatAsBooleanObject;

   public BooleanUserProfileItemConfig(
      String stringResourceKey,
      String propertyName,
      boolean optional,
      boolean modifiable,
      boolean validateOnlyChangedValues,
      boolean sortable,
      boolean formatAsBooleanObject
   ) {
      super(stringResourceKey, propertyName, optional, modifiable, validateOnlyChangedValues, sortable);
      this.formatAsBooleanObject = formatAsBooleanObject;
   }

   public BooleanProfileItemLogic getProfileItemLogic() {
      return BooleanProfileItemLogic.builder()
         .formatAsBooleanObject(this.formatAsBooleanObject)
         .optional(this.isOptional())
         .modifiable(this.isModifiable())
         .validateOnlyChangedValues(this.isValidateOnlyChangedValues())
         .build();
   }

   @Override
   public UserProfileItemConfig.ProfileItemType getItemType() {
      return UserProfileItemConfig.ProfileItemType.BOOLEAN;
   }

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

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

   @SuppressFBWarnings(justification = "generated code")
   public static class BooleanUserProfileItemConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String stringResourceKey;
      @SuppressFBWarnings(justification = "generated code")
      private String propertyName;
      @SuppressFBWarnings(justification = "generated code")
      private boolean optional;
      @SuppressFBWarnings(justification = "generated code")
      private boolean modifiable;
      @SuppressFBWarnings(justification = "generated code")
      private boolean validateOnlyChangedValues;
      @SuppressFBWarnings(justification = "generated code")
      private boolean sortable;
      @SuppressFBWarnings(justification = "generated code")
      private boolean formatAsBooleanObject;

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

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

      @SuppressFBWarnings(justification = "generated code")
      public BooleanUserProfileItemConfig.BooleanUserProfileItemConfigBuilder propertyName(String propertyName) {
         this.propertyName = propertyName;
         return this;
      }

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

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

      @SuppressFBWarnings(justification = "generated code")
      public BooleanUserProfileItemConfig.BooleanUserProfileItemConfigBuilder validateOnlyChangedValues(boolean validateOnlyChangedValues) {
         this.validateOnlyChangedValues = validateOnlyChangedValues;
         return this;
      }

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

      @SuppressFBWarnings(justification = "generated code")
      public BooleanUserProfileItemConfig.BooleanUserProfileItemConfigBuilder formatAsBooleanObject(boolean formatAsBooleanObject) {
         this.formatAsBooleanObject = formatAsBooleanObject;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public BooleanUserProfileItemConfig build() {
         return new BooleanUserProfileItemConfig(
            this.stringResourceKey,
            this.propertyName,
            this.optional,
            this.modifiable,
            this.validateOnlyChangedValues,
            this.sortable,
            this.formatAsBooleanObject
         );
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "BooleanUserProfileItemConfig.BooleanUserProfileItemConfigBuilder(stringResourceKey="
            + this.stringResourceKey
            + ", propertyName="
            + this.propertyName
            + ", optional="
            + this.optional
            + ", modifiable="
            + this.modifiable
            + ", validateOnlyChangedValues="
            + this.validateOnlyChangedValues
            + ", sortable="
            + this.sortable
            + ", formatAsBooleanObject="
            + this.formatAsBooleanObject
            + ")";
      }
   }
}
