package com.njyunzhi.pet_identity.common;

import java.io.Serializable;

/**
 * 接口统一状态返回BEAN.
 */
public class ResponseBean<T> implements Serializable {

    private static final long serialVersionUID = 3593827217136880822L;

    public static final int CODE_SUCCESS = 1000;
    public static final int ERROR_AUTH_FAIL = 1001;
    public static final int ERROR_UNAUTHORIZED = 1002;
    public static final int ERROR_MISSING_PARAMS = 1003;
    public static final int ERROR_ILLEGAL_PARAMS = 1004;
    public static final int ERROR_UNAVAILABLE = 1005;

    private int code;
    private String message;
    private T data;

    public ResponseBean() {
        code = ResponseBean.CODE_SUCCESS;
    }

    public static <T> ResponseBean success(T data, String ...msgs) {
        ResponseBean responseBean = new ResponseBean();
        responseBean.code = ResponseBean.CODE_SUCCESS;
        responseBean.data = data;

        if (null != msgs && msgs.length > 0) {
            responseBean.message = msgs[0];
        }

        return responseBean;
    }


    public static <T> ResponseBean error(String msg) {
        ResponseBean responseBean = new ResponseBean();
        responseBean.code = ERROR_ILLEGAL_PARAMS;
        responseBean.message = msg;

        return responseBean;
    }

    public static <T> ResponseBean error(String msg, int code, T ...datas) {
        ResponseBean responseBean = new ResponseBean();
        responseBean.code = code;
        responseBean.message = msg;

        if (null != datas && datas.length > 0) {
            responseBean.data = datas[0];
        }

        return responseBean;
    }

    public int getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }
    
    @Override
    public String toString() {
        return  "{ code: "+code+", message: "+message+", data: <T> }";
    }
}
