package com.bc_chat;

import android.os.Parcel;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;
import io.rong.imlib.model.UserInfo;

/**
 * author：Created by zhaohaoting on 2019/03/03
 * email：526309416@qq.com
 * desc：
 */
@MessageTag(flag = MessageTag.NONE, value = "RC:GroupAndP2PMessegeTipPush")
public class TipsMessage extends MessageContent {
    private String uid;
    private String type;
    private String content;

    public static final Creator<TipsMessage> CREATOR = new Creator() {
        public TipsMessage createFromParcel(Parcel var1) {
            return new TipsMessage(var1);
        }

        public TipsMessage[] newArray(int var1) {
            return new TipsMessage[var1];
        }
    };

    public byte[] encode() {
        JSONObject var1 = new JSONObject();

        try {
            if (!TextUtils.isEmpty(this.getUid())) {
                var1.put("uid", this.uid);
            }

            if (!TextUtils.isEmpty(this.getType())) {
                var1.put("type", this.type);
            }
            if (!TextUtils.isEmpty(this.getContent())) {
                var1.put("content", this.content);
            }

            if (this.getJSONUserInfo() != null) {
                var1.putOpt("bribery", this.getJSONUserInfo());
            }
        } catch (JSONException var4) {
            var4.printStackTrace();
        }

        try {
            return var1.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException var3) {
            var3.printStackTrace();
            return null;
        }
    }

    protected TipsMessage() {
    }

    public static TipsMessage obtain(String uid, String type, String content) {
        TipsMessage var4 = new TipsMessage();
        var4.setUid(uid);
        var4.setType(type);
        var4.setContent(content);
        return var4;
    }

    public TipsMessage(byte[] var1) {
        String var2 = null;

        try {
            var2 = new String(var1, "UTF-8");
        } catch (UnsupportedEncodingException var5) {
        }

        try {
            JSONObject var3 = new JSONObject(var2);
            if (var3.has("uid")) {
                this.setUid(var3.optString("uid"));
            }

            if (var3.has("type")) {
                this.setType(var3.optString("type"));
            }

            if (var3.has("content")) {
                this.setContent(var3.optString("content"));
            }


            if (var3.has("bribery")) {
                this.setUserInfo(this.parseJsonToUserInfo(var3.getJSONObject("bribery")));
            }
        } catch (JSONException var4) {
        }

    }

    public int describeContents() {
        return 0;
    }

    public void writeToParcel(Parcel var1, int var2) {
        ParcelUtils.writeToParcel(var1, this.uid);
        ParcelUtils.writeToParcel(var1, this.type);
        ParcelUtils.writeToParcel(var1, this.content);
        ParcelUtils.writeToParcel(var1, this.getUserInfo());
    }

    public TipsMessage(Parcel var1) {
        this.setUid(ParcelUtils.readFromParcel(var1));
        this.setType(ParcelUtils.readFromParcel(var1));
        this.setContent(ParcelUtils.readFromParcel(var1));
        this.setUserInfo((UserInfo) ParcelUtils.readFromParcel(var1, UserInfo.class));
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

