package com.yami.shop.common.util;

/**
 *
 * @author fubojin
 * @version 1.0.0
 * @since 2025-08-26
 */

import com.fasterxml.jackson.annotation.JsonInclude;
import com.yami.shop.common.enums.ResponseEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * <p>统一响应结果</p>
 *
 * @param <T> 任意泛型
 * @author fubojin
 * @time 2025-08-26
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class R<T> {

    int code;

    T data;

    String msg;

    boolean success;


    public static <T> R<T> SUCCESS() {
        return new R<>(ResponseEnum.SUCCESS.getCode(), null, ResponseEnum.SUCCESS.getValue(), Boolean.TRUE);
    }

    public static <T> R<T> SUCCESS(T param) {
        int code = ResponseEnum.SUCCESS.getCode();
        String value = ResponseEnum.SUCCESS.getValue();
        try {
            if (param.getClass().isEnum()) {
                IBasicInfo basicInfo = (IBasicInfo) param;
                code = basicInfo.getCode();
                value = basicInfo.getValue();
                return new R<>(code, null, value, Boolean.TRUE);
            }
        } catch (NullPointerException e) {
            return new R<>(code, null, value, Boolean.TRUE);
        }
        return new R<>(code, param, value, Boolean.TRUE);
    }

    public static <T> R<T> SUCCESS(T param, String msg) {
        return new R<>(ResponseEnum.SUCCESS.getCode(), param, msg, Boolean.TRUE);
    }

    public static <T> R<T> SUCCESS(Integer code, T param, String msg) {
        return new R<>(code, param, msg, Boolean.TRUE);
    }

    public static <T> R<T> FAIL() {
        return new R<>(ResponseEnum.ERROR.getCode(), null, ResponseEnum.ERROR.getValue(), Boolean.FALSE);
    }

    public static <T> R<T> FAIL(T param) {
        int code = ResponseEnum.ERROR.getCode();
        String value = ResponseEnum.ERROR.getValue();
        try {
            if (param.getClass().isEnum()) {
                IBasicInfo basicInfo = (IBasicInfo) param;
                code = basicInfo.getCode();
                value = basicInfo.getValue();
                return new R<>(code, null, value, Boolean.FALSE);
            }
        } catch (NullPointerException e) {
            return new R<>(code, null, value, Boolean.FALSE);
        }
        return new R<>(code, param, value, Boolean.FALSE);
    }

    public static <T> R<T> FAIL(String msg) {
        return new R<>(ResponseEnum.ERROR.getCode(), null, msg, Boolean.FALSE);
    }

    public static <T> R<T> FAIL(int code) {
        return new R<>(code, null, ResponseEnum.ERROR.getValue(), Boolean.FALSE);
    }

    public static <T> R<T> FAIL(T param, String msg) {
        return new R<>(ResponseEnum.ERROR.getCode(), param, msg, Boolean.FALSE);
    }


    public static <T> R<T> FAIL(int code, String msg) {
        return new R<>(code, null, msg, Boolean.FALSE);
    }

    public static <T> R<T> FAIL(int code, T param, String msg) {
        return new R<>(code, param, msg, Boolean.FALSE);
    }

}
