package com.casic.dcms.mvp.model.impl;

import com.casic.dcms.bean.PackageOrDeptBean;
import com.casic.dcms.listener.OnGetPackageCaseListener;
import com.casic.dcms.mvp.model.IPackageCaseModel;
import com.casic.dcms.utils.retrofit.RetrofitServiceManager;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class PackageCaseModelImpl implements IPackageCaseModel {

    private final OnGetPackageCaseListener packageCaseListener;

    public PackageCaseModelImpl(OnGetPackageCaseListener listener) {
        this.packageCaseListener = listener;
    }

    //三包处置

    /**
     * {@link IPackageCaseModel}
     */
    @Override
    public Subscription sendPackageCaseRequest(String isShop, int offset, boolean isCompleted) {
        Observable<PackageOrDeptBean> observable;
        if (isCompleted) {
            observable = RetrofitServiceManager.getCompletedListResult(isShop, offset);
        } else {
            observable = RetrofitServiceManager.getPackageWorkListResult(isShop, offset);
        }
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<PackageOrDeptBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                if (e.getCause() != null) {
                    packageCaseListener.onFailure(e);
                }
            }

            @Override
            public void onNext(PackageOrDeptBean resultBean) {
                if (resultBean != null) {
                    packageCaseListener.onSuccess(resultBean);
                }
            }
        });
    }
}
