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

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.Strings;

public class BooleanProfileItemLogic implements UserProfileItemLogic {
   private final boolean formatAsBooleanObject;
   private final boolean validateOnlyChangedValues;
   private final boolean optional;
   private final boolean modifiable;

   @Override
   public UserProfileItemLogic.CheckResult validate(Object newValue, Object oldValue, String uid) {
      if (!UserProfileItemLogic.validationRequired(newValue, oldValue, this.validateOnlyChangedValues)) {
         return UserProfileItemLogic.CheckResult.IS_OK;
      } else {
         Boolean bool = this.asBoolean(newValue);
         if (!this.optional && !bool) {
            return UserProfileItemLogic.CheckResult.REQUIRED;
         } else {
            return newValue instanceof String
                  && !Strings.CI.equals((String)newValue, Boolean.TRUE.toString())
                  && !Strings.CI.equals((String)newValue, Boolean.FALSE.toString())
               ? UserProfileItemLogic.CheckResult.WRONG_FORMAT
               : UserProfileItemLogic.CheckResult.IS_OK;
         }
      }
   }

   @Override
   public Object formatForPersistence(Object value) {
      return this.formatAsBooleanObject ? this.asBoolean(value) : this.asBoolean(value).toString();
   }

   private Boolean asBoolean(Object value) {
      if (value instanceof Boolean) {
         return (Boolean)value;
      } else {
         return value instanceof Number ? ((Number)value).intValue() >= 1 : Boolean.parseBoolean((String)value);
      }
   }

   @Override
   public Object transformPersistentValue(Object value) {
      return this.asBoolean(value);
   }

   @Override
   public boolean isModifiable(Object value) {
      return this.modifiable;
   }

   @SuppressFBWarnings(justification = "generated code")
   BooleanProfileItemLogic(boolean formatAsBooleanObject, boolean validateOnlyChangedValues, boolean optional, boolean modifiable) {
      this.formatAsBooleanObject = formatAsBooleanObject;
      this.validateOnlyChangedValues = validateOnlyChangedValues;
      this.optional = optional;
      this.modifiable = modifiable;
   }

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

   @SuppressFBWarnings(justification = "generated code")
   public static class BooleanProfileItemLogicBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private boolean formatAsBooleanObject;
      @SuppressFBWarnings(justification = "generated code")
      private boolean validateOnlyChangedValues;
      @SuppressFBWarnings(justification = "generated code")
      private boolean optional;
      @SuppressFBWarnings(justification = "generated code")
      private boolean modifiable;

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

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

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

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

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

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

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "BooleanProfileItemLogic.BooleanProfileItemLogicBuilder(formatAsBooleanObject="
            + this.formatAsBooleanObject
            + ", validateOnlyChangedValues="
            + this.validateOnlyChangedValues
            + ", optional="
            + this.optional
            + ", modifiable="
            + this.modifiable
            + ")";
      }
   }
}
