package org.mercury_im.messenger.ui.roster.contacts;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import org.mercury_im.messenger.MercuryImApplication;
import org.mercury_im.messenger.data.repository.XmppPeerRepository;
import org.mercury_im.messenger.entity.contact.Peer;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.disposables.CompositeDisposable;


public class ContactListViewModel extends ViewModel {

    @Inject
    XmppPeerRepository xmppContactRepository;

    private final MutableLiveData<List<Peer>> rosterEntryList = new MutableLiveData<>();
    private final CompositeDisposable compositeDisposable = new CompositeDisposable();

    public ContactListViewModel() {
        super();
        MercuryImApplication.getApplication().getAppComponent().inject(this);
        Log.d("ContactListViewModel", "Start observing database");
        // Subscribe to changes to the contacts table and update the LiveData object for the UI.
        compositeDisposable.add(xmppContactRepository.observeAllPeers()
                .subscribe(rosterEntryList::setValue));
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        compositeDisposable.clear();
    }

    public LiveData<List<Peer>> getRosterEntryList() {
        return rosterEntryList;
    }
}
