package com.datarecovery.master.module.example;

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

import com.atmob.app.lib.base.BaseViewModel;
import com.datarecovery.master.data.api.bean.ExampleBean;
import com.datarecovery.master.data.api.response.UserCaseResponse;
import com.datarecovery.master.data.repositories.DeviceFuncRepository;

import java.util.List;

import javax.inject.Inject;

import atmob.reactivex.rxjava3.annotations.NonNull;
import atmob.reactivex.rxjava3.core.SingleObserver;
import atmob.reactivex.rxjava3.disposables.Disposable;
import dagger.hilt.android.lifecycle.HiltViewModel;

@HiltViewModel
public class ExampleViewModel extends BaseViewModel {


    private final MutableLiveData<List<ExampleBean>> exampleList = new MutableLiveData<>();
    private final DeviceFuncRepository deviceFuncRepository;

    @Inject
    public ExampleViewModel(DeviceFuncRepository deviceFuncRepository) {
        this.deviceFuncRepository = deviceFuncRepository;
        refreshExampleData();
    }


    public LiveData<List<ExampleBean>> getExampleList() {
        return exampleList;
    }


    private void refreshExampleData() {
        deviceFuncRepository.userCaseList().subscribe(new SingleObserver<UserCaseResponse>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                addDisposable(d);
            }

            @Override
            public void onSuccess(@NonNull UserCaseResponse userCaseResponse) {
                exampleList.setValue(userCaseResponse.getExampleBeans());
            }

            @Override
            public void onError(@NonNull Throwable e) {

            }
        });
    }

}
