package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.CaseLargeClassBean;
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 CaseLargeClassModelImpl implements ICaseLargeClassModel {

    private OnGetLargeClassListener listener;

    public CaseLargeClassModelImpl(OnGetLargeClassListener onGetLargeClassListener) {
        this.listener = onGetLargeClassListener;
    }

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

            }

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

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

    public interface OnGetLargeClassListener {
        void onSuccess(CaseLargeClassBean largeClassBean);

        void onFailure(Throwable throwable);
    }
}
