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

import com.casic.dcms.bean.CaseStatusBean;
import com.casic.dcms.listener.OnGetCaseStatusListener;
import com.casic.dcms.mvp.model.ICaseStatusModel;
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 CaseStatusModelImpl implements ICaseStatusModel {

    private final OnGetCaseStatusListener caseStatusListener;

    public CaseStatusModelImpl(OnGetCaseStatusListener listener) {
        this.caseStatusListener = listener;
    }

    /**
     * {@link ICaseStatusModel}
     */
    @Override
    public Subscription sendCaseStatusRequest() {
        Observable<CaseStatusBean> observable = RetrofitServiceManager.getCaseStatusResult();
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CaseStatusBean>() {
            @Override
            public void onCompleted() {

            }

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

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