package cc.smtweb.framework.core.common;

import cc.smtweb.framework.core.exception.SwException;
import cc.smtweb.framework.core.util.MapUtil;

import java.util.*;
import java.util.function.Function;

/**
 * 通用map对象，用于无具体类型的传值
 *
 * @author kevin
 */
public class SwMap extends HashMap<String, Object> {
    public SwMap() {
    }

    public SwMap(int initialCapacity) {
        super(initialCapacity);
    }

    public String readString(String name) {
        return MapUtil.readString(this, name);
    }

    public SwMap readMap(String name) {
        Object v = get(name);
        if (v == null) return null;
        if (v instanceof SwMap) return (SwMap) v;
        if (v instanceof Map) {
            Map m = (Map) v;
            SwMap map = new SwMap(m.size());
            map.putAll(m);
            put(name, map);
            return map;
        }
        throw new SwException("不是合法的Map对象！" + v.getClass().getName());
    }

    public SwMap computeMapIfAbsent(String key, Function<? super String, ?> mappingFunction) {
        super.computeIfAbsent(key, mappingFunction);
        return readMap(key);
    }

    public SwMap readFilter() {
        return readMap("filter");
    }

    public List<Map<String, Object>> readListMap(String name) {
        return MapUtil.readListMap(this, name);
    }

    public String readString(String name, String defaultValue) {
        return MapUtil.readString(this, name, defaultValue);
    }

    public long readLong(String name) {
        return MapUtil.readLong(this, name);
    }

    public Long readLong(String name, Long defaultValue) {
        return MapUtil.readLong(this, name, defaultValue);
    }

    public Long[] readLongArray(String name) {
        return MapUtil.readLongArray(this, name);
    }

    public Long[] readLongArray(String name, Long[] defaultValue) {
        return MapUtil.readLongArray(this, name, defaultValue);
    }

    public Set<Long> readLongSet(String name) {
        return MapUtil.readLongSet(this, name);
    }

    public List<String> readStringList(String name) {
        return MapUtil.readStringList(this, name);
    }

    public Set<String> readStringSet(String name) {
        List<String> list = readStringList(name);
        if (list == null) return null;
        return new HashSet<String>(list);
    }

    public int readInt(String name) {
        return MapUtil.readInt(this, name);
    }

    public int readInt(String name, Integer defaultValue) {
        return MapUtil.readInt(this, name, defaultValue);
    }

    public float readFloat(String name) {
        return MapUtil.readFloat(this, name);
    }

    public float readFloat(String name, Float defaultValue) {
        return MapUtil.readFloat(this, name, defaultValue);
    }

    public double readDouble(String name) {
        return MapUtil.readDouble(this, name);
    }

    public double readDouble(String name, Double defaultValue) {
        return MapUtil.readDouble(this, name, defaultValue);
    }

    public boolean readBool(String name) {
        return MapUtil.readBool(this, name);
    }

    public boolean readBool(String name, Boolean defaultValue) {
        return MapUtil.readBool(this, name, defaultValue);
    }

    @Override
    public SwMap put(String name, Object value) {
        if (value != null) {
            super.put(name, value);
        } else {
            super.remove(name);
        }

        return this;
    }

    public static SwMap of(String name, Object value) {
        return new SwMap().put(name, value);
    }
}
