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:SeatResponse", flag = MessageTag.STATUS)
public class RCUserSeatResponseMessage extends MessageContent {
    private int type;//1 主讲人同意 2 主讲人拒绝 3 观众同意 4 观众拒绝
    private String teacherName;
    private String teacherId;
    private String audienceId;
    private String audienceName;

    public int getType() {
        return type;
    }

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

    public String getTeacherName() {
        return teacherName;
    }

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

    public String getTeacherId() {
        return teacherId;
    }

    public void setTeacherId(String teacherId) {
        this.teacherId = teacherId;
    }

    public String getAudienceId() {
        return audienceId;
    }

    public void setAudienceId(String audienceId) {
        this.audienceId = audienceId;
    }

    public String getAudienceName() {
        return audienceName;
    }

    public void setAudienceName(String audienceName) {
        this.audienceName = audienceName;
    }

    public RCUserSeatResponseMessage() {
    }

    public RCUserSeatResponseMessage(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("type")) {
                type = jsonObject.optInt("type");
            }

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

            if (jsonObject.has("teacherId")) {
                teacherId = jsonObject.optString("teacherId");
            }
            if (jsonObject.has("audienceId")) {
                audienceId = jsonObject.optString("audienceId");
            }
            if (jsonObject.has("audienceName")) {
                audienceName = jsonObject.optString("audienceName");
            }


        } 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 (type != 0) {
                jsonObj.putOpt("type", type);
            }

            if (!TextUtils.isEmpty(teacherName)) {
                jsonObj.putOpt("teacherName", teacherName);
            }

            if (!TextUtils.isEmpty(teacherId)) {
                jsonObj.putOpt("teacherId", teacherId);
            }

            if (!TextUtils.isEmpty(audienceId)) {
                jsonObj.putOpt("audienceId", audienceId);
            }

            if (!TextUtils.isEmpty(audienceName)) {
                jsonObj.putOpt("audienceName", audienceName);
            }
        } 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.writeInt(this.type);
        dest.writeString(this.teacherName);
        dest.writeString(this.teacherId);
        dest.writeString(this.audienceId);
        dest.writeString(this.audienceName);
        ParcelUtils.writeToParcel(dest, getExtra());
        ParcelUtils.writeToParcel(dest, getUserInfo());
    }

    protected RCUserSeatResponseMessage(Parcel in) {
        this.type = in.readInt();
        this.teacherName = in.readString();
        this.teacherId = in.readString();
        this.audienceId = in.readString();
        this.audienceName = in.readString();
        this.setExtra(ParcelUtils.readFromParcel(in));
        this.setUserInfo((UserInfo) ParcelUtils.readFromParcel(in, UserInfo.class));
    }

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

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