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

import com.casic.dcms.bean.CaseLargeClassBean;
import com.casic.dcms.listener.OnGetLargeClassListener;
import com.casic.dcms.mvp.model.ICaseLargeClassModel;
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 final OnGetLargeClassListener largeClassListener;

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

    /**
     * {@link ICaseLargeClassModel}
     */
    @Override
    public Subscription sendLargeClassRequest(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) {
                    largeClassListener.onFailure(e);
                }
            }

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