package org.springblade.thirdparty.mk.util;

import org.springblade.core.tool.utils.StringUtil;
import org.springblade.thirdparty.mk.exception.MKException;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author bfhuange
 * @since 2025/2/24
 */
public class AssertUtils {

    /**
     * 对象非空
     * @param object
     * @param message
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new MKException(message);
        }
    }

    /**
     * Map非空
     * @param map
     * @param message
     */
    public static void notEmpty(Map<?, ?> map, String message) {
        if (CollectionUtils.isEmpty(map)) {
            throw new MKException(message);
        }
    }

    /**
     * 列表非空
     * @param collection
     * @param message
     */
    public static void notEmpty(Collection<?> collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new MKException(message);
        }
    }

    /**
     * 数组非空
     * @param array
     * @param message
     */
    public static void notEmpty(Object[] array, String message) {
        if (ObjectUtils.isEmpty(array)) {
            throw new MKException(message);
        }
    }

    /**
     * 字符串非空
     * @param value
     * @param message
     */
    public static void notEmpty(String value, String message) {
        if (StringUtil.hasLength(value)) {
            throw new MKException(message);
        }
    }

    /**
     * 字符串去除空格非空
     * @param value
     * @param message
     */
    public static void notBlank(String value, String message) {
        if (StringUtil.isBlank(value)) {
            throw new MKException(message);
        }
    }

    /**
     * 获取重复数据
     * @param dataList
     * @return
     * @throws MKException
     */
    public static List<String> getRepeatData(List<String> dataList) {
        if(CollectionUtils.isEmpty(dataList)) {
            return null;
        }
        //判断是否有重复数据
        List<String> distinctDataList = dataList.stream().distinct().toList();
        if(dataList.size() == distinctDataList.size()) {
            return null;
        }
        List<String> repeatList = new ArrayList<>();
        Map<String,Long> dataMap = dataList.stream().collect(Collectors.groupingBy(p -> p,Collectors.counting()));
        //处理重复数据
        for(Map.Entry<String,Long> entry:dataMap.entrySet()) {
            if(entry.getValue() > 1) {
                repeatList.add(entry.getKey());
            }
        }
        return repeatList;
    }

    /**
     * 数据不允许重复
     * @param dataList
     * @param message
     * @throws MKException
     */
    public static void notRepeat(List<String> dataList,String message) {
        //获取重复数据
        List<String> repeatList = getRepeatData(dataList);
        //若有重复数据，则校验不通过处理
        if(!CollectionUtils.isEmpty(repeatList)) {
            String repeatData = String.join(",",repeatList);
            throw new MKException(String.format(message,repeatData));
        }
    }

    /**
     * 字符串相等校验
     * @param str1
     * @param str2
     * @param message
     * @throws MKException
     */
    public static void equals(String str1, String str2, String message) {
        if (!StringUtil.equals(str1,str2)) {
            throw new MKException(message);
        }
    }

    /**
     * 数字相等校验
     * @param num1
     * @param num2
     * @param message
     * @throws MKException
     */
    public static void equals(BigDecimal num1, BigDecimal num2, String message) {
        if (num1.compareTo(num2) != 0) {
            throw new MKException(message);
        }
    }

    /**
     * 数字不大于校验
     * @param num1
     * @param num2
     * @param message
     * @throws MKException
     */
    public static void notGreaterThan(BigDecimal num1, BigDecimal num2, String message) {
        if(num1.compareTo(num2) > 0) {
            throw new MKException(message);
        }
    }

    /**
     * 数字不大于校验
     * @param num1
     * @param num2
     * @param message
     * @throws MKException
     */
    public static void notGreaterThan(Integer num1, Integer num2, String message) {
        if(num1 > num2) {
            throw new MKException(message);
        }
    }

    /**
     * 数字不小于校验
     * @param num1
     * @param num2
     * @param message
     * @throws MKException
     */
    public static void notLessThan(BigDecimal num1, BigDecimal num2, String message) {
        if(num1.compareTo(num2) < 0) {
            throw new MKException(message);
        }
    }

    /**
     * 表达式为真判断校验
     * @param expression
     * @param message
     * @throws MKException
     */
    public static void isTrue(boolean expression, String message) {
        if(!expression) {
            throw new MKException(message);
        }
    }

    /**
     * 表达式判断为假校验
     * @param expression
     * @param message
     * @throws MKException
     */
    public static void isFalse(boolean expression, String message) {
        if(expression) {
            throw new MKException(message);
        }
    }

    /**
     * 字符串不相等校验
     * @param str1
     * @param str2
     * @param message
     * @throws MKException
     */
    public static void notEquals(String str1, String str2, String message) {
        if (StringUtil.equals(str1,str2)) {
            throw new MKException(message);
        }
    }

    /**
     * 对象为空
     * @param object
     * @param message
     */
    public static void isNull(Object object, String message) {
        if (object != null) {
            throw new MKException(message);
        }
    }

    /**
     * 对象为数值
     * @param value
     * @param message
     * @throws MKException
     */
    public static void isInteger(String value,String message) {
        try {
            Integer.valueOf(value);
        } catch (NumberFormatException e) {
            throw new MKException(message);
        }
    }

    /**
     * 列表为空
     * @param collection
     * @param message
     */
    public static void isEmpty(Collection<?> collection, String message) {
        if (!CollectionUtils.isEmpty(collection)) {
            throw new MKException(message);
        }
    }
}
