package com.boman.common.core.utils.obj;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;
import java.util.Objects;

/**
 * @author shiqian
 * @date 2021年03月26日 10:39
 **/
public class ObjectUtils {

    private static final String NULL = "null";
    private static final String UNDEFINED = "undefined";

    public static  <T> Collection<T> requiredNonNull(Collection<T> input){
        if (null == input || input.size() == 0) {
            throw new IllegalArgumentException("所传参数为空");
        }
        return input;
    }

    public static long[] requiredNonNull(long[] input){
        if (ArrayUtils.isEmpty(input)) {
            throw new IllegalArgumentException("所传参数为空");
        }

        return input;
    }


    public static Long[] requiredNonNull(Long[] input){
        if (ArrayUtils.isEmpty(input)) {
            throw new IllegalArgumentException("所传参数为空");
        }

        return input;
    }

    public static <T> T requireNonNull(T input) {
        if (input == null) {
            throw new IllegalArgumentException("所传参数为空");
        }
        return input;
    }

    public static Integer requireNonNull(Integer input) {
        if (input == null || input < 0) {
            throw new IllegalArgumentException("所传参数为空");
        }
        return input;
    }

    public static boolean isEmpty(Integer input) {
        return input == null || input < 0;
    }

    public static boolean isNotEmpty(Integer input) {
        return !isEmpty(input);
    }


    public static String requireNonNull(String input) {
        if (input == null || input.isEmpty() || NULL.equalsIgnoreCase(input) || UNDEFINED.equalsIgnoreCase(input)) {
            throw new IllegalArgumentException("所传参数为空");
        }
        return input;
    }

    public static int requireNonNull(int input) {
        if (input <= 0) {
            throw new IllegalArgumentException("所传参数为空");
        }
        return input;
    }


    public static JSONObject requireNonNull(JSONObject input) {
        if (input == null || input.isEmpty()) {
            throw new IllegalArgumentException("所传参数为空");
        }
        return input;
    }

    /**
     * 功能描述: 暂且只做 string collection long 三种类型的校验
     *
     * @param input input
     * @return boolean
     */
    public static boolean isEmpty(Object input) {
        if (input instanceof String) {
            return StringUtils.isEmpty((String) input);
        } else if (input instanceof Collection) {
            return CollectionUtils.isEmpty(((Collection<?>) input));
        } else {
            return Objects.isNull(input);
        }
    }

    public static boolean isNotEmpty(Object input) {
        return !isEmpty(input);
    }

    public static JSONArray requireNonNull(JSONArray input) {
        if (input == null || input.isEmpty()) {
            throw new IllegalArgumentException("所传参数为空");
        }
        return input;
    }

    /**
     * 功能描述: 需要判断数据库是什么类型，如果是VARCHAR则需要转，如果是数字则无需转
     *
     * @param input 输入
     * @return java.lang.String
     */
    public static String escapeStr(String input) {
        return "'" + input + "'";
    }



}
