package com.datarecovery.master.module.order;

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

import com.atmob.app.lib.base.BaseViewModel;
import com.atmob.app.lib.livedata.SingleLiveEvent;
import com.datarecovery.master.data.api.bean.OrderBean;
import com.datarecovery.master.data.api.response.OrderPageResponse;
import com.datarecovery.master.data.repositories.PayRepository;

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 OrderViewModel extends BaseViewModel {


    private final PayRepository payRepository;


    private final SingleLiveEvent<?> refreshOrderListEvent = new SingleLiveEvent<>();

    @Inject
    public OrderViewModel(PayRepository payRepository) {
        this.payRepository = payRepository;
        refreshOrderPageList();
    }

    public LiveData<List<OrderBean>> getOrderList() {
        return payRepository.getOrderList();
    }

    public LiveData<?> getRefreshOrderListEvent() {
        return refreshOrderListEvent;
    }

    public void refreshOrderPageList() {
        payRepository.orderPage(0, 200).subscribe(new SingleObserver<OrderPageResponse>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                addDisposable(d);
            }

            @Override
            public void onSuccess(@NonNull OrderPageResponse orderPageResponse) {
                refreshOrderListEvent.call();
            }

            @Override
            public void onError(@NonNull Throwable e) {
                refreshOrderListEvent.call();
            }
        });
    }
}
