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

import com.casic.dcms.bean.MaintainRecordBean;
import com.casic.dcms.listener.OnGetMaintainRecordListener;
import com.casic.dcms.mvp.model.IMaintainRecordModel;
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 MaintainRecordModelImpl implements IMaintainRecordModel {

    private final OnGetMaintainRecordListener maintainRecordListener;

    public MaintainRecordModelImpl(OnGetMaintainRecordListener listener) {
        this.maintainRecordListener = listener;
    }

    /**
     * {@link IMaintainRecordModel}
     */
    @Override
    public Subscription sendMaintainRecordRequest(String startDate, String endDate, int offset) {
        Observable<MaintainRecordBean> observable = RetrofitServiceManager.getMaintainRecordResult(startDate, endDate, offset);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<MaintainRecordBean>() {
            @Override
            public void onCompleted() {

            }

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

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