package com.crm.rely.backend.util;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;

import java.util.HashMap;
import java.util.Map;

/**
 * @program:
 * @description:
 * @author: houn
 * @create: 2019-11-21 21:45
 */
public class MapUtil {

    /**
     * 根据map的key排序 默认升序
     *
     * @param map 待排序的map
     * @param <K>
     * @param <V>
     * @return 排序好的map
     */
    public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(Map<K, V> map) {
        return sortByKey(map, false);
    }

    /**
     * 根据map的key排序
     *
     * @param map    待排序的map
     * @param isDesc 是否降序，true：降序，false：升序
     * @return 排序好的map
     * @author zero 2019/04/08
     */
    public static <K extends Comparable<? super K>, V> Map<K, V> sortByKey(Map<K, V> map, boolean isDesc) {
        Map<K, V> result = Maps.newLinkedHashMap();
        if (isDesc) {
            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey().reversed())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        } else {
            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByKey())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        }
        return result;
    }

    /**
     * 根据map的value排序
     *
     * @param map    待排序的map
     * @param isDesc 是否降序，true：降序，false：升序
     * @return 排序好的map
     * @author zero 2019/04/08
     */
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map, boolean isDesc) {
        Map<K, V> result = Maps.newLinkedHashMap();
        if (isDesc) {
            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByValue().reversed())
                    .forEach(e -> result.put(e.getKey(), e.getValue()));
        } else {
            map.entrySet().stream().sorted(Map.Entry.<K, V>comparingByValue())
                    .forEachOrdered(e -> result.put(e.getKey(), e.getValue()));
        }
        return result;
    }

    public static String getString(Map<String, String> map) {

        return getString(map, false);
    }

    public static String getString(Map<String, String> map, boolean isEmptyValue) {
        String signString = "";
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (isEmptyValue || !Strings.isNullOrEmpty(entry.getValue())) {
                String str;
                if (Strings.isNullOrEmpty(signString)) {
                    str = entry.getKey() + "=" + entry.getValue();
                } else {
                    str = "&" + entry.getKey() + "=" + entry.getValue();
                }
                signString += str;
            }
        }
        return signString;
    }

    public static String getString(Map<String, String> map, String key) {

        return getString(map, key, false);
    }

    public static String getString(Map<String, String> map, String key, boolean isEmptyValue) {
        String signString = "";
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (isEmptyValue || !Strings.isNullOrEmpty(entry.getValue())) {
                String str;
                if (Strings.isNullOrEmpty(signString)) {
                    str = entry.getKey() + "=" + entry.getValue();
                } else {
                    str = "&" + entry.getKey() + "=" + entry.getValue();
                }
                signString += str;
            }
        }
        signString = signString + "&" + key;
        return signString;
    }

    /**
     * 根据key排序 并返回连接的值 key1=value1&key2=value2
     * 默认排序为升序
     *
     * @param map
     * @return
     */
    public static String getStringSortByKey(Map<String, String> map) {

        return getStringSortByKey(map, false);
    }

    /**
     * 根据key排序 并返回连接的值 key1=value1&key2=value2
     *
     * @param map
     * @param isDesc false 升序 true 降序
     * @return
     */
    public static String getStringSortByKey(Map<String, String> map, boolean isDesc) {
        return getStringSortByKey(map, isDesc, false);
    }

    public static String getStringSortByKey(Map<String, String> map, boolean isDesc, boolean isEmptyValue) {
        map = sortByKey(map, isDesc);
        String signString = getString(map, isEmptyValue);

        return signString;
    }

    /**
     * 根据value排序 并返回连接的值 key1=value1&key2=value2
     * 默认为升序
     *
     * @param map
     * @return
     */
    public static String getStringSortByValue(Map<String, String> map) {

        return getStringSortByValue(map, false);
    }

    /**
     * 根据value排序 并返回连接的值 key1=value1&key2=value2
     *
     * @param map
     * @param isDesc false 升序 true 降序
     * @return
     */
    public static String getStringSortByValue(Map<String, String> map, boolean isDesc) {
        map = sortByKey(map, isDesc);
        String signString = getString(map);

        return signString;
    }

    /**
     * 将 Map<String, Object> 转换为 Map<String, String>
     *
     * @param map 原始 Map
     * @return 转换后的 Map
     */
    public static Map<String, String> convertToStringMap(Map<String, Object> map) {
        if (map == null) {
            return null;
        }
        Map<String, String> result = new HashMap<>();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            result.put(entry.getKey(), String.valueOf(entry.getValue()));
        }
        return result;
    }

    /**
     * 创建一个只包含一个键值对的 Map
     *
     * @param key 键
     * @param value 值
     * @param <K> 键的类型
     * @param <V> 值的类型
     * @return 包含单个键值对的 Map
     */
    public static <K, V> Map<K, V> singletonMap(K key, V value) {
        Map<K, V> map = new HashMap<>();
        map.put(key, value);
        return map;
    }

}
