package com.ruoyi.web.controller.chenyanlogin.huicunutil;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;

@Slf4j
public class MapUtils {

    public static TreeMap<String, Object> map2ObjectTreeMap(Map<String, String> map) {
        TreeMap<String, Object> treeMap = new TreeMap<>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String value = entry.getValue();
            if (StrUtil.isEmpty(value)) {
                continue;
            }
            treeMap.put(entry.getKey(), value);
        }
        return treeMap;
    }

    public static TreeMap<String, String> map2StringTreeMap(Map<String, Object> map) {
        TreeMap<String, String> treeMap = new TreeMap<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String value;
            Object obj = entry.getValue();
            if (null == obj) {
                continue;
            }
//            if (obj instanceof Date) {
//                value = String.valueOf(((Date)obj).getTime());
//            } else {
//                value = obj.toString();
//            }
            if (obj instanceof String) {
                value = (String) obj;
            } else {
                value = obj.toString();
            }
            if (StrUtil.isEmpty(value)) {
                continue;
            }
            treeMap.put(entry.getKey(), value);
        }
        return treeMap;
    }

    public static TreeMap<String, Object> toTreeMap(Object obj, boolean isFilterNull) {
        return obj2Map(obj, isFilterNull, false);
    }

    private static TreeMap<String, Object> obj2Map(Object obj, boolean isFilterNull, boolean isJson) {
        TreeMap<String, Object> map = new TreeMap<>();
        if (obj == null) {
            return map;
        }
        try {
            Class clazz = obj.getClass();
            BeanInfo e = Introspector.getBeanInfo(clazz);
            PropertyDescriptor[] properties = e.getPropertyDescriptors();
            for (int i = 0; i < properties.length; i++) {
                PropertyDescriptor property = properties[i];
                String key = property.getName();
                if (key.equals("class")) {
                    continue;
                }
                Method getter = property.getReadMethod();
                Object value = getter.invoke(obj, new Object[0]);
                if (isFilterNull && value == null) {
                    continue;
                }
            /*    if (isJson) {
                    key = BeanUtil.getJsonColumnName(clazz, key);
                }*/
                map.put(key, value);
            }
        } catch (Exception e) {
            log.error("object to json map error", e);
        }
        return map;
    }

    /**
     * 将TreeMap中所有参数按升序转换成字符串，格式：key1=value1&key2=value2
     *
     * @param map
     * @return
     */
    public static String treeMap2ascString(Map<String, Object> map) {
        return treeMap2ascString(map, null);
    }

    /**
     * 将TreeMap中所有参数按升序转换成字符串，格式：key1=value1&key2=value2
     *
     * @param map
     * @param charset 编码，为空则不编码
     * @return
     */
    public static String treeMap2ascString(Map<String, Object> map, String charset) {
        return treeMap2ascString(map, charset, '&');
    }

    /**
     * 将TreeMap中所有参数按升序转换成字符串
     *
     * @param map
     * @param charset 编码，为空则不编码
     * @param link    连接符
     * @return
     */
    public static String treeMap2ascString(Map<String, Object> map, String charset, Character link) {
        StringBuilder sb = new StringBuilder();
        boolean isEncode = StrUtil.isNotBlank(charset);//是否url编码value
        boolean isLink = null != link;
        try {
            Iterator<Map.Entry<String, Object>> iter = map.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry<String, Object> entry = iter.next();
                String value;
                Object obj = entry.getValue();
                if (null == obj) {
                    continue;
                }
//                if (obj instanceof Date) {
//                    value = String.valueOf(((Date)obj).getTime());
//                } else {
                value = obj.toString();
//                }
                if (StrUtil.isEmpty(value)) {
                    continue;
                }
                sb.append(entry.getKey()).append("=").append(isEncode ? URLEncoder.encode(value, charset) : value);
                if (isLink) {
                    sb.append(link);
                }
            }
            int n = sb.length() - 1;
            if (isLink && n >= 0 && sb.charAt(n) == link.charValue()) {
                sb.deleteCharAt(n);
            }
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
        }
        return sb.toString();
    }

    public static Map<String, String> encodeTreeMap(Map<String, String> map, String charset) {
        Map<String, String> newMap = new TreeMap<String, String>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            try {
                String value = URLEncoder.encode(entry.getValue(), charset);
                newMap.put(entry.getKey(), value);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return newMap;
    }
}