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

import com.casic.dcms.bean.HighEventBean;
import com.casic.dcms.listener.OnGetHighEventListener;
import com.casic.dcms.mvp.model.IHighEventModel;
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 HighEventModelImpl implements IHighEventModel {

    private final OnGetHighEventListener highEventListener;

    public HighEventModelImpl(OnGetHighEventListener listener) {
        this.highEventListener = listener;
    }

    /**
     * {@link IHighEventModel}
     */
    @Override
    public Subscription sendHighEventRequest(String begTime, String endTime) {
        Observable<HighEventBean> observable = RetrofitServiceManager.getHighEventResult(begTime, endTime);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<HighEventBean>() {
            @Override
            public void onCompleted() {

            }

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

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