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

import com.casic.dcms.bean.CaseListResultBean;
import com.casic.dcms.listener.OnGetSearchListListener;
import com.casic.dcms.mvp.model.ICaseListModel;
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 final OnGetSearchListListener searchListListener;

    public CaseListModelImpl(OnGetSearchListListener listener) {
        this.searchListListener = listener;
    }

    /**
     * {@link ICaseListModel}
     */
    @Override
    public Subscription sendCaseListRequest(String eorc, String casetypeCode, String casetypeDetailCode, String caseState, String registerTimeBegin, String registerTimeEnd, String isShopCase, int offset) {
        Observable<CaseListResultBean> observable = RetrofitServiceManager.getCaseListResult(eorc, casetypeCode, casetypeDetailCode, caseState,
                registerTimeBegin, registerTimeEnd, isShopCase, 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) {
                    searchListListener.onFailure(e);
                }
            }

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