package com.lantone.qc.pub.util;

import com.google.common.collect.Maps;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: map工具类
 * @author: gaodm
 * @time: 2018/9/4 9:24
 */
public class MapUtil {
    private static final String SP = ";";
    private static final String SSP = ":";

    /**
     * 把Map转换成String。注意以英文分号字符'；'开始和结束
     *
     * @param attrs
     * @return
     */
    public static String toString(Map<String, String> attrs) {
        StringBuilder sb = new StringBuilder();
        if (null != attrs && !attrs.isEmpty()) {
            sb.append(SP);
            for (String key : attrs.keySet()) {
                String val = attrs.get(key);
                if (val != null && !"".equals(val)) {
                    sb.append(key).append(SSP).append(val).append(SP);
                }
            }
        }
        return sb.toString();
    }

    /**
     * 把key:value;key:value格式的String转换成Map
     *
     * @param str
     * @return
     */
    public static Map<String, String> fromString(String str) {
        Map<String, String> attrs = new HashMap<String, String>();
        if (str != null && !"".equals(str)) {
            String[] arr = str.split(SP);
            if (null != arr) {
                for (String kv : arr) {
                    if (kv != null && !"".equals(kv)) {
                        String[] ar = kv.split(SSP);
                        if (null != ar && ar.length == 2) {
                            String key = ar[0];
                            String val = ar[1];
                            if (val != null && !"".equals(val)) {
                                attrs.put(key, val);
                            }
                        }
                    }
                }
            }
        }
        return attrs;
    }

    public static Object mapToObject(Map<String, Object> map, Class<?> beanClass) throws Exception {
        if (map == null) {
            return null;
        }

        Object obj = beanClass.newInstance();

        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            Method setter = property.getWriteMethod();
            if (setter != null) {
                setter.invoke(obj, map.get(property.getName()));
            }
        }

        return obj;
    }

    public static Map<String, Object> objectToMap(Object obj) throws Exception {
        if (obj == null) {
            return null;
        }

        Map<String, Object> map = new HashMap<String, Object>();

        BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor property : propertyDescriptors) {
            String key = property.getName();
            if (key.compareToIgnoreCase("class") == 0) {
                continue;
            }
            Method getter = property.getReadMethod();
            Object value = getter != null ? getter.invoke(obj) : null;
            map.put(key, value);
        }
        return map;
    }

    /**
     * 复制一个map
     *
     * @param map
     * @return
     */
    public static Map<String, Object> copyMap(Map<String, Object> map) {
        if (map == null) {
            return null;
        }
        Map<String, Object> retMap = Maps.newHashMap();
        map.keySet().forEach(key -> {
            retMap.put(key, map.get(key));
        });
        return retMap;
    }

    /**
     * map key同义词赋值
     *
     * @param map
     * @param sourceKey
     * @param targetKeys
     */
    public static void keyAssig(Map<String, String> map, String sourceKey, String... targetKeys) {
        if (map == null || StringUtil.isBlank(sourceKey) || targetKeys == null || targetKeys.length == 0 || StringUtil.isNotBlank(map.get(sourceKey))) {
            return;
        }
        for (String targetKey : targetKeys) {
            if (StringUtil.isNotBlank(map.get(targetKey))) {
                map.put(sourceKey, map.get(targetKey));
                break;
            }
        }
    }


    /**
     * 按照指定的key拼接数据
     *
     * @param dataMap
     * @param keyList
     * @return 例如
     *         主诉:xxxxxxx
     *         性病史:xxxxxx
     */
    public static String getSelectString(Map<String, String> dataMap, List<String> keyList) {
        StringBuffer res = new StringBuffer();
        if (dataMap == null || ListUtil.isEmpty(keyList)) {
            return res.toString();
        }
        boolean firstLine = true; // 第一行不添加回车符
        for (String key : keyList) {
            if (!firstLine) {
                res.append("\r\n");
            } else {
                firstLine = false;
            }
            String value = dataMap.get(key) == null ?  "" :  dataMap.get(key);
            res.append(key).append(":").append(value);
        }
        return res.toString();
    }

    /**
     * 拷贝map
     * @param sourceMap
     * @return
     */
    public static Map<String, String> copyStringMap(Map<String, String> sourceMap) {
        Map<String, String> sourceMapCopy = Maps.newHashMap();
        sourceMapCopy.putAll(sourceMap);
        return sourceMapCopy;
    }


    /**
     * 打印key=,结构，用于复制
     *
     * @param map
     */
    public static void consoleKey(Map<String, String> map) {
        System.out.println("***************打印map内容开始******************");
        for (String key : map.keySet()) {
            System.out.println("\"" + key + "=\",");
        }
        System.out.println("***************打印map内容结束******************");
    }

    /**
     * 打印key=value结构
     *
     * @param map
     */
    public static void console(Map<String, String> map) {
        System.out.println("***************打印map内容开始******************");
        for (String key : map.keySet()) {
            System.out.println(key + "=" + map.get(key));
        }
        System.out.println("***************打印map内容结束******************");
    }

    /**
     * 打印key=value结构
     *
     * @param map
     */
    public static void console(Map<String, String> map, String title) {
        System.out.println("***************打印【" + title + "】内容开始******************");
        for (String key : map.keySet()) {
            System.out.println(key + "=" + map.get(key));
        }
        System.out.println("***************打印【" + title + "】内容结束******************");
    }

    public static void main(String[] args) {
        Map<String, String> dataMap = new HashMap<>();
        dataMap.put("主诉","主诉");
        dataMap.put("现病史","现病史");
        dataMap.put("其他史11","3");
        dataMap.put("4","4");

        System.out.println(getSelectString(dataMap, Arrays.asList("现病史","主诉","其他史")));
    }

}