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.ICaseHandleModel;
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 CaseHandleModelImpl implements ICaseHandleModel {

    private final OnActionResultListener actionResultListener;

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

    /**
     * {@link ICaseHandleModel}
     * <p>
     * 案卷核实-仅添加核实意见/核实情况/核实图片
     */
    @Override
    public Subscription sendVerifyHandleRequest(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) {
                    actionResultListener.onFailure(e);
                }
            }

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

    /**
     * {@link ICaseHandleModel}
     * <p>
     * 超时案卷详情页处理
     */
    @Override
    public Subscription sendOvertimeHandleRequest(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) {
                    actionResultListener.onFailure(e);
                }
            }

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

    /**
     * {@link ICaseHandleModel}
     * <p>
     * 案卷核实-热线电话和微信公众号提交的数据，需要补全信息
     * 案卷核查详情页处理
     */
    @Override
    public Subscription sendCheckHandleRequest(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) {
                    actionResultListener.onFailure(e);
                }
            }

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

    //派遣员

    /**
     * {@link ICaseHandleModel}
     * <p>
     * 派遣员处理
     */
    @Override
    public Subscription sendDispatcherHandleRequest(String processId, String bizId, String currState, String changeState, String remarks, String operationKey, String taskUserId) {
        Observable<ActionResultBean> observable = RetrofitServiceManager.getDispatcherHandleCaseResult(processId,
                bizId, currState, changeState, remarks, operationKey, taskUserId);
        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);
                }
            }
        });
    }
}
