package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.CaseDetailBean;
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 OnGetCaseDetailListener listener;

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

    public interface OnGetCaseDetailListener {
        void onSuccess(CaseDetailBean resultBean);

        void onFailure(Throwable throwable);
    }

    @Override
    public Subscription sendRetrofitRequest(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) {
                    listener.onFailure(e);
                }
            }

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

    private static final String TAG = "CaseDetailModelImpl";
}
