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

import com.casic.dcms.bean.ActionResultBean;
import com.casic.dcms.listener.OnActionResultListener;
import com.casic.dcms.mvp.model.ICancelAlarmModel;
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 CancelAlarmModelImpl implements ICancelAlarmModel {

    private final OnActionResultListener actionResultListener;

    public CancelAlarmModelImpl(OnActionResultListener listener) {
        this.actionResultListener = listener;
    }

    /**
     * {@link ICancelAlarmModel}
     */
    @Override
    public Subscription sendSingleCancelRequest(String id, String jobStatus, String handleMessage) {
        Observable<ActionResultBean> observable = RetrofitServiceManager.getCancelAlarmResult(id, jobStatus, handleMessage);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ActionResultBean>() {
            @Override
            public void onCompleted() {

            }

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

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

    /**
     * {@link ICancelAlarmModel}
     */
    @Override
    public Subscription sendBatchCancelRequest(String jobStatus, String handleMessage) {
        Observable<ActionResultBean> observable = RetrofitServiceManager.getCancelAlarmResult(jobStatus, handleMessage);
        return observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ActionResultBean>() {
            @Override
            public void onCompleted() {

            }

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

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