package com.ningdatech.kqapi.common.util;

import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.NumberUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * <p>
 * BizUtils
 * </p>
 *
 * @author WendyYang
 * @since 17:32 2023/1/29
 */
public class BizUtils {

    private BizUtils() {

    }

    public static <T extends Collection<?>> void notEmpty(T objs, Consumer<T> consumer) {
        if (objs != null && !objs.isEmpty()) {
            consumer.accept(objs);
        }
    }

    public static <T extends Number> List<T> splitToNum(String str, Class<T> aClass) {
        if (StrUtil.isEmpty(str)) {
            return Collections.emptyList();
        }
        return Arrays.stream(str.split(StrPool.COMMA))
                .map(w -> NumberUtils.parseNumber(w, aClass))
                .collect(Collectors.toList());
    }

    public static List<Long> splitToLong(String str) {
        return splitToNum(str, Long.class);
    }


    public static <T> void notNull(T obj, Consumer<T> consumer) {
        if (obj != null) {
            consumer.accept(obj);
        }
    }

    public static void notBlank(String str, Consumer<String> consumer) {
        if (StrUtil.isNotBlank(str)) {
            consumer.accept(str);
        }
    }

    public static boolean getJSONType(String str) {
        boolean result = false;
        if (StringUtils.isNotBlank(str)) {
            str = str.trim();
            if (str.startsWith("{") && str.endsWith("}")) {
                result = true;
            } else if (str.startsWith("[") && str.endsWith("]")) {
                result = true;
            }
        }
        return result;
    }

    public static String uuid32() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 对象分组取第一条
     *
     * @param objs       对象集合
     * @param group      分组函数
     * @param comparator 比较器
     * @return java.util.Collection<T>
     * @author WendyYang
     **/
    public static <T> Collection<T> groupFirst(Collection<T> objs, Function<T, Object> group, Comparator<T> comparator) {
        return groupFirstMap(objs, group, comparator).values();
    }

    /**
     * 对象分组取第一条
     *
     * @param objs       对象集合
     * @param group      分组函数
     * @param comparator 比较器
     * @return java.util.Collection<T>
     * @author WendyYang
     **/
    public static <V, K> Map<K, V> groupFirstMap(Collection<V> objs, Function<V, K> group, Comparator<V> comparator) {
        return objs.stream().collect(Collectors.groupingBy(group,
                Collectors.collectingAndThen(toList(), w -> {
                    w.sort(comparator);
                    return w.get(0);
                })));
    }

    public static LocalDateTime convertDate(Date date) {
        if (Objects.nonNull(date)) {
            return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        }
        return null;
    }

    public static String computeRise(Integer numa, Integer numb) {
        if((Objects.isNull(numb)|| numb.equals(0)) &&
                (Objects.isNull(numa)|| numa.equals(0))){
            return "0";
        }
        if(Objects.isNull(numa) || numa.equals(0)){
            return "-100";
        }
        if(Objects.isNull(numb)|| numb.equals(0)){
            return "100";
        }
        return BigDecimal.valueOf((numa - numb) * 100.0).divide(BigDecimal.valueOf(numb),2, RoundingMode.CEILING)
                .stripTrailingZeros().toPlainString();
    }
}
