package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.CaseListResultBean;
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 CaseListModelImpl implements ICaseListModel {

    private OnGetCaseListListener listener;

    public CaseListModelImpl(OnGetCaseListListener caseListListener) {
        this.listener = caseListListener;
    }

    public interface OnGetCaseListListener {
        void onSuccess(CaseListResultBean resultBean);

        void onFailure(Throwable throwable);
    }

    @Override
    public Subscription sendRetrofitRequest(String casetypeCode, String casetypeDetailCode, String caseState,
                                            String registerTimeBegin, String registerTimeEnd, String eorc, int offset) {
        Observable<CaseListResultBean> observable = RetrofitServiceManager.getCaseListResult(casetypeCode, casetypeDetailCode, caseState,
                registerTimeBegin, registerTimeEnd, eorc, offset);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CaseListResultBean>() {
            @Override
            public void onCompleted() {

            }

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

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