package cn.reghao.tnb.common.web;

import cn.reghao.jutil.jdk.http.HeaderNames;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.jutil.jdk.web.result.ResultStatus;
import cn.reghao.jutil.jdk.serializer.JsonConverter;

/**
 * HTTP 响应 body 数据
 *
 * @author reghao
 * @date 2022-01-07 14:05:35
 */
public class WebResult<T> {
    private final int code;
    private final String msg;
    private final long timestamp;
    private final String requestId;
    private T data;

    private WebResult(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
        this.timestamp = System.currentTimeMillis();
        this.requestId = ServletUtil.getHeader(HeaderNames.XRequestId);
    }

    private WebResult(ResultStatus resultStatus) {
        this.code = resultStatus.getCode();
        this.msg = resultStatus.getMsg();
        this.timestamp = System.currentTimeMillis();
        this.requestId = ServletUtil.getHeader(HeaderNames.XRequestId);
    }

    private WebResult(String msg) {
        this.code = ResultStatus.SUCCESS.getCode();
        this.msg = msg;
        this.timestamp = System.currentTimeMillis();
        this.requestId = "";
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public String getRequestId() {
        return requestId;
    }

    private void setData(T data) {
        this.data = data;
    }

    public T getData() {
        return data;
    }

    public static <T> String result(Result result) {
        WebResult<T> webBody = new WebResult<>(result.getCode(), result.getMsg());
        return JsonConverter.objectToJson(webBody);
    }

    public static <T> String success() {
        WebResult<T> webBody = new WebResult<>(ResultStatus.SUCCESS.getCode(), ResultStatus.SUCCESS.getMsg());
        return JsonConverter.objectToJson(webBody);
    }

    public static <T> String successWithMsg(String msg) {
        WebResult<T> webBody = new WebResult<>(ResultStatus.SUCCESS.getCode(), msg);
        return JsonConverter.objectToJson(webBody);
    }

    public static <T> String success(T data) {
        WebResult<T> webBody = new WebResult<>(ResultStatus.SUCCESS.getCode(), ResultStatus.SUCCESS.getMsg());
        webBody.setData(data);
        return JsonConverter.objectToJson(webBody);
    }

    public static <T> String fail() {
        WebResult<T> webBody = new WebResult<>(ResultStatus.FAIL.getCode(), ResultStatus.FAIL.getMsg());
        return JsonConverter.objectToJson(webBody);
    }

    public static <T> String failWithMsg(String msg) {
        WebResult<T> webBody = new WebResult<>(ResultStatus.FAIL.getCode(), msg);
        return JsonConverter.objectToJson(webBody);
    }

    public static <T> String notFound() {
        WebResult<T> webBody = new WebResult<>(ResultStatus.NOTFOUND);
        return JsonConverter.objectToJson(webBody);
    }

    public static <T> String error() {
        WebResult<T> webBody = new WebResult<>(ResultStatus.ERROR.getCode(), ResultStatus.ERROR.getMsg());
        return JsonConverter.objectToJson(webBody);
    }

    public static <T> String errorWithMsg(String msg) {
        WebResult<T> webBody = new WebResult<>(ResultStatus.ERROR.getCode(), msg);
        return JsonConverter.objectToJson(webBody);
    }

    public static <T> String fallback(String msg) {
        WebResult<T> webBody = new WebResult<>(msg);
        return JsonConverter.objectToJson(webBody);
    }
}
