package com.yonge.cooleshow.common.entity;

import com.yonge.cooleshow.common.enums.ErrorEnum;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import static com.yonge.cooleshow.common.enums.ErrorEnum.ERR_SUCCESS;

@Data
public class BaseResponse<T> {
    private @Getter int errCode;
    private @Setter @Getter String errMsg;
    private @Setter @Getter String errDetail;
    private @Getter BaseResponseResult data;


    public void setData(T data) {
        this.data = new BaseResponseResult<T>(data);
    }

    public static <T> BaseResponse<T> success(T data) {
        BaseResponse<T> response = success();
        response.setData(data);
        return response;
    }

    public static <T> BaseResponse<T> success() {
        BaseResponse<T> response = new BaseResponse();
        response.setErr(ERR_SUCCESS);
        return response;
    }

    public static <T> BaseResponse<T> failed(ErrorEnum err,T data) {
        BaseResponse<T> response = failed(err);
        response.setData(data);
        return response;
    }

    public static <T> BaseResponse<T> failed(ErrorEnum err) {
        BaseResponse<T> response = new BaseResponse();
        response.setErr(err);
        return response;
    }

    public void setErr(ErrorEnum error) {
        this.errCode = error.getErrCode();
        this.errMsg = error.getErrMsg();
        this.errDetail = error.getErrMsg();
    }

    class BaseResponseResult<R> {
        private @Setter @Getter R result;

        BaseResponseResult(R result) {
            this.result = result;
        }
    }
}
