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.
 * 用户被踢出房间消息--只接收
 */
@MessageTag(value = "RC:Chatroom:KickOut",flag = MessageTag.STATUS)
public class RCUserKickOutMessage extends MessageContent {
    private String userId;// 操作者ID
    private String userName;//操作者名称
    private String targetId;//被踢出用户id
    private String targetName;//被踢出用户名称

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getTargetId() {
        return targetId;
    }

    public void setTargetId(String targetId) {
        this.targetId = targetId;
    }

    public String getTargetName() {
        return targetName;
    }

    public void setTargetName(String targetName) {
        this.targetName = targetName;
    }

    public RCUserKickOutMessage() {
    }
    public RCUserKickOutMessage(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("user")) {
                setUserInfo(this.parseJsonToUserInfo(jsonObject.getJSONObject("user")));
            }

            if (jsonObject.has("userId")) {
                userId = jsonObject.optString("userId");
            }
            if (jsonObject.has("userName")) {
                userName = jsonObject.optString("userName");
            }

            if (jsonObject.has("targetId")) {
                targetId = jsonObject.optString("targetId");
            }
            if (jsonObject.has("targetName")) {
                targetName = jsonObject.optString("targetName");
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

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

            if (!TextUtils.isEmpty(userId)) {
                jsonObj.putOpt("userId", userId);
            }
            if (!TextUtils.isEmpty(userName)) {
                jsonObj.putOpt("audienceName", userName);
            }

            if (!TextUtils.isEmpty(targetId)) {
                jsonObj.putOpt("targetId", targetId);
            }
            if (!TextUtils.isEmpty(targetName)) {
                jsonObj.putOpt("targetName", targetName);
            }
        } 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.userId);
        dest.writeString(this.userName);
        dest.writeString(this.targetId);
        dest.writeString(this.targetName);
        ParcelUtils.writeToParcel(dest, getExtra());
        ParcelUtils.writeToParcel(dest, getUserInfo());
    }

    protected RCUserKickOutMessage(Parcel in) {
        this.userId = in.readString();
        this.userName = in.readString();
        this.targetId = in.readString();
        this.targetName = in.readString();
        this.setExtra(ParcelUtils.readFromParcel(in));
        this.setUserInfo((UserInfo)ParcelUtils.readFromParcel(in, UserInfo.class));
    }

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

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