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

import com.casic.dcms.bean.CaseVerifyBean;
import com.casic.dcms.listener.OnGetCaseVerifyListener;
import com.casic.dcms.mvp.model.ICaseVerifyModel;
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 CaseVerifyModelImpl implements ICaseVerifyModel {

    private final OnGetCaseVerifyListener verifyListener;

    public CaseVerifyModelImpl(OnGetCaseVerifyListener listener) {
        this.verifyListener = listener;
    }

    /**
     * {@link ICaseVerifyModel}
     * <p>
     * 地图查看案卷-待核实
     */
    @Override
    public Subscription sendCaseVerifyRequest(int limit, int offset, boolean verified) {
        Observable<CaseVerifyBean> observable;
        if (verified) {
            observable = RetrofitServiceManager.getCaseVerifiedListResult(limit, offset);
        } else {
            observable = RetrofitServiceManager.getCaseVerifyListResult(limit, offset);
        }
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CaseVerifyBean>() {
            @Override
            public void onCompleted() {

            }

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

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