package com.daya.live_teaching.im.message;

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

import com.cooleshow.base.utils.LogUtils;
import com.daya.live_teaching.model.ClassMemberChangedAction;
import com.daya.live_teaching.model.Role;
import com.daya.live_teaching.utils.log.SLog;

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

import java.io.UnsupportedEncodingException;

import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

/**
 * 课堂内用户进出消息
 */
@MessageTag(value = "SC:RMCMsg", flag = MessageTag.ISPERSISTED)
public class MemberChangedMessage extends MessageContent {
    private final static String TAG = MemberChangedMessage.class.getSimpleName();
    private int action;     // 用户行为：1.加入；2.离开；3.踢出
    private String userId;
    private String userName;
    private String avatar;
    private int role;       // 用户角色
    private long timestamp;
    private boolean metronomeSwitch;
    private boolean handUpOn;
    private boolean examSongSwitch;


    public MemberChangedMessage(byte[] data) {
        String jsonStr = null;
        try {
            jsonStr = new String(data, "UTF-8");
            LogUtils.e("pq","MemberChangedMessage:"+jsonStr);
        } catch (UnsupportedEncodingException e) {
            SLog.e(TAG, e.toString());
        }
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            String changedEnum = jsonObject.optString("changedEnum");
            if (TextUtils.equals(changedEnum, "LEAVE")) {
                action = 2;
            } else if (TextUtils.equals(changedEnum, "JOIN")) {
                action = 1;
            } else {
                action = 3;
            }
            JSONObject roomMemberJson = jsonObject.optJSONObject("roomMember");
            userId = roomMemberJson.optString("userId");
            userName = roomMemberJson.optString("username");
            avatar = roomMemberJson.optString("avatar");
            role = roomMemberJson.optInt("role");
            timestamp = roomMemberJson.optLong("timestamp");
            metronomeSwitch = jsonObject.optBoolean("metronomeSwitch");
            handUpOn = roomMemberJson.optBoolean("handFlag");
            examSongSwitch = jsonObject.optBoolean("examSongSwitch");
        } catch (JSONException e) {
            SLog.e(TAG, e.toString());
        }
    }

    public MemberChangedMessage(Parcel in) {
        action = in.readInt();
        userId = in.readString();
        userName = in.readString();
        avatar = in.readString();
        role = in.readInt();
        timestamp = in.readLong();
        metronomeSwitch = in.readByte() == 1;
        handUpOn = in.readByte() == 1;
        examSongSwitch = in.readByte() == 1;
    }

    public ClassMemberChangedAction getAction() {
        return ClassMemberChangedAction.getAction(action);
    }

    public String getUserId() {
        return userId;
    }

    public String getUserName() {
        return userName;
    }

    public Role getRole() {
        return Role.createRole(role);
    }

    public long getTimestamp() {
        return timestamp;
    }

    public boolean isMetronomeSwitch() {
        return metronomeSwitch;
    }

    public boolean isHandUpOn() {
        return handUpOn;
    }

    public boolean isExamSongSwitch() {
        return examSongSwitch;
    }

    public String getHeadUrl() {
        return avatar;
    }

    public void setHeadUrl(String headUrl) {
        this.avatar = headUrl;
    }

    @Override
    public byte[] encode() {
        JSONObject jsonObject = new JSONObject();
        try {
//            jsonObject.put("action", action);
            jsonObject.put("userId", userId);
            jsonObject.put("username", userName);
            jsonObject.put("avatar", avatar);
            jsonObject.put("role", role);
            jsonObject.put("timestamp", timestamp);
            jsonObject.put("metronomeSwitch", metronomeSwitch);
            jsonObject.put("handFlag", handUpOn);
            jsonObject.put("examSongSwitch", examSongSwitch);
        } catch (JSONException e) {
            SLog.e(TAG, e.toString());
        }

        try {
            return jsonObject.toString().getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            SLog.e(TAG, e.toString());
        }
        return new byte[0];
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(action);
        dest.writeString(userId);
        dest.writeString(userName);
        dest.writeString(avatar);
        dest.writeInt(role);
        dest.writeLong(timestamp);
        dest.writeByte((byte) (metronomeSwitch ? 1 : 0));
        dest.writeByte((byte) (handUpOn ? 1 : 0));
        dest.writeByte((byte) (examSongSwitch ? 1 : 0));

    }

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

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