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

import com.casic.dcms.bean.ComSentenceBean;
import com.casic.dcms.listener.OnGetSentenceListener;
import com.casic.dcms.mvp.model.IComSentenceModel;
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 ComSentenceModelImpl implements IComSentenceModel {

    private final OnGetSentenceListener sentenceListener;

    public ComSentenceModelImpl(OnGetSentenceListener listener) {
        this.sentenceListener = listener;
    }

    /**
     * {@link IComSentenceModel}
     */
    @Override
    public Subscription sendComSentenceRequest() {
        Observable<ComSentenceBean> observable = RetrofitServiceManager.getCommonSentenceResult();
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ComSentenceBean>() {
            @Override
            public void onCompleted() {

            }

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

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