package com.bcrjl.miwifi.common.response;

import com.bcrjl.miwifi.common.enums.GlobalEnumCode;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.Optional;

/**
 * 统一API响应结果封装
 *
 * @author yanqs
 */
@Data
@Accessors(chain = true)
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

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

    private R(int code, T data, String message) {
        this.code = code;
        this.data = data;
        this.message = message;
        this.success = GlobalEnumCode.SUCCESS.getCode() == code;
    }

    /**
     * 判断返回是否为成功
     *
     * @param result Result
     * @return 是否成功
     */
    public static boolean isSuccess(@Nullable R<?> result) {
        return Optional.ofNullable(result)
                .map(x -> ObjectUtils.nullSafeEquals(GlobalEnumCode.SUCCESS.getCode(), x.code))
                .orElse(Boolean.FALSE);
    }

    /**
     * 判断返回是否为成功
     *
     * @param result Result
     * @return 是否成功
     */
    public static boolean isNotSuccess(@Nullable R<?> result) {
        return !R.isSuccess(result);
    }

    /**
     * 返回R
     *
     * @param data 数据
     * @param <T>  T 泛型标记
     * @return R
     */
    public static <T> R<T> data(T data) {
        return data(data, GlobalEnumCode.SUCCESS.getMsg());
    }

    /**
     * 返回R
     *
     * @param data    数据
     * @param message 消息
     * @param <T>     T 泛型标记
     * @return R
     */
    public static <T> R<T> data(T data, String message) {
        return data(HttpServletResponse.SC_OK, data, message);
    }

    /**
     * 返回R
     *
     * @param code    状态码
     * @param data    数据
     * @param message 消息
     * @param <T>     T 泛型标记
     * @return R
     */
    public static <T> R<T> data(int code, T data, String message) {
        return new R<>(code, data, data == null ? "暂无承载数据" : message);
    }

    @SuppressWarnings("unchecked")
    public static <T> R<T> success() {
        return new R(GlobalEnumCode.SUCCESS.getCode(), null, GlobalEnumCode.SUCCESS.getMsg());
    }

    @SuppressWarnings("unchecked")
    public static <T> R<T> success(T data) {
        return new R(GlobalEnumCode.SUCCESS.getCode(), data, GlobalEnumCode.SUCCESS.getMsg());
    }

    /**
     * 返回R
     *
     * @param message 消息
     * @param <T>     T 泛型标记
     * @return R
     */
    @SuppressWarnings("unchecked")
    public static <T> R<T> success(T data, String message) {
        return new R(GlobalEnumCode.SUCCESS.getCode(), data, message);
    }


    public static <T> R<T> fail() {
        return R.fail(GlobalEnumCode.FAIL.getCode(), GlobalEnumCode.FAIL.getMsg());
    }

    /**
     * 返回R
     *
     * @param message 消息
     * @param <T>     T 泛型标记
     * @return R
     */
    public static <T> R<T> fail(String message) {
        return R.fail(GlobalEnumCode.FAIL.getCode(), message);
    }


    /**
     * 返回R
     *
     * @param code    状态码
     * @param message 消息
     * @param <T>     T 泛型标记
     * @return R
     */
    public static <T> R<T> fail(int code, String message) {
        return new R<>(code, null, message);
    }

    /**
     * 返回R
     *
     * @param flag 成功状态
     * @return R
     */
    public static <T> R<T> status(boolean flag) {
        return flag ? success() : fail();
    }
}
