package com.ttstd.dialer.fragment.contact;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.trello.rxlifecycle4.RxLifecycle;
import com.trello.rxlifecycle4.android.FragmentEvent;
import com.ttstd.dialer.base.mvvm.BaseViewModel;
import com.ttstd.dialer.databinding.FragmentContactBinding;
import com.ttstd.dialer.db.contact.ContactInfo;
import com.ttstd.dialer.db.contact.ContactRepository;

import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.ObservableEmitter;
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class ContactViewModel extends BaseViewModel<FragmentContactBinding, FragmentEvent> {
    private ContactRepository mRepository;

    @Override
    public void setContext(Context context) {
        super.setContext(context);
        mRepository = new ContactRepository(context);
    }

    public MutableLiveData<List<ContactInfo>> mContactListData = new MutableLiveData<>();

    public void getAllContacts() {
        Observable.create(new ObservableOnSubscribe<List<ContactInfo>>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<List<ContactInfo>> emitter) throws Throwable {
                List<ContactInfo> contactInfos = mRepository.getAllContacts();
                emitter.onNext(contactInfos);
                emitter.onComplete();
            }
        }).compose(RxLifecycle.bindUntilEvent(getLifecycle(), FragmentEvent.DESTROY))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ContactInfo>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Log.e("getAllContacts", "onSubscribe: ");
                    }

                    @Override
                    public void onNext(@NonNull List<ContactInfo> contactInfos) {
                        Log.e("getAllContacts", "onNext: ");
                        mContactListData.setValue(contactInfos);

//                        List<Contact> sorted = contacts.stream().sorted(new Comparator<Contact>() {
//                            @Override
//                            public int compare(Contact o1, Contact o2) {
//                                return Integer.compare(o1.getSort(), o2.getSort());
//                            }
//                        }).collect(Collectors.toList());
//                        mContactListData.setValue(sorted);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.e("getAllContacts", "onError: " + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        Log.e("getAllContacts", "onComplete: ");
                    }
                });
    }

}
