package com.daya.live_teaching.im.message;

import android.os.Parcel;

import com.cooleshow.base.utils.LogUtils;
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 = "DY:PlayMidiMessage", flag = MessageTag.NONE)
public class PlayMidiMessage extends MessageContent {
    private final static String TAG = PlayMidiMessage.class.getSimpleName();

    private String  userId;
    private boolean enable;
    private int     rate;
    private int     customType;
    private int     playVolume;

    public PlayMidiMessage(byte[] data) {
        String jsonStr = null;
        try {
            jsonStr = new String(data, "UTF-8");
            LogUtils.e(jsonStr);
        } catch (UnsupportedEncodingException e) {
            SLog.e(TAG, e.toString());
        }
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            userId = jsonObject.optString("userId");
            enable = jsonObject.optBoolean("enable");
            rate = jsonObject.optInt("rate");
            customType = jsonObject.optInt("customType");
            playVolume = jsonObject.optInt("playVolume");
        } catch (JSONException e) {
            SLog.e(TAG, e.toString());
        }
    }

    public PlayMidiMessage(Parcel parcel) {
        userId = parcel.readString();
        enable = parcel.readByte() == 1;
        rate = parcel.readInt();
        customType = parcel.readInt();
        playVolume = parcel.readInt();
    }

    public String getUserId() {
        return userId;
    }

    public boolean isEnable() {
        return enable;
    }

    public int getRate() {
        return rate;
    }

    public int getCustomType() {
        return customType;
    }

    public int getPlayVolume() {
        return playVolume;
    }


    @Override
    public byte[] encode() {
        return new byte[0];
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(userId);
        dest.writeByte((byte) (enable ? 1 : 0));
        dest.writeInt(rate);
        dest.writeInt(customType);
        dest.writeInt(playVolume);
    }

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

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