package com.muyu.common.core.domain;

import com.muyu.common.core.constant.Constants;
import com.muyu.common.core.constant.HttpStatus;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author muyu
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {
    /**
     * 成功
     */
    public static final int SUCCESS = Constants.SUCCESS;
    /**
     * 失败
     */
    public static final int FAIL = Constants.FAIL;
    /**
     * 警告
     */
    public static final int WARN = HttpStatus.WARN;

    private static final long serialVersionUID = 1L;
    private int code;

    private String msg;

    private T data;

    public static <T> Result<T> success () {
        return restResult(null, SUCCESS, null);
    }

    public static <T> Result<T> success (T data) {
        return restResult(data, SUCCESS, null);
    }

    public static <T> Result<T> success (T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> Result<T> error () {
        return restResult(null, FAIL, null);
    }

    public static <T> Result<T> error (String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> Result<T> error (T data) {
        return restResult(data, FAIL, null);
    }

    public static <T> Result<T> error (T data, String msg) {
        return restResult(data, FAIL, msg);
    }

    public static <T> Result<T> error (int code, String msg) {
        return restResult(null, code, msg);
    }



    public static <T> Result<T> warn () {
        return restResult(null, WARN, null);
    }

    public static <T> Result<T> warn (String msg) {
        return restResult(null, WARN, msg);
    }

    public static <T> Result<T> warn (T data) {
        return restResult(data, WARN, null);
    }

    public static <T> Result<T> warn (T data, String msg) {
        return restResult(data, WARN, msg);
    }

    public static <T> Result<T> warn (int code, String msg) {
        return restResult(null, code, msg);
    }

    private static <T> Result<T> restResult (T data, int code, String msg) {
        return Result.<T>builder()
                .code(code)
                .data(data)
                .msg(msg)
                .build();
    }

    public static <T> Boolean isError (Result<T> ret) {
        return !isSuccess(ret);
    }

    public static <T> Boolean isSuccess (Result<T> ret) {
        return Result.SUCCESS == ret.getCode();
    }

}
