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 java.util.List;

import okhttp3.ResponseBody;

public class VMSearch extends ViewModel {
    private MutableLiveData<List<String>> _suggestion = new MutableLiveData<List<String>>();
    public LiveData<List<String>> suggestion = _suggestion;
    public void getSuggestion(String input) {
        RetrofitManager.getInstance().getSearchSuggestion(input,new RequestListener<ResponseBody>() {

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

            @Override
            public void onSuccess(JSONObject data) {
                if (data != null) {
                    List<String> stringList = JsonHelper.ResolveSearchSuggestion(data);
                    CommonUtils.LogMsg("------------onSuccess=  stringList="+stringList );
                    _suggestion.setValue(stringList);
                } else {
                    _suggestion.setValue(null);
                }

            }
        });
    }


    public void getSearchResult(String query) {
        RetrofitManager.getInstance().getSearch(query, new RequestListener<ResponseBody>() {

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

            @Override
            public void onSuccess(JSONObject data) {
                if (data != null) {
                      JsonHelper.ResolveSearchResult(data);
//                    _data.setValue(responseHome);
                } else {
//                    _data.setValue(null);
                }

            }
        });
    }

}
