package com.qqchen.deploy.backend.framework.api;

import com.qqchen.deploy.backend.framework.enums.ResponseCode;
import com.qqchen.deploy.backend.framework.utils.MessageUtils;
import lombok.Data;

import java.io.Serializable;


@Data
public class Response<T> implements Serializable {

    private boolean success;

    private String message;

    private T data;

    private Integer code;

    public static <T> Response<T> success() {
        Response<T> response = new Response<>();
        response.setSuccess(true);
        response.setCode(ResponseCode.SUCCESS.getCode());
        response.setMessage(MessageUtils.getMessage(ResponseCode.SUCCESS.getMessageKey()));
        return response;
    }

    public static <T> Response<T> success(T data) {
        Response<T> response = new Response<>();
        response.setSuccess(true);
        response.setCode(ResponseCode.SUCCESS.getCode());
        response.setMessage(MessageUtils.getMessage(ResponseCode.SUCCESS.getMessageKey()));
        response.setData(data);
        return response;
    }

    public static <T> Response<T> error(ResponseCode responseCode) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setCode(responseCode.getCode());
        response.setMessage(MessageUtils.getMessage(responseCode.getMessageKey()));
        return response;
    }

    public static <T> Response<T> error(ResponseCode responseCode, Object... args) {
        Response<T> response = new Response<>();
        response.setSuccess(false);
        response.setCode(responseCode.getCode());
        response.setMessage(MessageUtils.getMessage(responseCode.getMessageKey(), args));
        return response;
    }
}