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

import com.casic.dcms.bean.CaseDetailBean;
import com.casic.dcms.listener.OnGetCaseDetailListener;
import com.casic.dcms.mvp.model.ICaseDetailModel;
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 CaseDetailModelImpl implements ICaseDetailModel {

    private final OnGetCaseDetailListener caseDetailListener;

    public CaseDetailModelImpl(OnGetCaseDetailListener listener) {
        this.caseDetailListener = listener;
    }

    /**
     * {@link ICaseDetailModel}
     */
    @Override
    public Subscription sendCaseDetailRequest(String id) {
        Observable<CaseDetailBean> observable = RetrofitServiceManager.getCaseDetailResult(id);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CaseDetailBean>() {
            @Override
            public void onCompleted() {

            }

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

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