package com.yonge.toolset.utils.collection;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class ListUtil {
    private final static Logger log = LoggerFactory.getLogger(ListUtil.class);

    /**
     * 两个集合是否相等，与元素顺序无关
     *
     * @param list1
     * @param list2
     * @return
     */
    public static boolean isEquals(List<?> list1, List<?> list2) {
        if (list1 == list2) {
            return true;
        }
        if (list1 == null && list2 == null) {
            return true;
        }
        if (list1 == null || list2 == null) {
            return false;
        }
        if (list1.size() != list2.size()) {
            return false;
        }
        if (list1.containsAll(list2) && list2.containsAll(list1)) {
            return true;
        }
        return false;
    }

    /**
     * 将list转成queue
     *
     * @return java.util.Queue<T>
     * @Author weifanl
     * @Date 14:49 2020/6/12
     * @Param [list]
     **/
    public static <T> Queue<T> convertListToQueue(List<T> list, Class<T> clazz) {
        Queue<T> queue = new LinkedList<>();
        if (CollectionUtils.isEmpty(list)) {
            return queue;
        }
        list.forEach(obj -> {
            T clone = obj;
            if (null != obj && !isBaseType(obj.getClass())) {
                try {
                    T t = clazz.newInstance();
                    BeanUtils.copyProperties(obj, t);
                    clone = t;
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            queue.add(clone);
        });
        return queue;
    }

    /**
     * list对象转换
     *
     * @return java.util.List<T>
     * @Author weifanl
     * @Date 17:43 2020/6/17
     * @Param [list, clazz]
     **/
    public static <T> List<T> convertList(List list, Class<T> clazz) {
        List<T> retList = new ArrayList<>();
        if (CollectionUtils.isEmpty(list)) {
            return retList;
        }

        list.forEach(item -> {
            try {
                T t = clazz.newInstance();
                BeanUtils.copyProperties(item, t);
                retList.add(t);
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });
        return retList;
    }


    public static boolean isBaseType(Class className) {
        return className.equals(String.class) ||
                className.equals(Integer.class) ||
                className.equals(int.class) ||
                className.equals(Byte.class) ||
                className.equals(byte.class) ||
                className.equals(Long.class) ||
                className.equals(long.class) ||
                className.equals(Double.class) ||
                className.equals(double.class) ||
                className.equals(Float.class) ||
                className.equals(float.class) ||
                className.equals(Character.class) ||
                className.equals(char.class) ||
                className.equals(Short.class) ||
                className.equals(short.class) ||
                className.equals(Boolean.class) ||
                className.equals(boolean.class);
    }

    public static <T> T convertToBaseType(Object source, Class<T> clazz) {
        try {
            if (clazz.equals(String.class)) {
                return (T) source.toString();
            } else if (clazz.equals(Integer.class) || clazz.equals(int.class)) {
                return (T) (Integer) Integer.parseInt(source.toString());
            } else if (clazz.equals(Byte.class) || clazz.equals(byte.class)) {
                return (T) (Byte) Byte.parseByte(source.toString());
            } else if (clazz.equals(Long.class) || clazz.equals(long.class)) {
                return (T) (Long) Long.parseLong(source.toString());
            } else if (clazz.equals(Double.class) || clazz.equals(double.class)) {
                return (T) (Double) Double.parseDouble(source.toString());
            } else if (clazz.equals(Float.class) || clazz.equals(float.class)) {
                return (T) (Float) Float.parseFloat(source.toString());
            } else if (clazz.equals(Character.class) || clazz.equals(char.class)) {
                return (T) (Integer) Integer.parseInt(source.toString());
            } else if (clazz.equals(Short.class) || clazz.equals(short.class)) {
                return (T) (Short) Short.parseShort(source.toString());
            } else if (clazz.equals(Boolean.class) || clazz.equals(boolean.class)) {
                return (T) (Boolean) Boolean.parseBoolean(source.toString());
            }

            try {
                T t = clazz.newInstance();
                BeanUtils.copyProperties(source, t);
                return t;
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } catch (NumberFormatException e) {
            log.error("转换基础类型异常!");
            log.error("异常方法：ListUtil.convertToBaseType()");
            log.error("异常参数：" + JSONObject.toJSONString(source));
            log.error("异常参数：" + clazz);
            log.error("异常内容：" + e.getMessage());
        }
        return null;
    }
}
