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.CustomerUrlInfo;
import com.hi.music.player.javabean.response.ResponsePlayListInfo;
import com.hi.music.player.javabean.response.ResponsePlayUrl;
import com.hi.music.player.javabean.response.child.ResponseSingle;
import com.hi.music.player.media3.MyMediaControllerManager;
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<ResponsePlayListInfo>> _playList = new MutableLiveData<List<ResponsePlayListInfo>>();
    public LiveData<List<ResponsePlayListInfo>> playList = _playList;


    private MutableLiveData<CustomerUrlInfo> _playUrlMutableLiveData = new MutableLiveData<CustomerUrlInfo>();
    public LiveData<CustomerUrlInfo> playUrlLiveData = _playUrlMutableLiveData;



    public void getPlayMusicList(String playlistId, String videoId,  String params, String musicVideoType) {
        RetrofitManager.getInstance().getPlayList(params, playlistId, videoId, musicVideoType, new RequestListener<ResponseBody>() {

            @Override
            public void onFail(String errorMsg) {
                // TODO: 2024/10/8  播放列表拉取失败 
                _playList.setValue(null);
            }

            @Override
            public void onSuccess(JSONObject data) {
//                JSONObject jsonObject = CommonUtils.toJsonObject(data);
                if (data != null) {
                    List<ResponsePlayListInfo> responsePlayListInfos = JsonHelper.ResolvePlayListJson(data);
                    _playList.setValue(responsePlayListInfos);
                } else {
                    _playList.setValue(null);
                }

            }
        });
    }

    public void getPlayUrl(String videoId, int playListIndex,boolean playNow) {
        CustomerUrlInfo customerUrlInfo = new CustomerUrlInfo();
        customerUrlInfo.setNeedPlayNow(playNow);
        customerUrlInfo.setVideoId(videoId);
        customerUrlInfo.setPlayMusicIndex(playListIndex);
        RetrofitManager.getInstance().getPlayUrl(videoId, new RequestListener<ResponseBody>() {

            @Override
            public void onFail(String errorMsg) {
                CommonUtils.LogMsg("-------------此次网络请求失败  playNow="+playNow +"--playListIndex="+playListIndex);
                _playUrlMutableLiveData.setValue(customerUrlInfo);
            }

            @Override
            public void onSuccess(JSONObject data) {
//                JSONObject jsonObject = CommonUtils.toJsonObject(data);
                if (data != null) {
                    ResponsePlayUrl responsePlayUrl = JsonHelper.ResolvePlayUrlJson(data);
                    if(responsePlayUrl == null){
                        // TODO: 2024/9/27  
                        return;
                    }
                    MyMediaControllerManager.getInstance().UpdateAudioUrl(responsePlayUrl,playListIndex);
//                    MyMediaControllerManager.getInstance().addMusicToPlayList(responsePlayUrl,playListIndex);
                    customerUrlInfo.setPlayUrl(responsePlayUrl);

                }
                _playUrlMutableLiveData.setValue(customerUrlInfo);

            }
        });
    }


}
