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

import com.casic.dcms.bean.CaseSmallClassBean;
import com.casic.dcms.listener.OnGetSmallClassListener;
import com.casic.dcms.mvp.model.ICaseSmallClassModel;
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 final OnGetSmallClassListener smallClassListener;

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

    /**
     * {@link ICaseSmallClassModel}
     */
    @Override
    public Subscription sendSmallClassRequest(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) {
                    smallClassListener.onFailure(e);
                }
            }

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