package zmo.technology.lite.core;

import java.nio.charset.StandardCharsets;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

public class ZeroResponse<T> {

	private int code;
	private String message;
	private List<T> datas;
	private JSONObject expands;

	private ZeroResponse() {
		super();
	}

	public int getCode() {
		return code;
	}

	public ZeroResponse<T> setCode(int code) {
		this.code = code;
		return this;
	}

	public String getMessage() {
		return message;
	}

	public ZeroResponse<T> setMessage(String message) {
		this.message = message;
		return this;
	}

	public List<T> getDatas() {
		return datas;
	}

	public ZeroResponse<T> setDatas(List<T> datas) {
		this.datas = datas;
		return this;
	}

	public ZeroResponse<T> addData(T data) {
		this.datas.add(data);
		return this;
	}

	public ZeroResponse<T> removeData(int i) {
		this.datas.remove(i);
		return this;
	}

	public JSONObject getExpands() {
		return expands;
	}

	public ZeroResponse<T> setExpands(JSONObject expands) {
		this.expands = expands;
		return this;
	}

	public ZeroResponse<T> putExpands(String key, String value) {
		this.expands.put(key, value);
		return this;
	}

	public ZeroResponse<T> removeExpands(String key) {
		this.expands.remove(key);
		return this;
	}

	public String toJSONString() {
		JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
		return JSON.toJSONString(this, SerializerFeature.WriteDateUseDateFormat);
	}

	public static ZeroResponse<?> build() {
		return new ZeroResponse<>();
	}

	public static <T> ZeroResponse<T> build(Class<T> type) {
		return new ZeroResponse<T>();
	}

	public static <T> ZeroResponse<T> build(List<T> datas) {
		ZeroResponse<T> response = new ZeroResponse<T>();
		response.setDatas(datas);
		return response;
	}

	public static ZeroResponse<?> build(String json) {
		return JSONObject.parseObject(json, ZeroResponse.class);
	}

	@SuppressWarnings("unchecked")
	public static <T> ZeroResponse<T> build(Class<T> type, String json) {
		return (ZeroResponse<T>) build(json);
	}

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

	public byte[] toBytes() {
		return toJSONString().getBytes(StandardCharsets.UTF_8);
	}
}
