package com.sheep.gamegroup.model.entity;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by ljy on 2018/3/16.
 */

public class BaseMessage implements Serializable{

    private String msg;
    private Integer code = 0;
    private Object data;
    private int total;
    private Object object;

    public Object getData() {
        return data == null ?"":data;
    }

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

    public String getMsg() {
        return msg == null ?"":msg;
    }

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

    public Integer getCode() {
        return code;
    }

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

    public Object getObject() {
        if(object == null){
            object = new Object();
        }
        return object;
    }

    public void setObject(Object object) {
        this.object = object;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getTotal() {
        return total;
    }

    /**
     * 失败返回null
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getData(Class<T> clazz) {
        if (data != null) {
            try {
                return JSONObject.parseObject(JSONObject.toJSONString(data), clazz);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    /**
     * 失败返回null
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> T getData(Class<T> clazz, T defaultValue) {
        if (data != null) {
            T t = null;
            try {
                t = JSONObject.parseObject(JSONObject.toJSONString(data), clazz);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(t == null){
                return defaultValue;
            }
        }
        return defaultValue;
    }

    /**
     * 如果data里是数组，直接这样获取
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> List<T> getDatas(Class<T> clazz) {
        if (data == null) {
            return null;
        }
        String json = JSONArray.toJSONString(data);
        return JSONArray.parseArray(json, clazz);
    }
    /**
     * 如果data里是数组，直接这样获取
     *
     * @param clazz
     * @param <T>
     * @return
     */
    public <T> ArrayList<T> getDataList(Class<T> clazz) {
        ArrayList<T> newList = ListUtil.emptyList();
        ListUtil.addAll(newList, getDatas(clazz));
        return newList;
    }

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

    public String getErrorMsg(){
        return TextUtils.isEmpty(msg) ? SheepApp.getInstance().getString(R.string.unknown_error) : msg;
    }
}
