package yem.base.common.utils;

import java.lang.reflect.Array;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 图表工具类
 *
 * @author longh
 * @description:
 * @date 2022年6月23日 下午4:36:02
 */
public class ChartUtil {
    private static List<String> pointSymbols = Stream.of("pin", "arrow", "rect", "diamond", "triangle", "circle").collect(Collectors.toList());

    /***
     * 随机生成折线图图标
     * @return
     */
    public static List<String> getSymbols(int n) {
        return Stream.generate(ChartUtil::getSymbol).limit(n).collect(Collectors.toList());
    }

    /***
     * 随机生成折线图图标
     * @return
     */
    public static String getSymbol() {
        return pointSymbols.get((int) (Math.random() * pointSymbols.size()));
    }

    /**
     * 随机生成颜色
     *
     * @return
     */
    public static List<String> getRandColors(int n) {
        return Stream.generate(ChartUtil::getRandColor).limit(n).collect(Collectors.toList());
    }

    /**
     * 随机生成颜色
     *
     * @return
     */
    public static String getRandColor() {
        Random random = new Random();
        return "#" + Stream.generate(() -> {
            String r = Integer.toHexString(random.nextInt(256)).toUpperCase();
            return r.length() == 1 ? "0" + r : r;
        }).limit(3).collect(Collectors.joining());
    }

    /**
     * 从集合中随机取出N个不重复的元素
     *
     * @param list
     * @param n
     * @return
     */
    public static <T> List<T> getRandList(List<T> list, int n) {
        if (list == null || list.size() <= n) {
            return list;
        } else {
            List<T> olds = list.stream().collect(Collectors.toList());
            return Stream.generate(() -> {
                return olds.remove((int) (Math.random() * olds.size()));
            }).limit(n).collect(Collectors.toList());
        }
    }

    /**
     * 从数组中随机取出N个不重复的元素
     *
     * @param array
     * @param n
     * @return
     */
    public static <T> T[] getRandArray(T[] array, int n) {
        if (array == null || array.length <= n) {
            return array;
        } else {
            @SuppressWarnings("unchecked") T[] news = (T[]) Array.newInstance(array.getClass().getComponentType(), n);
            List<T> olds = Stream.of(array).collect(Collectors.toList());
            return Stream.generate(() -> {
                return olds.remove((int) (Math.random() * olds.size()));
            }).limit(n).collect(Collectors.toList()).toArray(news);
        }
    }

}
