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

import com.casic.dcms.bean.CleanRecordBean;
import com.casic.dcms.listener.OnGetCleanRecordListener;
import com.casic.dcms.mvp.model.ICleanRecordModel;
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 CleanRecordModelImpl implements ICleanRecordModel {

    private final OnGetCleanRecordListener cleanRecordListener;

    public CleanRecordModelImpl(OnGetCleanRecordListener listener) {
        this.cleanRecordListener = listener;
    }

    /**
     * {@link ICleanRecordModel}
     */
    @Override
    public Subscription sendCleanRecordRequest(String userId, String keywords, String beginDate, String endDate, int offset) {
        Observable<CleanRecordBean> observable = RetrofitServiceManager.getCleanRecordResult(userId, keywords, beginDate, endDate, offset);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<CleanRecordBean>() {
            @Override
            public void onCompleted() {

            }

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

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