package com.yonge.cooleshow.common.entity;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yonge.toolset.base.result.BaseResult;
import com.yonge.toolset.utils.json.JsonUtil;
import org.springframework.http.HttpStatus;

/**
 * HttpResponse 返回结果
 */
public class HttpResponseResult<T> extends BaseResult<T> {

    public HttpResponseResult(boolean status, int code, T data, String message) {
        super(status, code, data, message);
    }

    public HttpResponseResult(boolean status, HttpStatus resultCode, T data, String message) {
        super(status, resultCode, data, message);

    }

    public HttpResponseResult() {
    }

    public static <T> HttpResponseResult<T> succeed(T object) {
        return getResponseData(true, HttpStatus.OK, object, "");
    }

    public static <T> HttpResponseResult<T> succeed() {
        return getResponseData(true, HttpStatus.OK, null, "");
    }

    public static <T> HttpResponseResult<T> succeed(T object, String message) {
        return getResponseData(true, HttpStatus.OK, object, message);
    }

    public static <T> HttpResponseResult<T> warned(String message) {
        return failed(HttpStatus.MULTI_STATUS, message);
    }

    public static <T> HttpResponseResult<T> failed() {
        return failed("");
    }

    public static <T> HttpResponseResult<T> failed(String msg) {
        return failed(HttpStatus.INTERNAL_SERVER_ERROR, msg);
    }

    public static <T> HttpResponseResult<T> failed(HttpStatus statusCode, String msg) {
        return getResponseData(false, statusCode, null, msg);
    }

    public static <T> HttpResponseResult<T> status(boolean flag) {
        return flag ? succeed() : failed("操作失败");
    }

    public static <T> HttpResponseResult<T> status(BaseResult<T> baseResult) {
        HttpResponseResult<T> obj = new HttpResponseResult<T>();
        obj.setStatus(baseResult.getStatus());
        obj.setCode(baseResult.getCode());
        obj.setData(baseResult.getData());
        obj.setMsg(baseResult.getMsg());
        return obj;
    }

    public static <T> HttpResponseResult<T> failed(HttpStatus statusCode, T data, String msg) {
        return getResponseData(false, statusCode, data, msg);
    }

    public static <T> HttpResponseResult<T> failed(int code, T data, String msg) {
        return new HttpResponseResult<>(false, code, data, msg);
    }

    private static <T> HttpResponseResult<T> getResponseData(boolean status, HttpStatus statusCode, T data, String message) {
        HttpResponseResult<T> obj = new HttpResponseResult<T>();
        obj.setStatus(status);
        obj.setCode(statusCode.value());
        obj.setData(data);
        obj.setMsg(message);
        return obj;
    }

    @Override
    public String toString() {
        return JsonUtil.toJSONString(this, SerializerFeature.PrettyFormat);
    }
}
