package com.market.entity;


import com.alibaba.fastjson.JSONObject;
import com.market.enums.MessageCode;

public class MessageResult<T> {
	private T data;
	public MessageResult(int code , String msg){
		this.code = code;
		this.message = msg;
	}
	public MessageResult(int code , String msg, T object){
		this.code = code;
		this.message = msg;
		this.data = object;
	}
	public MessageResult() {
		// TODO Auto-generated constructor stub
	}
	
	public MessageResult(JSONObject json) {
		this.code = json.getInteger("code");
		this.message = json.getString("message");
		if(json.containsKey("data"))
			this.setData((T)json.get("data"));
	}
	
	public static MessageResult success(){
		return new MessageResult(0, MessageCode.SUCCESS.getMessage(null));
	}
	public static MessageResult success(String msg){
		return new MessageResult(0,msg);
	}
	public static MessageResult success(String msg,Object data){
		return new MessageResult(0,msg,data);
	}

	/** 与 spot 一致：按请求头 lang 多语言成功文案 */
	public static MessageResult successLang(String lang) {
		return new MessageResult(0, MessageCode.SUCCESS.getMessage(lang));
	}

	public static MessageResult successLang(String lang, Object data) {
		return new MessageResult(0, MessageCode.SUCCESS.getMessage(lang), data);
	}

	public static MessageResult failLang(String lang, MessageCode code) {
		return new MessageResult(code.getCode(), code.getMessage(lang));
	}

	public static MessageResult failLang(String lang, MessageCode code, String message) {
		String msg = (message != null && !message.isEmpty()) ? message : code.getMessage(lang);
		return new MessageResult(code.getCode(), msg);
	}
	
	public static MessageResult error(int code,String msg){
		return new MessageResult(code,msg);
	}
	public static MessageResult error(String msg){
		return new MessageResult(500,msg);
	}
	
	private int code;
	private String message;
	
	private String totalPage;
	private String totalElement;

    public String getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(String totalPage) {
        this.totalPage = totalPage;
    }

    public String getTotalElement() {
        return totalElement;
    }

    public void setTotalElement(String totalElement) {
        this.totalElement = totalElement;
    }

    public int getCode() {
		return code;
	}
	public void setCode(int code) {
		this.code = code;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	@Override
    public String toString(){
		return JSONObject.toJSONString(this);
		//return "{\"code\":"+code+",\"message\":\""+message+"\"}";
	}
	public Object getData() {
		return data;
	}
	public void setData(T data) {
		this.data = data;
	}

	public static MessageResult getSuccessInstance(String message ,Object data){
		MessageResult result = success(message);
		result.setData(data);
		return result ;
	}
}
