package com.iamberry.app.config;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

public class Response implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1497535410510537697L;

	private static final ObjectMapper mapper = new ObjectMapper();
	private ResponseHeader header;
	private Object data;

	public Response(ResponseHeader header) {
		this(header, null);
	}

	public Response(ResponseHeader header, Object data) {
		super();
		this.header = header;
		this.data = data;
	}

	public ResponseHeader getHeader() {
		return header;
	}

	public Response setHeader(ResponseHeader header) {
		this.header = header;
		return this;
	}

	public Object getData() {
		return data;
	}

	public Response setData(Object data) {
		this.data = data;
		return this;
	}

	public Response setData(Object data, int result_count) {
		this.data = data;
		this.header.setResult_count(result_count);
		return this;
	}

	@Override
	public String toString() {
		try {
			return mapper.writeValueAsString(this);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	
	/** 
	* @Description: 将对象转为Map。注意方法名不能为getXxx，否则在将对象进行Json转换时，该get方法也会被默认调用。 
	* @param @return  
	* @return Map<String,Object>   
	* @throws 
	*/
	public Map<String, Object> toMap() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("header", header);
		map.put("data", data);
		return map;
	}

	public static final Response AUTHENTICATION_ERROR = new Response(new ResponseHeader(APIStatus.AUTHENTICATION_ERROR));
	public static final Response SERVER_INTERNAL_ERROR = new Response(new ResponseHeader(APIStatus.INTERNAL_SERVER_ERROR));
	public static final Response INVALID_PARMS = new Response(new ResponseHeader(APIStatus.INVALID_PARAMETER));
	public static final Response ERROR_REGISTER = new Response(new ResponseHeader(APIStatus.ERROR_REGISTER));
	public static final Response INSERT_DUPLICATE = new Response(new ResponseHeader(APIStatus.INSERT_DUPLICATE));
	public static final Response SUCCESS = new Response(new ResponseHeader(APIStatus.SUCCESS));
	public static final Response FAILURE = new Response(new ResponseHeader(APIStatus.FAILURE));
	public static final Response ERROR_CODE = new Response(new ResponseHeader(APIStatus.ERROR_CODE));
	public static final Response CODE_TIMEOUT = new Response(new ResponseHeader(APIStatus.CODE_TIMEOUT));
	public static final Response NOT_FOUND = new Response(new ResponseHeader(APIStatus.NOT_FOUND));
	public static final Response INVALID_TOKEN = new Response(new ResponseHeader(APIStatus.INVALID_TOKEN));
	public static final Response ERROR_OLDPASSWORD = new Response(new ResponseHeader(APIStatus.ERROR_OLDPASSWORD));
	public static final Response WARN_CART_FULL = new Response(new ResponseHeader(APIStatus.WARN_CART_FULL));
	public static final Response USER_NOT_EXIST = new Response(new ResponseHeader(APIStatus.USER_NOT_EXIST));
	public static final Response USER_FROZEN_ERROR = new Response(new ResponseHeader(APIStatus.USER_FROZEN_ERROR));
	public static final Response POINTS_NOT_AVALIABLE = new Response(new ResponseHeader(APIStatus.POINTS_NOT_AVALIABLE));
	public static final Response AMOUNT_NOT_AVALIABLE = new Response(new ResponseHeader(APIStatus.AMOUNT_NOT_AVALIABLE));
	public static final Response BARCODE_NOT_FOUND = new Response(new ResponseHeader(APIStatus.BARCODE_NOT_FOUND));
}
