package com.daya.live_teaching.im.message;

import android.os.Parcel;

import com.cooleshow.base.utils.LogUtils;
import com.daya.live_teaching.model.DownLoadSongStatus;
import com.daya.live_teaching.utils.log.SLog;
import com.google.gson.JsonObject;

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

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

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

/**
 * 伴奏下载消息
 */
@MessageTag(value = "DY:MSDSMsg", flag = MessageTag.NONE)
public class ExamSongDownloadStatusMessage extends MessageContent {
    private final static String TAG = ExamSongDownloadStatusMessage.class.getSimpleName();


    private String studentId;
    private List<DownLoadSongStatus> downLoadSongStatus = new ArrayList<>();

    public ExamSongDownloadStatusMessage(byte[] data) {
        String jsonStr = null;
        try {
            jsonStr = new String(data, "UTF-8");
            LogUtils.e(jsonStr);

            JSONObject jsonObject = new JSONObject(jsonStr);
            studentId = jsonObject.optString("studentId");
            JSONArray jsonArray = jsonObject.optJSONArray("studentMusicScores");
            downLoadSongStatus.clear();
            for (int i = 0; i < jsonArray.length(); i++) {
                Object object = jsonArray.get(i);
                JSONObject json = (JSONObject) object;
                DownLoadSongStatus student = new DownLoadSongStatus(json.optString("musicScoreAccompanimentId"), json.optInt("downStatus")
                        , json.optInt("playStatus"), json.optInt("accompanimentPlayStatus"), json.optInt("speed"), json.optString("url")
                        , json.optString("mp3Url"), json.optString("musicScoreName"));
                downLoadSongStatus.add(student);


            }

        } catch (Exception e) {
            SLog.e(TAG, e.toString());
        }
    }

    public ExamSongDownloadStatusMessage(Parcel parcel) {

        studentId = parcel.readString();
        downLoadSongStatus = parcel.createTypedArrayList(DownLoadSongStatus.CREATOR);
    }


    public String getStudentId() {
        return studentId;
    }


    public List<DownLoadSongStatus> getDownLoadSongStatus() {
        return downLoadSongStatus;
    }

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

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(studentId);
        dest.writeTypedList(downLoadSongStatus);

    }

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

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