/*
 *
 *  * | Licensed 未经许可不能去掉「OPENIITA」相关版权
 *  * +----------------------------------------------------------------------
 *  * | Author: xw2sy@163.com
 *  * +----------------------------------------------------------------------
 *
 *  Copyright [2024] [OPENIITA]
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * /
 */

package cc.iotkit.data.util;

import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import java.util.function.Supplier;

/**
 * @author: Longjun.Tu
 * @description:
 * @date:created in 2023/5/26 17:02
 * @modificed by:
 */
public class PredicateBuilder {
  /**
   * 1 = 1, 永远为真.
   */
  private static final BooleanExpression ALWAYS_TRUE = Expressions.ONE.eq(Expressions.ONE);

  private BooleanExpression expression;

  public PredicateBuilder(BooleanExpression expression) {
    this.expression = expression;
  }

  /**
   * 获取{@link PredicateBuilder}实例.
   *
   * @return PredicateBuilder
   */
  public static PredicateBuilder instance() {
    return instance(null);
  }

  /**
   * 获取{@link PredicateBuilder}实例.
   *
   * @param init 初始条件
   * @return PredicateBuilder
   */
  public static PredicateBuilder instance(BooleanExpression init) {
    return new PredicateBuilder(init);
  }

  /**
   * 使用 'and' 对条件进行拼接
   *
   * @param expr Boolean expressions
   * @return PredicateBuilder
   */
  public PredicateBuilder and(BooleanExpression expr) {
    return and(true, () -> expr);
  }

  /**
   * 如果条件为true, 则使用 'and' 对条件进行拼接
   *
   * @param condition    执行条件, 如果为false, 则不会拼接该条件.
   * @param exprSupplier expression supplier.
   * @return PredicateBuilder
   */
  public PredicateBuilder and(boolean condition, Supplier<BooleanExpression> exprSupplier) {
    if (condition) {
      if (exprIsNull()) {
        expression = exprSupplier.get();
        return this;
      }
      expression = expression.and(exprSupplier.get());
    }
    return this;
  }

  /**
   * 使用 'or' 对条件进行拼接
   *
   * @param expr Boolean expressions
   * @return PredicateBuilder
   */
  public PredicateBuilder or(BooleanExpression expr) {
    return or(true, () -> expr);
  }

  /**
   * 如果条件为true, 则使用 'or' 对条件进行拼接
   *
   * @param condition    执行条件, 如果为false, 则不会拼接该条件.
   * @param exprSupplier expression supplier.
   * @return PredicateBuilder
   */
  public PredicateBuilder or(boolean condition, Supplier<BooleanExpression> exprSupplier) {
    if (condition) {
      if (exprIsNull()) {
        expression = exprSupplier.get();
        return this;
      }
      expression = expression.or(exprSupplier.get());
    }
    return this;
  }

  /**
   * 获取最终的{@link BooleanExpression}表达式
   *
   * @return Predicate, 如果没有拼接条件, 则默认返回 1 = 1的表达式.
   */
  public Predicate build() {
    if (exprIsNull()) {
      return ALWAYS_TRUE;
    }
    return expression;
  }

  private boolean exprIsNull() {
    return null == expression;
  }
}
