package com.dayou.utils;

import org.apache.commons.collections.CollectionUtils;
import org.dozer.DozerBeanMapper;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 类说明：类型转换器 包括VO DTO entity间的转化
 *
 * @author: wucl
 * @since: 2022/11/28
 * created with IntelliJ IDEA.
 */
public class ConvertUtil {

    //对象转换核心实例
    private final static DozerBeanMapper dozer = new DozerBeanMapper() {
    };

    private ConvertUtil() {
    }

    /**
     * 将src对象中的属性值复制到desc对象的同名属性中
     *
     * @param src  源
     * @param desc 目标
     */
    public static void copyProperties(Object src, Object desc) {
        if (src == null || desc == null) {
            throw new IllegalArgumentException("param must not be null");
        }
        dozer.map(src, desc);
    }

    /**
     * 将src对象中的属性值复制到新建的desc对象的同名属性中
     *
     * @param src       源
     * @param descClass
     * @return 结果
     */
    public static <T> T copyProperties(Object src, Class<T> descClass) {
        if (src == null || descClass == null) {
            throw new IllegalArgumentException("param must not be null");
        }
        return (T) dozer.map(src, descClass);
    }

    /**
     * 将srcList中的元素复制到元素类型为descClass的List集合中
     *
     * @param srcList   源
     * @param descClass
     * @return 结果
     */
    public static <T> List<T> copyList(@SuppressWarnings("rawtypes") List srcList, Class<T> descClass) {
        if (CollectionUtils.isEmpty(srcList)) {
            return Collections.EMPTY_LIST;
        }
        if (srcList == null || descClass == null) {
            throw new IllegalArgumentException("param must not be null");
        }
        List<T> descList = new ArrayList<T>();
        if (srcList != null) {
            for (Object obj : srcList) {
                T t = copyProperties(obj, descClass);
                descList.add(t);
            }
        }
        return descList;
    }

    /**
     * 转换为字符串<br>
     * 如果给定的值为<code>null</code>，或者转换失败，返回默认值<code>null</code><br>
     * 转换失败不会报错
     *
     * @param value 被转换的值
     * @return 结果
     */
    public static String toStr(Object value) {
        return toStr(value, null);
    }

    /**
     * 转换为字符串<br>
     * 如果给定的值为null，或者转换失败，返回默认值<br>
     * 转换失败不会报错
     *
     * @param value        被转换的值
     * @param defaultValue 转换错误时的默认值
     * @return 结果
     */
    public static String toStr(Object value, String defaultValue) {
        if (null == value) {
            return defaultValue;
        }
        if (value instanceof String) {
            return (String) value;
        }
        return value.toString();
    }

    /*    *//**
     * 将srcList中的元素复制到元素类型为descClass的List集合中
     *
     * @param srcList 源
     * @return 结果
     *//*
    public static void copyList(@SuppressWarnings("rawtypes") List srcList, List list) {
        if (CollectionUtils.isEmpty(srcList)) {
            return;
        }
        if (srcList == null || list == null) {
            throw new IllegalArgumentException("param must not be null");
        }
        if (srcList != null) {
            dozer.map(srcList, list);
        }
    }*/

    /**
     * 将srcList中的元素复制到元素类型为descClass的List集合中
     *
     * @param collections 源
     * @param descClass
     * @return 结果
     */
    public static <T> Set<T> copySet(@SuppressWarnings("rawtypes") Collection collections, Class<T> descClass) {
        if (descClass == null) {
            throw new IllegalArgumentException("param must not be null");
        }
        Set<T> descSet = new HashSet<>();
        if (collections != null) {
            for (Object obj : collections) {
                T t = copyProperties(obj, descClass);
                descSet.add(t);
            }
        }
        return descSet;
    }

    public static void copySet(@SuppressWarnings("rawtypes") Collection collections, Set set) {
        if (set == null) {
            throw new IllegalArgumentException("param must not be null");
        }
        if (collections != null) {
            dozer.map(collections, set);
        }
    }

    public static <T, S> T copyBean(S s, Convertor<S, T> convertor) {
        return convertor.convert(s);
    }

    public static <T, S> List<T> copyList(List<S> sourceList, Convertor<S, T> convertor) {
        if (CollectionUtils.isEmpty(sourceList)) {
            return Collections.EMPTY_LIST;
        }

        List<T> targetList = sourceList.stream().map(x -> copyBean(x, convertor)).collect(Collectors.toList());
        return targetList;
    }

}
