package com.casic.dcms.mvp.model;

import com.casic.dcms.bean.ActionResultBean;
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/12 14:13
 * @Email: 290677893@qq.com
 **/
public class CaseHandleModelImpl implements ICaseHandleModel {

    private OnHandleListener listener;

    public CaseHandleModelImpl(OnHandleListener submitListener) {
        this.listener = submitListener;
    }

    /**
     * 数据回调接口
     */
    public interface OnHandleListener {
        void onSuccess(ActionResultBean resultBean);

        void onFailure(Throwable throwable);
    }

    @Override
    public Subscription sendRetrofitRequest(String processId, String bizId, String currState, String changeState,
                                            String remarks, String fileId) {
        Observable<ActionResultBean> observable = RetrofitServiceManager.getHandleCaseResult(processId, bizId, currState, changeState, remarks, fileId);
        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) {
                    listener.onFailure(e);
                }
            }

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

    @Override
    public Subscription sendRetrofitRequest(String processId, String bizId, String currState, String changeState,
                                            String remarks, String fileId, String approvalResult) {
        Observable<ActionResultBean> observable = RetrofitServiceManager.getHandleCaseResult(processId, bizId, currState, changeState, remarks, fileId, approvalResult);
        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) {
                    listener.onFailure(e);
                }
            }

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

    @Override
    public Subscription sendRetrofitRequest(String processId, String bizId, String currState, String changeState,
                                            String remarks, String fileId, String eorc, String casetypeCode,
                                            String casetypeDetailCode, String lng, String lat, String caseLevel,
                                            String areaCode, String streetCode, String communityCode, String gridId,
                                            String fieldintro) {
        Observable<ActionResultBean> observable = RetrofitServiceManager.getHandleCaseResult(processId, bizId, currState,
                changeState, remarks, fileId, eorc, casetypeCode, casetypeDetailCode, lng, lat, caseLevel,
                areaCode, streetCode, communityCode, gridId, fieldintro);
        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) {
                    listener.onFailure(e);
                }
            }

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