package net.geedge.common;

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

/**
 * 返回数据
 * <p>
 * 错误码、错误内容统一在枚举类RCode中定义， 错误码格式见RCode注释，错误码内容必须用英文，作为国际化的code 自定义的错误类型必须加注释
 */
public class R extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    public R() {
        put("code", RCode.SUCCESS.getCode());
        put("msg", RCode.SUCCESS.getMsg());
        put("timestamp", T.DateUtil.current());
    }

    public static R ok() {
        return new R();
    }

    public static R ok(String msg) {
        R r = new R();
        r.put("msg", msg);
        return r;
    }

    public static R ok(Object data) {
        R r = new R();
        r.put("data", data);
        return r;
    }

    public static R error() {
        return error(RCode.ERROR.getCode(), RCode.ERROR.getMsg());
    }

    public static R error(RCode rCode) {
        R r = new R();
        r.put("code", rCode.getCode());
        r.put("msg", rCode.getMsg());
        return r;
    }

    public static R error(String msg) {
        R r = new R();
        r.put("code", RCode.ERROR.getCode());
        r.put("msg", msg);
        return r;
    }

    public static R error(Integer code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    @Override
    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public R putData(Object value) {
        this.put("data", value);
        return this;
    }

    @SuppressWarnings("unchecked")
    public R putData(String key, Object value) {
        Object data = super.getOrDefault("data", new LinkedHashMap<String, Object>());
        if (!(data instanceof Map)) {
            throw new APIException("data put error");
        }
        ((Map<String, Object>) data).put(key, value);
        super.put("data", data);
        return this;
    }

    @SuppressWarnings("all")
    public R putAllData(Map m) {
        super.putAll(m);
        return this;
    }
}