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.user.UserList;
import com.kaixed.kchat.network.NetworkRequest;

import java.io.IOException;

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

/**
 * @Author: kaixed
 * @Date: 2024/6/13 12:34
 * @Description: TODO
 */
public class UserRepository {
    public MutableLiveData<UserList> getUserListByNickname(String username) {

        MutableLiveData<UserList> 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();
                    UserList userList = new Gson().fromJson(responseBody, UserList.class);
                    listMutableLiveData.postValue(userList);
                } else {
                    listMutableLiveData.postValue(null);
                }
            }
        });
        return listMutableLiveData;
    }
}
