package com.seer.teach.common.utils;

import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.exception.CommonException;

/**
 * 断言工具类，用于抛出 CommonException 异常
 */
public class AssertUtils {


    /**
     * 如果对象为 null，抛出异常
     *
     * @param object         要判断的对象
     * @param resultCodeEnum 错误码枚举
     */
    public static void notNull(Object object, ResultCodeEnum resultCodeEnum) {
        if (object == null) {
            throw new CommonException(resultCodeEnum);
        }
    }

    /**
     * 如果字符串为空或 null，抛出异常
     *
     * @param str            要判断的字符串
     * @param resultCodeEnum 错误码枚举
     */
    public static void notEmpty(String str, ResultCodeEnum resultCodeEnum) {
        if (str == null || str.trim().isEmpty()) {
            throw new CommonException(resultCodeEnum);
        }
    }

    /**
     * 如果集合为空或 null，抛出异常
     *
     * @param collection     集合
     * @param resultCodeEnum 错误码枚举
     */
    public static void notEmpty(java.util.Collection<?> collection, ResultCodeEnum resultCodeEnum) {
        if (collection == null || collection.isEmpty()) {
            throw new CommonException(resultCodeEnum);
        }
    }


    /**
     * 如果集合不为空，抛出异常
     * @param collection
     * @param resultCodeEnum
     */
    public static void isEmpty(java.util.Collection<?> collection, ResultCodeEnum resultCodeEnum) {
        if (collection != null && !collection.isEmpty()) {
            throw new CommonException(resultCodeEnum);
        }
    }

    /**
     * 如果布尔条件为 false，抛出异常
     *
     * @param expression     布尔表达式
     * @param resultCodeEnum 错误码枚举
     */
    public static void isTrue(boolean expression, ResultCodeEnum resultCodeEnum) {
        if (!expression) {
            throw new CommonException(resultCodeEnum);
        }
    }

    public static void isTrue(boolean expression, ResultCodeEnum resultCodeEnum, String args) {
        if (!expression) {
            throw new CommonException(resultCodeEnum,String.format(resultCodeEnum.getMsg(),  args));
        }
    }

    /**
     * 直接抛出异常
     *
     * @param resultCodeEnum 错误码枚举
     */
    public static void fail(ResultCodeEnum resultCodeEnum) {
        throw new CommonException(resultCodeEnum);
    }


    public static void isNull(Object one, ResultCodeEnum resultCodeEnum) {
        if (one != null){
            throw new CommonException(resultCodeEnum);
        }
    }

}
