package com.smppw.utils;

import cn.hutool.core.collection.CollUtil;
import com.smppw.common.pojo.CommonData;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonUtil {

    private static final String SEPARATE = "_";

    public static Boolean isEmptyCollection(Collection<?> collection) {
        return (null == collection || 0 == collection.size());
    }

    public static Double object2Double(Object obj) {
        return obj == null ? null : Double.valueOf(obj.toString());
    }

    @SuppressWarnings("unchecked")
    public static <T> List<T> removeNull(List<? extends T> dataList) {
        dataList.removeAll(Collections.singleton(null));
        return (List<T>) dataList;
    }

    public static String double2String(Double v) {
        if (v == null) {
            return null;
        } else {
            return v.toString();
        }
    }

    public static String double2StringPoint6(Double v) {
        if (v == null) {
            return null;
        } else {
            return String.format("%.6f", v);
        }
    }

    public static Integer string2Integer(String s) {
        Integer v = null;
        if (s != null) {
            v = Integer.valueOf(s);
        }
        return v;
    }

    public static String integer2String(Integer v) {
        if (v == null) {
            return null;
        } else {
            return v.toString();
        }
    }

    public static List<Double> string2Double(List<String> list) {
        List<Double> values = new ArrayList<>();
        for (String unit : list) {
            values.add(string2Double(unit));
        }
        return values;
    }

    public static Double string2Double(String s) {
        Double v = null;
        if (s != null) {
            v = Double.valueOf(s);
        }
        return v;
    }

    public static double[] list2Array(List<Double> list) {
        int n = list.size();
        if (n == 0) {
            return new double[]{};
        }
        double[] arr = new double[n];
        for (int i = 0; i < n; i++) {
            arr[i] = list.get(i);
        }
        return arr;
    }

    public static List<String> string2List(String str) {
        if (StringUtils.isEmpty(str)) {
            return new ArrayList<>();
        }

        List<String> list = new ArrayList<>();
        for (String s : str.split(",")) {
            if (StringUtils.isNotEmpty(s)) {
                list.add(s);
            }
        }

        return list;
    }

    public static List<String> string2ListByUnderline(String str) {
        if (StringUtils.isEmpty(str)) {
            return new ArrayList<>();
        }

        List<String> list = new ArrayList<>();
        for (String s : str.split(SEPARATE)) {
            if (StringUtils.isNotEmpty(s)) {
                list.add(s);
            }
        }

        return list;
    }


    public static double[][] List2Array(List<List<Double>> lists) {
        int n = lists.size();
        if (n == 0) {
            return new double[][]{};
        }
        int m = lists.get(0).size();
        double[][] arr = new double[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                arr[i][j] = lists.get(i).get(j);
            }
        }
        return arr;
    }

    public static String double2IntegerString(Double v) {
        if (v == null || v.isInfinite() || v.isNaN()) {
            return null;
        } else {
            return BigDecimalUtils.toBigDecimal(v).setScale(0, RoundingMode.HALF_UP).toPlainString();
        }
    }

    public static String double2StringPoint6WithoutHalfUp(Double v) {
        if (v == null) {
            return null;
        } else {
            double v1 = BigDecimalUtils.toBigDecimal(v).setScale(16, BigDecimal.ROUND_HALF_UP).doubleValue();
            return BigDecimalUtils.toBigDecimal(v1).setScale(6, BigDecimal.ROUND_DOWN).toPlainString();
        }
    }

    /**
     * 把CommonData扩展为map对象
     *
     * @param onlyExt   是否仅根据extInfos字段扩展
     * @param extFields 要扩展的字段，从extInfos取数据
     * @return /
     */
    public static <T> Map<String, Object> covertToMap(CommonData<T> data, boolean onlyExt, String... extFields) {
        Map<String, Object> result = new HashMap<>();
        result.put("dataset", data.getDataset());
        result.put("productNameMapping", data.getProductNameMapping());
        Map<String, Object> extInfos = data.getExtInfos();
        if (onlyExt && extInfos != null && !extInfos.isEmpty()) {
            result.putAll(extInfos);
            return result;
        }
        if (extFields != null && extInfos != null && extFields.length > 0) {
            for (String field : extFields) {
                result.put(field, extInfos.get(field));
            }
        }
        return result;
    }

    public static <T> Map<String, Object> covertToMap(CommonData<T> data) {
        return covertToMap(data, true);
    }

    public static <T> Map<String, Object> covertToMap(CommonData<T> data, String... extFields) {
        return covertToMap(data, false, extFields);
    }

    public static String list2StringWithDot(List<String> idList) {
        StringJoiner stringJoiner = new StringJoiner("','", "'", "'");
        for (String s : idList) {
            stringJoiner.add(s);
        }
        return stringJoiner.toString();
    }

    public static String integerList2StringWithDot(List<Integer> idList) {
        StringJoiner stringJoiner = new StringJoiner("','", "'", "'");
        for (Integer s : idList) {
            stringJoiner.add(s + "");
        }
        return stringJoiner.toString();
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public static List<String> intersectionDistinct(List<List<String>> coll) {
        List<String> res = CollUtil.newArrayList();
        if (CollUtil.isEmpty(coll)) {
            return res;
        }
        for (int i = 0; i < coll.size(); i++) {
            if (i == 0) {
                res = coll.get(0);
                continue;
            }
            List<String> cur = coll.get(i);
            res = CollUtil.newArrayList(CollUtil.intersection(res, cur));
        }
        return res;
    }
}
