package com.hi.music.player.ui.activity.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.hi.music.player.api.RequestListener;
import com.hi.music.player.helper.CommonUtils;
import com.hi.music.player.javabean.response.ResponseHome;
import com.hi.music.player.javabean.response.ResponsePlay;
import com.hi.music.player.javabean.response.child.ResponseSingle;
import com.hi.music.player.network.JsonHelper;
import com.hi.music.player.network.RetrofitManager;

import org.json.JSONObject;

import java.util.List;

import okhttp3.ResponseBody;

public class VMPlay extends ViewModel {


    private MutableLiveData<List<ResponsePlay>> _data = new MutableLiveData<List<ResponsePlay>>();
    public LiveData<List<ResponsePlay>> data = _data;

    private String continuation, clickTrackingParams, visitorData;

    public void getPlay(ResponseSingle responseSingle) {
        String playlistId = responseSingle.getPlaylistId();
        String videoId = responseSingle.getVideoId();
        String params = responseSingle.getParams();
        String musicVideoType = responseSingle.getMusicVideoType();

        RetrofitManager.getInstance().getNext(params, playlistId, videoId, musicVideoType, new RequestListener<ResponseBody>() {

            @Override
            public void onFail(String errorMsg) {
                _data.setValue(null);
            }

            @Override
            public void onSuccess(ResponseBody data) {
                JSONObject jsonObject = CommonUtils.toJsonObject(data);
                if (jsonObject != null) {
                    List<ResponsePlay> responsePlays = JsonHelper.ResolvePlayJson(jsonObject);
                    _data.setValue(responsePlays);
                } else {
                    _data.setValue(null);
                }

            }
        });
    }


}
