package com.rong.io.live.message;

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 by pq, Date on 2022/4/1.
 * 观众数量（web发送消息 学生端同步数据）
 */
@MessageTag(value = "RC:Chatroom:MemberCountUp", flag = MessageTag.STATUS)
public class RCChatRoomMemberNumMessage extends MessageContent {
    private String count;

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }

    public RCChatRoomMemberNumMessage() {
    }

    public RCChatRoomMemberNumMessage(byte[] data) {
        String jsonStr = null;
        try {
            jsonStr = new String(data, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            if (jsonObject.has("content")) {
                JSONObject content = (JSONObject) jsonObject.opt("content");
                if (content != null) {
                    count = content.optString("count");
                }
            }

            if (jsonObject.has("user")) {
                setUserInfo(this.parseJsonToUserInfo(jsonObject.getJSONObject("user")));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public byte[] encode() {
        JSONObject jsonObj = new JSONObject();
        try {
            // 消息携带用户信息时, 自定义消息需添加下面代码
            if (!TextUtils.isEmpty(count)) {
                jsonObj.putOpt("count", count);
            }
            JSONObject jsonUserInfo = this.getJSONUserInfo();
            if (jsonUserInfo != null) {
                jsonObj.putOpt("user", jsonUserInfo);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

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

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.count);
        ParcelUtils.writeToParcel(dest, getExtra());
        ParcelUtils.writeToParcel(dest, getUserInfo());
    }

    protected RCChatRoomMemberNumMessage(Parcel in) {
        this.count = in.readString();
        this.setExtra(ParcelUtils.readFromParcel(in));
        this.setUserInfo((UserInfo) ParcelUtils.readFromParcel(in, UserInfo.class));
    }

    public static final Creator<RCChatRoomMemberNumMessage> CREATOR = new Creator<RCChatRoomMemberNumMessage>() {
        @Override
        public RCChatRoomMemberNumMessage createFromParcel(Parcel source) {
            return new RCChatRoomMemberNumMessage(source);
        }

        @Override
        public RCChatRoomMemberNumMessage[] newArray(int size) {
            return new RCChatRoomMemberNumMessage[size];
        }
    };
}
