package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.CommonSentenceBean;
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 CaseCommonSentenceModelImpl implements ICaseCommonSentenceModel {

    private OnGetCommonSentenceListener sentenceListener;

    public CaseCommonSentenceModelImpl(OnGetCommonSentenceListener commonSentenceListener) {
        this.sentenceListener = commonSentenceListener;
    }

    public interface OnGetCommonSentenceListener {
        void onSuccess(CommonSentenceBean resultBean);

        void onFailure(Throwable throwable);
    }

    @Override
    public Subscription sendRetrofitRequest() {
        Observable<CommonSentenceBean> observable = RetrofitServiceManager.getCommonSentenceResult();
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CommonSentenceBean>() {
            @Override
            public void onCompleted() {

            }

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

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