package com.kaixed.kchat.repository;

import static com.kaixed.kchat.network.NetworkInterface.SERVER_URL;
import static com.kaixed.kchat.network.NetworkInterface.USER_LIST;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.kaixed.kchat.model.search.SearchFriends;
import com.kaixed.kchat.network.NetworkRequest;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * @Author: kaixed
 * @Date: 2024/6/1 20:53
 * @Description: TODO
 */
public class SearchFriendsRepository {
    public MutableLiveData<SearchFriends> searchFriends(String username) {

        MutableLiveData<SearchFriends> listMutableLiveData = new MutableLiveData<>();

        new NetworkRequest().getAsync(SERVER_URL + USER_LIST + username, new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {

            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                if (response.isSuccessful() && response.body() != null) {
                    String responseBody = response.body().string();
                    SearchFriends searchFriends = new Gson().fromJson(responseBody, SearchFriends.class);
                    listMutableLiveData.postValue(searchFriends);
                } else {
                    listMutableLiveData.postValue(null);
                }
            }
        });
        return listMutableLiveData;
    }
}
