package com.yonge.cooleshow.common.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.yonge.cooleshow.common.exception.BizException;
import com.yonge.toolset.base.exception.ThirdpartyException;
import com.yonge.toolset.utils.http.HttpUtil;
import com.yonge.toolset.utils.json.JsonUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.http.HttpStatus;
import java.util.Date;

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

	private boolean status = true;
	private String msg;
	private T data;
	private int code;
	private Date nowTime = new Date();

	public HttpResponseResult(boolean status, int code, T data, String message) {
		this.status = status;
		this.msg = message;
		this.data = data;
		this.code = code;
	}

	public HttpResponseResult(boolean status, HttpStatus resultCode, T data, String message) {
		this.status = status;
		this.msg = message;
		this.data = data;
		this.code = resultCode.value();
	}

	public HttpResponseResult() {
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public boolean getStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

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

	public void success() {
		this.status = true;
	}

	public void fail() {
		this.status = false;
	}

	public Date getNowTime() {
		return nowTime;
	}

	public void setNowTime(Date nowTime) {
		this.nowTime = nowTime;
	}


	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> succeedData(T obj) {
		return getResponseData(true, HttpStatus.OK, obj, "操作成功");
	}

	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> 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);
    }
}
