package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.CaseSmallClassBean;
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 CaseSmallClassModelImpl implements ICaseSmallClassModel {

    private OnGetSmallClassListener listener;

    public CaseSmallClassModelImpl(OnGetSmallClassListener onGetSmallClassListener) {
        this.listener = onGetSmallClassListener;
    }

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

            }

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

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

    public interface OnGetSmallClassListener {
        void onSuccess(CaseSmallClassBean smallClassBean);

        void onFailure(Throwable throwable);
    }
}
