package com.offline.music.playermp3.ui.fragmnt.viewmodel;

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

import com.offline.music.playermp3.MusicApplication;
import com.offline.music.playermp3.api.RequestListener;
import com.offline.music.playermp3.javabean.response.ResponseHome;
import com.offline.music.playermp3.network.JsonHelper;
import com.offline.music.playermp3.network.RetrofitManager;

import org.json.JSONObject;

import okhttp3.ResponseBody;

public class VMHome extends ViewModel {


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

    private String continuation, clickTrackingParams, visitorData;

    public void getHome() {
        RetrofitManager.getInstance().getHomeData(new RequestListener<ResponseBody>() {

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

            @Override
            public void onSuccess(JSONObject data) {
//                JSONObject jsonObject = CommonUtils.toJsonObject(data);
                if (data != null) {
                    ResponseHome responseHome = JsonHelper.ResolveHomeJson(data);

                    continuation = responseHome.getContinuation();
                    clickTrackingParams = responseHome.getClickTrackingParams();
                    visitorData = responseHome.getVisitorData();
                    MusicApplication.setVisitorData(visitorData);
                    _data.setValue(responseHome);
                } else {
                    _data.setValue(null);
                }

            }
        });
    }


    public void getHomeMore() {
        RetrofitManager.getInstance().getHomeMoreData(continuation, clickTrackingParams, visitorData, new RequestListener<ResponseBody>() {

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

            @Override
            public void onSuccess(JSONObject data) {
//                JSONObject jsonObject = CommonUtils.toJsonObject(data);
                if (data != null) {
                    ResponseHome responseHome = JsonHelper.ResolveHomeMoreJson(data);

                    continuation = responseHome.getContinuation();
                    clickTrackingParams = responseHome.getClickTrackingParams();

                    _data.setValue(responseHome);
                } else {
                    _data.setValue(null);
                }

            }
        });
    }

}
