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

import com.casic.dcms.bean.EventSourceBean;
import com.casic.dcms.listener.OnGetEventSourceListener;
import com.casic.dcms.mvp.model.IEventSourceModel;
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;

/**
 * @author : Pengxh
 * @time : 2021/4/16 9:14
 * @email : 290677893@qq.com
 **/
public class EventSourceModelImpl implements IEventSourceModel {

    private final OnGetEventSourceListener eventSourceListener;

    public EventSourceModelImpl(OnGetEventSourceListener listener) {
        this.eventSourceListener = listener;
    }

    /**
     * {@link com.casic.dcms.mvp.model.IEventSourceModel}
     */
    @Override
    public Subscription sendEventSourceRequest(String begTime, String endTime) {
        Observable<EventSourceBean> observable = RetrofitServiceManager.getEventSourceResult(begTime, endTime);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<EventSourceBean>() {
            @Override
            public void onCompleted() {

            }

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

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