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

import com.casic.dcms.bean.WellDetailBean;
import com.casic.dcms.listener.OnGetWellDetailListener;
import com.casic.dcms.mvp.model.IWellDetailModel;
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 WellDetailModelImpl implements IWellDetailModel {

    private final OnGetWellDetailListener wellDetailListener;

    public WellDetailModelImpl(OnGetWellDetailListener listener) {
        this.wellDetailListener = listener;
    }

    /**
     * {@link IWellDetailModel}
     */
    @Override
    public Subscription sendWellDetailRequest(String where) {
        Observable<WellDetailBean> observable = RetrofitServiceManager.getWellDetailResult(where);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<WellDetailBean>() {
            @Override
            public void onCompleted() {

            }

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

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