package cc.smtweb.framework.core.util;

import cc.smtweb.framework.core.common.SwMap;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * map工具类
 */
public class MapUtil {
    private MapUtil() {
    }

    public static String readString(Map map, String name) {
        return readString(map, name, "");
    }

    public static String readString(Map map, String name, String defaultValue) {
        Object s = map.get(name);

        if (s != null) {
            return s.toString();
        }

        return defaultValue;
    }

    public static long readLong(Map map, String name) {
        return readLong(map, name, 0L);
    }

    public static long readLong(Map map, String name, Long defaultValue) {
        Object s = map.get(name);

        if (s != null) {
            if (s instanceof Number) {
                return ((Number) s).longValue();
            } else {
                String value = s.toString();
                if (StringUtils.isNotBlank(value)) {
                    return Long.parseLong(value);
                }
            }
        }

        return defaultValue;
    }

    public static Long[] readLongArray(Map map, String name) {
        return readLongArray(map, name, null);
    }

    public static Long[] readLongArray(Map map, String name, Long[] defaultValue) {
        Object value = map.get(name);

        if (value != null) {
            if (value instanceof Long[]) {
                return (Long[]) value;
            } else if (value instanceof Object[]) {
                Object[] items = (Object[]) value;
                List<Long> result = new ArrayList<>(items.length);

                for (Object item : items) {
                    if (item != null) {
                        String s = item.toString();
                        if (StringUtils.isNotBlank(s)) {
                            result.add(Long.valueOf(s));
                        }
                    }
                }

                if (result.size() > 0) {
                    return result.toArray(new Long[result.size()]);
                }
            } else if (value instanceof List) {
                List items = (List) value;
                List<Long> result = new ArrayList<>(items.size());

                for (Object item : items) {
                    if (item != null) {
                        String s = item.toString();
                        if (StringUtils.isNotBlank(s)) {
                            result.add(Long.valueOf(s));
                        }
                    }
                }

                if (result.size() > 0) {
                    return result.toArray(new Long[result.size()]);
                }
            } else if (value instanceof String) {
                String[] ary = value.toString().split(",");
                if (ary.length > 0) {
                    List<Long> result = new ArrayList<>(ary.length);

                    for (String item : ary) {
                        if (StringUtils.isNotBlank(item)) {
                            result.add(Long.valueOf(item));
                        }
                    }

                    if (result.size() > 0) {
                        return result.toArray(new Long[result.size()]);
                    }
                }
            }
        }

        return defaultValue;
    }

    public static int readInt(Map map, String name) {
        return readInt(map, name, 0);
    }

    public static int readInt(Map map, String name, int defaultValue) {
        Object s = map.get(name);

        if (s != null) {
            if (s instanceof Number) {
                return ((Number) s).intValue();
            } else {
                String value = s.toString();
                if (StringUtils.isNotBlank(value)) {
                    return Integer.parseInt(value);
                }
            }
        }

        return defaultValue;
    }

    public static float readFloat(Map map, String name) {
        return readFloat(map, name, 0.0F);
    }

    public static float readFloat(Map map, String name, float defaultValue) {
        Object s = map.get(name);

        if (s != null) {
            if (s instanceof Number) {
                return ((Number) s).floatValue();
            } else {
                String value = s.toString();
                if (StringUtils.isNotBlank(value)) {
                    return Float.parseFloat(value);
                }
            }
        }

        return defaultValue;
    }

    public static double readDouble(Map map, String name) {
        return readDouble(map, name, 0d);
    }

    public static double readDouble(Map map, String name, double defaultValue) {
        Object s = map.get(name);

        if (s != null) {
            if (s instanceof Number) {
                return ((Number) s).doubleValue();
            } else {
                String value = s.toString();
                if (StringUtils.isNotBlank(value)) {
                    return Double.parseDouble(value);
                }
            }
        }

        return defaultValue;
    }

    public static boolean readBool(Map map, String name) {
        return readBool(map, name, false);
    }

    public static boolean readBool(Map map, String name, boolean defaultValue) {
        Object s = map.get(name);

        if (s != null) {
            if (s instanceof Boolean) {
                return (Boolean) s;
            } else {
                String value = s.toString();
                if ("true".equalsIgnoreCase(value) || "1".equals(value) || "y".equalsIgnoreCase(value)) {
                    return Boolean.TRUE;
                }
            }
        }

        return defaultValue;
    }

    public static Set<Long> readLongSet(SwMap swMap, String name) {
        Object value = swMap.get(name);
        if (value != null) {
            String[] ary = value.toString().split(",");
            if (ary.length > 0) {
                Set<Long> result = new HashSet<>(ary.length);

                for (String item : ary) {
                    if (StringUtils.isNotBlank(item)) {
                        result.add(Long.valueOf(item));
                    }
                }

                return result;
            }
        }

        return null;
    }

    public static List<String> readStringList(SwMap swMap, String name) {
        Object value = swMap.get(name);
        if (value != null && StringUtil.isNotEmpty(value.toString())) {
            return Arrays.asList(value.toString().split(","));
        }
        return null;
    }

    public static List<Map<String, Object>> readListMap(Map map, String name) {
        Object v = map.get(name);
        if (v == null) return null;
        return (List<Map<String, Object>>) v;
    }
}
