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.ISubmitShopCaseModel;
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 SubmitShopCaseModelImpl implements ISubmitShopCaseModel {

    private final OnActionResultListener actionResultListener;

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

    /**
     * {@link ISubmitShopCaseModel}
     */
    @Override
    public Subscription sendShopCaseSubmitRequest(String isNeedVerify, String isNeedRevisit, String longitude, String latitude, String source, String caseLevel, String areaCode, String streetCode, String communityCode, String gridId, String description, String fieldintro, String fileIdVerify, int bzhours, int bzminis, String shopId, String shopkeeperName, String shopPhone) {
        Observable<ActionResultBean> observable = RetrofitServiceManager.getSubmitShopCaseResult(isNeedVerify, isNeedRevisit, longitude, latitude,
                source, caseLevel, areaCode, streetCode, communityCode, gridId, description, fieldintro, fileIdVerify,
                bzhours, bzminis, shopId, shopkeeperName, shopPhone);
        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);
                }
            }
        });
    }

}
