package com.daya.live_teaching.im.message;

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

import com.daya.live_teaching.model.DeviceType;
import com.daya.live_teaching.utils.log.SLog;

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

import java.io.UnsupportedEncodingException;

import io.rong.common.ParcelUtils;
import io.rong.common.RLog;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

/**
 * 设备状态同步消息
 */
@MessageTag(value = "SC:DRMsg", flag = MessageTag.NONE)
public class DeviceStateChangedMessage extends MessageContent {
    private final static String TAG = DeviceStateChangedMessage.class.getSimpleName();

    private Boolean enable; // 开启状态
    private int type; // 设备类型:0.麦克风；1.摄像头 5 原生音量  6 伴奏音量
    private String userId;
    private String musicScoreAccompanimentId;
    private int soundVolume;


    public DeviceStateChangedMessage(byte[] data) {
        Log.i("VOLEUM_TAG","musicScoreAccompanimentId");
        String jsonStr = null;
        try {
            jsonStr = new String(data, "UTF-8");
            Log.e("VOLEUM_TAG",jsonStr);
        } catch (UnsupportedEncodingException e) {
            SLog.e(TAG, e.toString());
        }
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            enable = jsonObject.optBoolean("enable");
            type = jsonObject.optInt("type");
            userId = jsonObject.optString("userId");
            if (jsonObject.has("soundVolume")) {
                soundVolume = jsonObject.optInt("soundVolume");
            }
            musicScoreAccompanimentId = jsonObject.optString("musicScoreAccompanimentId");
            Log.i("VOLEUM_TAG","musicScoreAccompanimentId  jsonObject " + jsonObject.toString());
        } catch (JSONException e) {
            SLog.e(TAG, e.toString());
        }
    }

    public DeviceStateChangedMessage(Parcel parcel) {
        enable = parcel.readInt() != 0;
        type = parcel.readInt();
        userId = parcel.readString();
        soundVolume = parcel.readInt();
        musicScoreAccompanimentId = parcel.readString();
        Log.i("VOLEUM_TAG","musicScoreAccompanimentId_Parcel _soundVolume=" +soundVolume);
    }

    public Boolean getEnable() {
        return enable;
    }

    public DeviceType getType() {
        return DeviceType.getDeviceType(type);
    }

    public String getUserId() {
        return userId;
    }

    public int getSoundVolume() {
        return soundVolume;
    }

    public void setSoundVolume(int soundVolume) {
        this.soundVolume = soundVolume;
    }

    public String getMusicScoreAccompanimentId() {
        return musicScoreAccompanimentId;
    }

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(enable ? 1 : 0);
        dest.writeInt(type);
        dest.writeString(userId);
        dest.writeInt(soundVolume);
        dest.writeString(musicScoreAccompanimentId);

    }

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

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