package com.yutou.bilibili.datas;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import lombok.Data;

import java.util.Collection;
import java.util.List;

@Data
public class ResultData<T> {
    private int status;
    private String message;
    private T data;
    private int count;
    private long timestamp;

    public ResultData() {
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> JSONObject success(T data) {
        return success(data, -1);
    }
    public static <T> JSONObject success(T data, int count) {
        return success(data, count, ReturnCode.RC100);
    }

    public static <T> JSONObject success(T data, int count,ReturnCode returnCode) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(returnCode.getCode());
        resultData.setMessage(returnCode.getMessage());
        resultData.setData(data);
        if (data instanceof Collection<?>) {
            resultData.count = ((Collection<?>) data).size();
        }
        if (count != -1) {
            resultData.count = count;
        }
        return JSONObject.parseObject(JSONObject.toJSONString(resultData));
    }

    public static <T> JSONObject fail(int code, String message) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(code);
        resultData.setMessage(message);
        return JSONObject.parseObject(JSONObject.toJSONString(resultData));
    }

    public static JSONObject success(ReturnCode code) {
        return fail(code);
    }

    public static <T> JSONObject fail(ReturnCode code) {
        ResultData<T> resultData = new ResultData<>();
        resultData.setStatus(code.getCode());
        resultData.setMessage(code.getMessage());
        return JSONObject.parseObject(JSONObject.toJSONString(resultData));
    }

}
