package top.imwork.commons.core.pojo;

import top.imwork.commons.core.enums.HttpStatusCodeEnum;

import java.io.Serial;
import java.util.HashMap;
import java.util.Map;

/**
 * Copyright (C), 2015-2021
 * FileName: ResponseMsg
 * Author(作者姓名):   stars
 * CreateDate(创建时间):     2021/8/2 15:00
 * UpdateDate(修改时间):     2021/8/2 15:00
 * Description(功能描述):    响应实体类
 * Since(版本号)  1.0.0
 * History:
 */
public class ResponseMsg extends HashMap<String, Object> {
    @Serial
    private static final long serialVersionUID = 1L;

    public ResponseMsg() {
        put("code", HttpStatusCodeEnum.OK.getCode());
        put("msg", HttpStatusCodeEnum.OK.getZmsg());
        put("timestamp", System.currentTimeMillis());
    }

    /**
     * 默认响应成功消息
     *
     * @return ResponseMsg
     */
    public static ResponseMsg ok() {
        return new ResponseMsg();
    }

    /**
     * 响应状态码消息
     *
     * @param code 状态码
     * @return ResponseMsg
     */
    public static ResponseMsg ok(HttpStatusCodeEnum code) {
        return buildResponse(code.getCode(), code.getZmsg());
    }

    /**
     * 响应自定义成功消息
     *
     * @param message 消息内容
     * @return ResponseMsg
     */
    public static ResponseMsg ok(String message) {
        return buildResponse(HttpStatusCodeEnum.OK.getCode(), message);
    }

    /**
     * 响应失败消息
     *
     * @return ResponseMsg
     */
    public static ResponseMsg fail() {
        return buildResponse(HttpStatusCodeEnum.BAD_REQUEST.getCode(), HttpStatusCodeEnum.BAD_REQUEST.getZmsg());
    }

    /**
     * 响应失败消息
     *
     * @return ResponseMsg
     */
    public static ResponseMsg fail(Object message) {
        return buildResponse(HttpStatusCodeEnum.BAD_REQUEST.getCode(), message, null);
    }

    /**
     * 响应错误消息
     *
     * @return ResponseMsg
     */
    public static ResponseMsg error() {
        return buildResponse(HttpStatusCodeEnum.INTERNAL_SERVER_ERROR.getCode(), HttpStatusCodeEnum.INTERNAL_SERVER_ERROR.getZmsg());
    }

    /**
     * 响应自定义错误消息
     *
     * @return ResponseMsg
     */
    public static ResponseMsg error(Object message) {
        return buildResponse(HttpStatusCodeEnum.INTERNAL_SERVER_ERROR.getCode(), message);
    }

    public static ResponseMsg unauthorized() {
        return buildResponse(HttpStatusCodeEnum.UNAUTHORIZED.getCode(), HttpStatusCodeEnum.UNAUTHORIZED.getZmsg());
    }

    /**
     * 响应自定义键-值消息
     *
     * @param key   键
     * @param value 值
     * @return ResponseMsg
     */
    @Override
    public ResponseMsg put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    /**
     * 响应自定义对象消息
     *
     * @param map 集合
     * @return ResponseMsg
     */
    public static ResponseMsg ok(Map<String, Object> map) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.putAll(map);
        return responseMsg;
    }

    /**
     * 构建返回对象，此为一般返回正常结果集， 一般为REST FUL API所用
     *
     * @param data 内容
     * @return ResponseMsg
     */
    public static ResponseMsg buildResponse(Object data) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.put("code", HttpStatusCodeEnum.OK.getCode());
        responseMsg.put("msg", HttpStatusCodeEnum.OK.getZmsg());
        responseMsg.put("data", data);
        return responseMsg;
    }

    /**
     * 自定义响应消息
     *
     * @param code    状态码
     * @param message 消息内容
     * @return ResponseMsg
     */
    public static ResponseMsg buildResponse(Integer code, Object message) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.put("code", code);
        responseMsg.put("msg", message);
        responseMsg.put("timestamp", System.currentTimeMillis());
        return responseMsg;
    }

    /**
     * 构建返回对象，此为一般返回正常结果集， 一般为REST FUL API所用
     *
     * @param data 内容
     * @return ResponseMsg
     */
    public static ResponseMsg buildResponse(Integer code, Object message, Object data) {
        ResponseMsg responseMsg = buildResponse(code, message);
        responseMsg.put("data", data);
        return responseMsg;
    }

    /**
     * 构建返回对象，此为一般返回正常结果集， 一般为REST FUL API所用
     *
     * @param data 内容
     * @return ResponseMsg
     */
    public static ResponseMsg buildResponse(Integer code, Object message, Object params, Object data) {
        ResponseMsg responseMsg = buildResponse(code, message);
        responseMsg.put("params", params);
        responseMsg.put("data", data);
        return responseMsg;
    }

    /**
     * 构建返回对象，此为一般返回正常结果集， 一般为REST FUL API所用
     *
     * @param data 内容
     * @return ResponseMsg
     */
    public static ResponseMsg buildResponse(Integer code, Object message, Object params, Object data, Object details) {
        ResponseMsg responseMsg = buildResponse(code, message, params, data);
        responseMsg.put("details", details);
        return responseMsg;
    }
}
