package io.rong.imkit.widget;

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;

/**
 * 创建日期：2022/6/14 10:56
 *
 * @author Ryan
 * 类说明：
 */
@MessageTag(value = "RC:CHATSHARE:LIVE", flag = MessageTag.ISCOUNTED)
public class RCChatShareLiveMessage extends MessageContent {
    private String roomUID;
    private String teacherAvatar;
    private String teacherName;
    private String liveDescMessage;

    public String getRoomUID() {
        return roomUID;
    }

    public void setRoomUID(String roomUID) {
        this.roomUID = roomUID;
    }

    public String getTeacherAvatar() {
        return teacherAvatar;
    }

    public void setTeacherAvatar(String teacherAvatar) {
        this.teacherAvatar = teacherAvatar;
    }

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public String getLiveDescMessage() {
        return liveDescMessage;
    }

    public void setLiveDescMessage(String liveDescMessage) {
        this.liveDescMessage = liveDescMessage;
    }

    public RCChatShareLiveMessage() {
    }

    public RCChatShareLiveMessage(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("roomUID")) {
                roomUID = jsonObject.optString("roomUID");
            }
            if (jsonObject.has("teacherAvatar")) {
                teacherAvatar = jsonObject.optString("teacherAvatar");
            }
            if (jsonObject.has("teacherName")) {
                teacherName = jsonObject.optString("teacherName");
            }
            if (jsonObject.has("liveDescMessage")) {
                liveDescMessage = jsonObject.optString("liveDescMessage");
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public byte[] encode() {
        JSONObject jsonObj = new JSONObject();
        try {
            // 消息携带用户信息时, 自定义消息需添加下面代码
            if (!TextUtils.isEmpty(roomUID)) {
                jsonObj.putOpt("roomUID", roomUID);
            }
            if (!TextUtils.isEmpty(teacherAvatar)) {
                jsonObj.putOpt("teacherAvatar", teacherAvatar);
            }
            if (!TextUtils.isEmpty(teacherName)) {
                jsonObj.putOpt("teacherName", teacherName);
            }
            if (!TextUtils.isEmpty(liveDescMessage)) {
                jsonObj.putOpt("liveDescMessage", liveDescMessage);
            }
        } 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.roomUID);
        dest.writeString(this.teacherAvatar);
        dest.writeString(this.teacherName);
        dest.writeString(this.liveDescMessage);
        ParcelUtils.writeToParcel(dest, getExtra());
        ParcelUtils.writeToParcel(dest, getUserInfo());
    }

    protected RCChatShareLiveMessage(Parcel in) {
        this.roomUID = in.readString();
        this.teacherAvatar = in.readString();
        this.teacherName = in.readString();
        this.liveDescMessage = in.readString();
        this.setExtra(ParcelUtils.readFromParcel(in));
        this.setUserInfo((UserInfo)ParcelUtils.readFromParcel(in, UserInfo.class));
    }

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

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