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

import com.casic.dcms.bean.CaseCheckBean;
import com.casic.dcms.listener.OnGetCaseCheckListener;
import com.casic.dcms.mvp.model.ICaseCheckModel;
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 CaseCheckModelImpl implements ICaseCheckModel {

    private final OnGetCaseCheckListener caseCheckListener;

    public CaseCheckModelImpl(OnGetCaseCheckListener listener) {
        this.caseCheckListener = listener;
    }

    /**
     * {@link ICaseCheckModel}
     * <p>
     * 地图查看案卷-待核查
     */
    @Override
    public Subscription sendCaseCheckRequest(int limit, int offset, boolean isChecked) {
        Observable<CaseCheckBean> observable;
        if (isChecked) {
            observable = RetrofitServiceManager.getCaseCheckedListResult(limit, offset);
        } else {
            observable = RetrofitServiceManager.getCaseCheckListResult(limit, offset);
        }
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CaseCheckBean>() {
            @Override
            public void onCompleted() {

            }

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

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