package com.datarecovery.master.data.repositories;



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

import com.datarecovery.master.data.api.AtmobApi;
import com.datarecovery.master.data.api.bean.OrderBean;
import com.datarecovery.master.data.api.request.FindOrderRequest;
import com.datarecovery.master.data.api.request.OrderPageRequest;
import com.datarecovery.master.data.api.request.OrderRequest;
import com.datarecovery.master.data.api.response.FindOrderResponse;
import com.datarecovery.master.data.api.response.OrderPageResponse;
import com.datarecovery.master.utils.RxHttpHandler;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import atmob.reactivex.rxjava3.core.Completable;
import atmob.reactivex.rxjava3.core.Single;
import atmob.rxjava.utils.RxJavaUtil;

@Singleton
public class PayRepository {

    private final MutableLiveData<List<OrderBean>> orderList = new MutableLiveData<>();
    private final AtmobApi atmobApi;

    @Inject
    public PayRepository(AtmobApi atmobApi) {
        this.atmobApi = atmobApi;
    }


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

    public void refreshOrderPageList() {
        orderPage(0, 200).subscribe();
    }

    public Single<OrderPageResponse> orderPage(int offset, int limit) {
        return atmobApi.orderPage(new OrderPageRequest(limit, offset))
                .compose(RxHttpHandler.handle(true))
                .compose(RxJavaUtil.SingleSchedule.io2Main())
                .doOnSuccess(orderPageResponse -> orderList.setValue(orderPageResponse.getList()));
    }

    public Single<FindOrderResponse> findOrder(String[] auths) {
        return atmobApi.findOrder(new FindOrderRequest(auths))
                .compose(RxHttpHandler.handle(true))
                .compose(RxJavaUtil.SingleSchedule.io2Main());
    }


    public void clearOderList() {
        orderList.setValue(null);
    }

    public Single<Object> cancelOrder(int id) {
        return atmobApi.cancelOrder(new OrderRequest(id))
                .compose(RxHttpHandler.handle(true))
                .compose(RxJavaUtil.SingleSchedule.io2Main());
    }
}
