package com.hi.music.player.ui.fragmnt.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.network.JsonHelper;
import com.hi.music.player.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(ResponseBody data) {
                JSONObject jsonObject = CommonUtils.toJsonObject(data);
                if (jsonObject != null) {
                    ResponseHome responseHome = JsonHelper.ResolveHomeJson(jsonObject);

                    continuation = responseHome.getContinuation();
                    clickTrackingParams = responseHome.getClickTrackingParams();
                    visitorData = responseHome.getVisitorData();
                    _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(ResponseBody data) {
                JSONObject jsonObject = CommonUtils.toJsonObject(data);
                if (jsonObject != null) {
                    ResponseHome responseHome = JsonHelper.ResolveHomeMoreJson(jsonObject);

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

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

            }
        });
    }

}
