package cn.cslg.pas.common.utils;

public class Response {

    private Integer code;
    private Object data;
    private String message;

    public static String success() {
        Response response = new Response();
        response.setResultEnum(ResponseEnum.SUCCESS);
        return JsonUtils.objectToJson(response);
    }

    public static String success(Object data) {
        Response response = new Response();
        response.setResultEnum(ResponseEnum.SUCCESS);
        response.setData(data);
        return JsonUtils.objectToJson(response);
    }

    public static String successBut(Object data) {
        Response response = new Response();
        response.setResultEnum(ResponseEnum.SUCCESSBUT);
        response.setData(data);
        return JsonUtils.objectToJson(response);
    }

    /**
     * 陈宇 ↓ 直接返回对象(前台默认显示为jsonResult格式对象)
     */
    public static Response ok(Object data) {
        Response response = new Response();
        response.setResultEnum(ResponseEnum.SUCCESS);
        response.setData(data);
        return response;
    }

    public static Response fail() {
        Response response = new Response();
        response.setResultEnum(ResponseEnum.ERROR);
        return response;
    }

    public static Response fail(Object data) {
        Response response = new Response();
        response.setCode(ResponseEnum.ERROR.getCode());
        response.setMessage(data + "");
        response.setData(data);
        return response;
    }

    public static String websocket(Object data, ResponseEnum responseEnum) {
        Response response = new Response();
        response.setResultEnum(responseEnum);
        response.setData(data);
        return JsonUtils.objectToJson(response);
    }

    public static String error() {
        Response response = new Response();
        response.setData(Boolean.FALSE);
        response.setResultEnum(ResponseEnum.ERROR);
        return JsonUtils.objectToJson(response);
    }

    public static String error(String message) {
        Response response = new Response();
        response.setCode(ResponseEnum.ERROR.getCode());
        response.setData(Boolean.FALSE);
        response.setMessage(message);
        return JsonUtils.objectToJson(response);
    }

    public static String error(ResponseEnum responseEnum) {
        Response response = new Response();
        response.setResultEnum(responseEnum);
        return JsonUtils.objectToJson(response);
    }

    public Response(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Response() {
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    private void setResultEnum(ResponseEnum responseEnum) {
        this.code = responseEnum.getCode();
        this.message = responseEnum.getMessage();
    }
}
