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.api.RequestListener;
import com.offline.music.playermp3.helper.CommonUtils;
import com.offline.music.playermp3.javabean.response.ResponseSearch;
import com.offline.music.playermp3.network.JsonHelper;
import com.offline.music.playermp3.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;


    private MutableLiveData<List<ResponseSearch>> _result= new MutableLiveData<List<ResponseSearch>>();
    public LiveData<List<ResponseSearch>> result = _result;
    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) {
                _result.setValue(null);
            }

            @Override
            public void onSuccess(JSONObject data) {
                if (data != null) {
                    List<ResponseSearch> responseSearches = JsonHelper.ResolveSearchResult(data);
                    _result.setValue(responseSearches);
                } else {
                    _result.setValue(null);
                }

            }
        });
    }

}
